Analysers implements the Visitor pattern in a simple and effective way to make the render of your results truly customizable.

Visitor pattern

Each Analyser class must implement interface Bartlett\Reflect\Visitor\VisitorInterface.

<?php

namespace Bartlett\Reflect\Visitor;

use Bartlett\Reflect\Model\Visitable;

interface VisitorInterface
{
    public function visit(Visitable $visitable);
}

Each element that need to be explored by your analyser should have a visit method accordingly.

  • For packages, we need to implement a visitPackageModel method.

  • For classes, we need to implement a visitClassModel method.

  • For properties, we need to implement a visitPropertyModel method.

  • For methods, we need to implement a visitMethodModel method.

  • For functions, we need to implement a visitFunctionModel method.

  • For function or method parameters, we need to implement a visitParameterModel method.

  • For constants, we need to implement a visitConstantModel method.

  • For includes, we need to implement a visitIncludeModel method.

  • For dependencies, we need to implement a visitDependencyModel method.

Abstract class Bartlett\Reflect\Visitor\AbstractVisitor ( source code ), that implement interface Bartlett\Reflect\Visitor\VisitorInterface, holds a basic visitor.
<?php

use Bartlett\Reflect\Visitor\AbstractVisitor;

class Analyser extends AbstractVisitor
{
    public function visitPackageModel($package)
    {
    }

    public function visitClassModel($class)
    {
    }

    public function visitPropertyModel($property)
    {
    }

    public function visitMethodModel($method)
    {
    }

    public function visitFunctionModel($function)
    {
    }

    public function visitParameterModel($parameter)
    {
    }

    public function visitConstantModel($constant)
    {
    }

    public function visitIncludeModel($include)
    {
    }

    public function visitDependencyModel($dependency)
    {
    }
}

An abstract class Bartlett\Reflect\Analyser\AbstractAnalyser ( source code ) that implement all required interfaces may be used to initialize common data in a simple way.

Your analyser became as simple like that:

<?php

use Bartlett\Reflect\Analyser\AbstractAnalyser;

class Analyser extends AbstractAnalyser
{
}

Print results

Once you have used visit methods to explore parsing results, you probably want to display it.

To do so, you should implement the render() method of Bartlett\Reflect\Analyser\AnalyserInterface.

Each analyser is responsible to display results with the render() method. You can either used the Bartlett\Reflect\Printer\Text helper to produces report line by line, or any other solution.

If you used the printer text helper, your implementation should follow only one rule. Return an array with :

  • free data identifier as key.

  • data contents are an array with a string format compatible vsprintf as first element, and the value as second element.

Example 1. Console lines information (without data)
<?php

    $lines['dataSourceAnalysed'] = array(
        '<info>Data Source Analysed</info>%s',
        array(PHP_EOL)
    );

    $lines['methodsScope'] = array(
        '    Scope',
        array()
    );
Example 2. Console line information with only one value
<?php

    $lines['methods'] = array(
        '  Methods                                   %10d',
        array($count['methods'])
    );
Example 3. Console line information with more than one value
<?php

    $lines['nonStaticMethods'] = array(
        '      Non-Static Methods                    %10d (%.2f%%)',
        array(
            $count['nonStaticMethods'],
            $count['methods'] > 0 ? ($count['nonStaticMethods'] / $count['methods']) * 100 : 0,
        )
    );
See source code of Structure Analyser as example.