The Command-Line

The command-line interface is the easiest way to try and learn the basic Reflect features.

For all users.

Command-Line Options

Without plugins and analysers sections in your phpreflect.json configuration file, when you invoke the phpreflect command, you should obtain the following commands and options :

        _           ____       __ _           _
  _ __ | |__  _ __ |  _ \ ___ / _| | ___  ___| |_
 | '_ \| '_ \| '_ \| |_) / _ \ |_| |/ _ \/ __| __|
 | |_) | | | | |_) |  _ <  __/  _| |  __/ (__| |_
 | .__/|_| |_| .__/|_| \_\___|_| |_|\___|\___|\__|
 |_|         |_|

phpReflect version 3.1.2

Usage:
 [options] command [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --no-plugins      Disables all plugins.
      --profile         Display timing and memory usage information.
      --progress        Show progress bar.
      --output=OUTPUT   Write results to file or URL.
      --manifest        Show which versions of dependencies are bundled.
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
 help                  Displays help for a command
 list                  Lists commands
analyser
 analyser:list         List all analysers available.
 analyser:run          Analyse a data source and display results.
cache
 cache:clear           Clear cache (any adapter and backend).
config
 config:validate       Validates a JSON configuration file.
diagnose
 diagnose:run          Diagnoses the system to identify common errors.
diagram
 diagram:class         Generates diagram about a user class present in a data source.
 diagram:package       Generates diagram about namespaces in a data source.
plugin
 plugin:list           List all plugins installed.
reflection
 reflection:class      Reports information about a user class present in a data source.
 reflection:function   Reports information about a user function present in a data source.
config:validate
Validates an optional JSON config file.
$ phpreflect config:validate
"/etc/phpreflect.json" config file is valid.
diagnose:run
Diagnoses the system to identify common errors.
$ phpreflect diagnose:run
Checking php settings:
- Requires PHP 5.3.2 or better OK
- php.ini file loaded C:\UwAmp\bin\php\php-5.6.13\php.ini
- date extension loaded YES
- json extension loaded YES
- pcre extension loaded YES
- phar extension loaded YES
- reflection extension loaded YES
- spl extension loaded YES
- tokenizer extension loaded YES
Use verbose level 2 for more details, and level 3 to get raw response
diagram:class
Generates diagram about a user class present in a data source.
$ phpreflect diagram:class Bartlett\Reflect\Plugin\LogPlugin ./src
diagram:package
Generates diagram about namespaces in a data source.
$ phpreflect diagram:package --engine=graphviz ./src
plugin:list
List all plugins configured (and correctly installed) in plugins section of your phpreflect.json config file.
Without plugins, you will get
$ phpreflect plugin:list
No plugin installed.
With only Cache plugin configured, you will get
$ phpreflect plugin:list
 Plugin Class                           Events Subscribed
 Bartlett\Reflect\Plugin\CachePlugin    reflect.progress
                                        reflect.success
                                        reflect.complete
analyser:list
List all analysers configured in analysers section of your phpreflect.json config file, and available by default.
With only default analysers, you will get
$ phpreflect analyser:list
 Analyser Name Analyser Class
 loc           Bartlett\Reflect\Analyser\LocAnalyser
 reflection    Bartlett\Reflect\Analyser\ReflectionAnalyser
 structure     Bartlett\Reflect\Analyser\StructureAnalyser
analyser:run
Parse a data source and display results. May vary depending of the data source and analyser used.
With structure analyser and the Reflect source code, you will get something like
$ phpreflect analyser:run .
Possible alternative (if you use the default json config file)
$ phpreflect analyser:run --alias current
Data Source Analysed

Directories                                         20
Files                                               72

Structure
  Namespaces                                        20
  Interfaces                                         8
  Traits                                             0
  Classes                                           64
    Abstract Classes                                 6 (9.38%)
    Concrete Classes                                58 (90.62%)
  Methods                                          293
    Scope
      Non-Static Methods                           280 (95.56%)
      Static Methods                                13 (4.44%)
    Visibility
      Public Method                                250 (85.32%)
      Protected Method                              34 (11.60%)
      Private Method                                 9 (3.07%)
  Functions                                         13
    Named Functions                                  0 (0.00%)
    Anonymous Functions                             13 (100.00%)
  Constants                                         22
    Global Constants                                 0 (0.00%)
    Magic Constants                                  3 (13.64%)
    Class Constants                                 19 (86.36%)
  Tests
    Classes                                          0
    Methods                                          0
Filter results with a closure (available since version 3.1.0)
$ phpreflect analyser:run --filter=YourFilters.php .
<?php
$closure = function ($data) {
    $filterOnKeys = array(
        'namespaces',
        'interfaces',
        'traits',
        'classes', 'abstractClasses', 'concreteClasses',
        'functions', 'namedFunctions', 'anonymousFunctions',
        'classConstants', 'globalConstants', 'magicConstants',
    );

    foreach ($data as $title => &$keys) {
        if (strpos($title, 'StructureAnalyser') === false) {
            continue;
        }
        // looking into Structure Analyser metrics only
        foreach ($keys as $key => $val) {
            if (!in_array($key, $filterOnKeys)) {
                unset($keys[$key]);  // "removed" unsolicited values
                continue;
            }
        }
    }
    return $data;
};

return $closure;
Data Source Analysed

Directories                                         20
Files                                               72

Structure
  Namespaces                                        20
  Interfaces                                         8
  Traits                                             0
  Classes                                           64
    Abstract Classes                                 6 (9.38%)
    Concrete Classes                                58 (90.62%)
  Functions                                         13
    Named Functions                                  0 (0.00%)
    Anonymous Functions                             13 (100.00%)
  Constants                                         22
    Global Constants                                 0 (0.00%)
    Magic Constants                                  3 (13.64%)
    Class Constants                                 19 (86.36%)
The filter’s file that host the $closure, must be resolvable through the include_path.
reflection:class
Reports information about a user class present in a data source.
With the Reflect source code (./src), and Bartlett\Reflect class
$ phpreflect reflection:class Bartlett\Reflect ./src
Class [ <user> class Bartlett\Reflect extends Bartlett\Reflect\Event\AbstractDispatcher ] {
  @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 45 - 340

  - Constants [0] {
  }

  - Properties [2] {
    Property [ private $analysers ]
    Property [ private $dataSourceId ]
  }

  - Methods [6] {
    Method [ <user> public method __construct ] {
      @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 53 - 57

      - Parameters [0] {
      }
    }

    Method [ <user> public method addAnalyser ] {
      @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 66 - 71

      - Parameters [1] {
        Parameter #0 [ <required> PhpParser\NodeVisitor $analyser ]
      }
    }

    Method [ <user> public method getAnalysers ] {
      @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 78 - 81

      - Parameters [0] {
      }
    }

    Method [ <user> public method setDataSourceId ] {
      @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 90 - 94

      - Parameters [1] {
        Parameter #0 [ <required> $id ]
      }
    }

    Method [ <user> public method getDataSourceId ] {
      @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 101 - 104

      - Parameters [0] {
      }
    }

    Method [ <user> public method parse ] {
      @@ C:\home\github\php-reflect\src\Bartlett\Reflect.php 113 - 339

      - Parameters [1] {
        Parameter #0 [ <required> Symfony\Component\Finder\Finder $finder ]
      }
    }
  }
}

Summary

Let’s review what we’ve learned about the command-line interface :

  • It’s a Symfony Console Component that can be extended to infinite via plugins and analysers.

  • Default analyser produced results such as PHPLoc by Sebastian Bergmann.

Next

For PHP developers only.

Read more
  • Want to learn more about Reflect architecture and /or you want to extends it to match your needs, have a look on developer’s guide.