Chapter 17. API

17.1. Data Source Identification

Basic or Complex Strategy to identify the Data Source.

[Warning]

Now, and for the following chapters, we will not mention how you load the classes. Depending of the install strategy you’ve adopted, Composer or other, don’t forget to load your autoloader.

Compare to version 2, Reflect 3 offers two simple strategies to identify the data source.

First, is to give the relative or absolute path to file or directory to parse (without limitation).

Second, is to specify options to customize parsing process, to the Symfony Finder Component.

17.1.1. Basic Strategy

With all SAPI, no JSON config file is required (as it was for Reflect 2). You have just to give the relative or absolute path to file or directory to parse.

It’s also possible to specify any archive (phar, zip, tar, tgz, gz, rar) as file source.

Example with a simple file or directory (absolute path). 

$ phpreflect analyser:run /absolute/path/to/source

Example with a simple file or directory (relative path). 

$ phpreflect analyser:run ./relative/path/to/source

17.1.2. Complex Strategy

Still as it was with Reflect 2, you will need to configure your data source in a JSON file.

Syntax is closed to the Symfony Finder Component that is used to limit data source contents to parse.

Example to parse an archive. 

{
    "source-providers": [
        {
            "in": "phar:///var/dist/owncloud-7.0.2.tar as owncloud7",
            "name": "*.php",
            "exclude": ["3rdparty"]
        }
    ],
    "plugins": [
    ],
    "analysers" : [
    ]
}

[Important]

Do not forget the phar:// protocol in front of archive identification.

[Tip]

Use alias named here owncloud7 to identify data source entry in the JSON config file, rather than the full path phar:///var/dist/owncloud-7.0.2.tar.

Example to parse a directory. 

{
    "source-providers": [
        {
            "in": "/home/github/phing/ as phing2",
            "path": ["bin", "classes"],
            "exclude": ["test"],
            "name": "*.php"
        }
    ],
    "plugins": [
    ],
    "analysers" : [
    ]
}

Learn more about directives, see Section 8.1, “section Source Providers”

Whatever SAPI you use, all metrics (for each analysers asked) are available at end of parse, in the same format.

With CLI, and Reflect source code, to get a structure report, you have to invoke the following command :

$ phpreflect analyser:run /home/github/php-reflect/src

With others SAPI, use example https://raw.githubusercontent.com/llaville/php-reflect/3.0/examples/api_analyser_run.php

and you should obtain something like this :

Data Source Analysed

Directories                                         20
Files                                               73

Structure
  Namespaces                                        20
  Interfaces                                         8
  Traits                                             0
  Classes                                           65
    Abstract Classes                                 6 (9.23%)
    Concrete Classes                                59 (90.77%)
  Methods                                          295
    Scope
      Non-Static Methods                           282 (95.59%)
      Static Methods                                13 (4.41%)
    Visibility
      Public Method                                251 (85.08%)
      Protected Method                              34 (11.53%)
      Private Method                                10 (3.39%)
  Functions                                         12
    Named Functions                                  0 (0.00%)
    Anonymous Functions                             12 (100.00%)
  Constants                                         22
    Global Constants                                 0 (0.00%)
    Magic Constants                                  3 (13.64%)
    Class Constants                                 19 (86.36%)
  Tests
    Classes                                          0
    Methods                                          0

This is the default render. But, if you want to compare with other SAPI, activate the debug verbose mode (-vvv) to get the raw response. You should obtain something like this :

Array
(
    [files] => Array
        (
            [0] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/AbstractAnalyser.php
            [1] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/AnalyserInterface.php
            [2] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/AnalyserManager.php
            [3] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/LocAnalyser.php
            [4] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/ReflectionAnalyser.php
            [5] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/StructureAnalyser.php
            [6] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Analyser.php
            [7] => /home/github/php-reflect/src/Bartlett/Reflect/Api/BaseApi.php
            [8] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Cache.php
            [9] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Config.php
            [10] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Diagnose.php
            [11] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Diagram.php
            [12] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Plugin.php
            [13] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Reflection.php
            [14] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Analyser.php
            [15] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Cache.php
            [16] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Common.php
            [17] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Config.php
            [18] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Diagnose.php
            [19] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Diagram.php
            [20] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Plugin.php
            [21] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Reflection.php
            [22] => /home/github/php-reflect/src/Bartlett/Reflect/Client/ClientInterface.php
            [23] => /home/github/php-reflect/src/Bartlett/Reflect/Client/LocalClient.php
            [24] => /home/github/php-reflect/src/Bartlett/Reflect/Client.php
            [25] => /home/github/php-reflect/src/Bartlett/Reflect/Collection/ReflectionCollection.php
            [26] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Application.php
            [27] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Command.php
            [28] => /home/github/php-reflect/src/Bartlett/Reflect/Console/CommandFactory.php
            [29] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Formatter/LocOutputFormatter.php
            [30] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Formatter/OutputFormatter.php
            [31] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Formatter/StructureOutputFormatter.php
            [32] => /home/github/php-reflect/src/Bartlett/Reflect/Environment.php
            [33] => /home/github/php-reflect/src/Bartlett/Reflect/Event/AbstractDispatcher.php
            [34] => /home/github/php-reflect/src/Bartlett/Reflect/Event/CacheAwareEventDispatcher.php
            [35] => /home/github/php-reflect/src/Bartlett/Reflect/Event/DispatcherInterface.php
            [36] => /home/github/php-reflect/src/Bartlett/Reflect/Events.php
            [37] => /home/github/php-reflect/src/Bartlett/Reflect/Exception/Exception.php
            [38] => /home/github/php-reflect/src/Bartlett/Reflect/Exception/ModelException.php
            [39] => /home/github/php-reflect/src/Bartlett/Reflect/Model/AbstractFunctionModel.php
            [40] => /home/github/php-reflect/src/Bartlett/Reflect/Model/AbstractModel.php
            [41] => /home/github/php-reflect/src/Bartlett/Reflect/Model/ClassModel.php
            [42] => /home/github/php-reflect/src/Bartlett/Reflect/Model/ConstantModel.php
            [43] => /home/github/php-reflect/src/Bartlett/Reflect/Model/FunctionModel.php
            [44] => /home/github/php-reflect/src/Bartlett/Reflect/Model/MethodModel.php
            [45] => /home/github/php-reflect/src/Bartlett/Reflect/Model/ParameterModel.php
            [46] => /home/github/php-reflect/src/Bartlett/Reflect/Model/PropertyModel.php
            [47] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Analyser.php
            [48] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Cache.php
            [49] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Config.php
            [50] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Diagnose.php
            [51] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Diagram.php
            [52] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Plugin.php
            [53] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Reflection.php
            [54] => /home/github/php-reflect/src/Bartlett/Reflect/PhpParser/NameResolver.php
            [55] => /home/github/php-reflect/src/Bartlett/Reflect/PhpParser/NodeProcessor.php
            [56] => /home/github/php-reflect/src/Bartlett/Reflect/PhpParser/NodeProcessorAbstract.php
            [57] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/CacheAdapterInterface.php
            [58] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/CacheStorageInterface.php
            [59] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/DefaultCacheStorage.php
            [60] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/DoctrineCacheAdapter.php
            [61] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/CachePlugin.php
            [62] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Log/DefaultLogger.php
            [63] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/LogPlugin.php
            [64] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Notifier/GrowlNotifier.php
            [65] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Notifier/NotifierInterface.php
            [66] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/NotifierPlugin.php
            [67] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/PluginInterface.php
            [68] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/PluginManager.php
            [69] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/ProfilerPlugin.php
            [70] => /home/github/php-reflect/src/Bartlett/Reflect/Tokenizer/DefaultTokenizer.php
            [71] => /home/github/php-reflect/src/Bartlett/Reflect/Util/Timer.php
            [72] => /home/github/php-reflect/src/Bartlett/Reflect.php
        )

    [Bartlett\Reflect\Analyser\StructureAnalyser] => Array
        (
            [namespaces] => 20
            [interfaces] => 8
            [traits] => 0
            [classes] => 65
            [abstractClasses] => 6
            [concreteClasses] => 59
            [functions] => 12
            [namedFunctions] => 0
            [anonymousFunctions] => 12
            [methods] => 295
            [publicMethods] => 251
            [protectedMethods] => 34
            [privateMethods] => 10
            [nonStaticMethods] => 282
            [staticMethods] => 13
            [constants] => 0
            [classConstants] => 19
            [globalConstants] => 0
            [magicConstants] => 3
            [testClasses] => 0
            [testMethods] => 0
        )

)
  • First entry in array is the list of parsed files
  • Second entry in array is the structure analyser result

Each analyser as its own data structure and results, but you will always get the fully qualified class name that identify origin of analyser used.

Example with two analysers (structure and loc). 

$ phpreflect analyser:run /home/github/php-reflect/src structure loc

Raw response
Array
(
    [files] => Array
        (
            ...
        )

    [Bartlett\Reflect\Analyser\StructureAnalyser] => Array
        (
            ...
        )

Array
(
    [files] => Array
        (
            [0] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/AbstractAnalyser.php
            [1] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/AnalyserInterface.php
            [2] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/AnalyserManager.php
            [3] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/LocAnalyser.php
            [4] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/ReflectionAnalyser.php
            [5] => /home/github/php-reflect/src/Bartlett/Reflect/Analyser/StructureAnalyser.php
            [6] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Analyser.php
            [7] => /home/github/php-reflect/src/Bartlett/Reflect/Api/BaseApi.php
            [8] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Cache.php
            [9] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Config.php
            [10] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Diagnose.php
            [11] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Diagram.php
            [12] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Plugin.php
            [13] => /home/github/php-reflect/src/Bartlett/Reflect/Api/Reflection.php
            [14] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Analyser.php
            [15] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Cache.php
            [16] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Common.php
            [17] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Config.php
            [18] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Diagnose.php
            [19] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Diagram.php
            [20] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Plugin.php
            [21] => /home/github/php-reflect/src/Bartlett/Reflect/Api/V3/Reflection.php
            [22] => /home/github/php-reflect/src/Bartlett/Reflect/Client/ClientInterface.php
            [23] => /home/github/php-reflect/src/Bartlett/Reflect/Client/LocalClient.php
            [24] => /home/github/php-reflect/src/Bartlett/Reflect/Client.php
            [25] => /home/github/php-reflect/src/Bartlett/Reflect/Collection/ReflectionCollection.php
            [26] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Application.php
            [27] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Command.php
            [28] => /home/github/php-reflect/src/Bartlett/Reflect/Console/CommandFactory.php
            [29] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Formatter/LocOutputFormatter.php
            [30] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Formatter/OutputFormatter.php
            [31] => /home/github/php-reflect/src/Bartlett/Reflect/Console/Formatter/StructureOutputFormatter.php
            [32] => /home/github/php-reflect/src/Bartlett/Reflect/Environment.php
            [33] => /home/github/php-reflect/src/Bartlett/Reflect/Event/AbstractDispatcher.php
            [34] => /home/github/php-reflect/src/Bartlett/Reflect/Event/CacheAwareEventDispatcher.php
            [35] => /home/github/php-reflect/src/Bartlett/Reflect/Event/DispatcherInterface.php
            [36] => /home/github/php-reflect/src/Bartlett/Reflect/Events.php
            [37] => /home/github/php-reflect/src/Bartlett/Reflect/Exception/Exception.php
            [38] => /home/github/php-reflect/src/Bartlett/Reflect/Exception/ModelException.php
            [39] => /home/github/php-reflect/src/Bartlett/Reflect/Model/AbstractFunctionModel.php
            [40] => /home/github/php-reflect/src/Bartlett/Reflect/Model/AbstractModel.php
            [41] => /home/github/php-reflect/src/Bartlett/Reflect/Model/ClassModel.php
            [42] => /home/github/php-reflect/src/Bartlett/Reflect/Model/ConstantModel.php
            [43] => /home/github/php-reflect/src/Bartlett/Reflect/Model/FunctionModel.php
            [44] => /home/github/php-reflect/src/Bartlett/Reflect/Model/MethodModel.php
            [45] => /home/github/php-reflect/src/Bartlett/Reflect/Model/ParameterModel.php
            [46] => /home/github/php-reflect/src/Bartlett/Reflect/Model/PropertyModel.php
            [47] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Analyser.php
            [48] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Cache.php
            [49] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Config.php
            [50] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Diagnose.php
            [51] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Diagram.php
            [52] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Plugin.php
            [53] => /home/github/php-reflect/src/Bartlett/Reflect/Output/Reflection.php
            [54] => /home/github/php-reflect/src/Bartlett/Reflect/PhpParser/NameResolver.php
            [55] => /home/github/php-reflect/src/Bartlett/Reflect/PhpParser/NodeProcessor.php
            [56] => /home/github/php-reflect/src/Bartlett/Reflect/PhpParser/NodeProcessorAbstract.php
            [57] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/CacheAdapterInterface.php
            [58] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/CacheStorageInterface.php
            [59] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/DefaultCacheStorage.php
            [60] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Cache/DoctrineCacheAdapter.php
            [61] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/CachePlugin.php
            [62] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Log/DefaultLogger.php
            [63] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/LogPlugin.php
            [64] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Notifier/GrowlNotifier.php
            [65] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/Notifier/NotifierInterface.php
            [66] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/NotifierPlugin.php
            [67] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/PluginInterface.php
            [68] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/PluginManager.php
            [69] => /home/github/php-reflect/src/Bartlett/Reflect/Plugin/ProfilerPlugin.php
            [70] => /home/github/php-reflect/src/Bartlett/Reflect/Tokenizer/DefaultTokenizer.php
            [71] => /home/github/php-reflect/src/Bartlett/Reflect/Util/Timer.php
            [72] => /home/github/php-reflect/src/Bartlett/Reflect.php
        )

    [Bartlett\Reflect\Analyser\LocAnalyser] => Array
        (
            [llocClasses] => 1115
            [llocByNoc] => 0
            [llocByNom] => 0
            [llocFunctions] => 79
            [llocByNof] => 0
            [llocGlobal] => 0
            [classes] => 65
            [functions] => 12
            [methods] => 323
            [cloc] => 137
            [eloc] => 3117
            [lloc] => 1194
            [wloc] => 366
            [loc] => 3620
            [ccn] => 610
            [ccnMethods] => 556
        )

)

)