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 two interfaces Bartlett\Reflect\Analyser\AnalyserInterface, and PhpParser\NodeVisitor.

Your first own analyser may start like that :

<?php

namespace YourNamespace;

use Bartlett\Reflect\Analyser\AnalyserInterface;
use PhpParser\NodeVisitor;

class YourAnalyser implements AnalyserInterface, NodeVisitor
{
    //
    // AnalyserInterface methods
    //

    public function getSubject()
    {
    }

    public function getCurrentFile()
    {
    }

    public function getTokens()
    {
    }

    public function setSubject(Reflect $reflect)
    {
    }

    public function setTokens(array $tokens)
    {
    }

    public function setCurrentFile($path)
    {
    }

    public function getMetrics()
    {
    }

    public function getName()
    {
    }

    public function getNamespace()
    {
    }

    public function getShortName()
    {
    }

    //
    // NodeVisitor methods
    //
    public function beforeTraverse(array $nodes)
    {
    }

    public function enterNode(Node $node)
    {
    }

    public function leaveNode(Node $node)
    {
    }

    public function afterTraverse(array $nodes)
    {
    }
}

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

Your analyser became as simple like that:

<?php

namespace YourNamespace;

use Bartlett\Reflect\Analyser\AbstractAnalyser;

class YourAnalyser extends AbstractAnalyser
{
}

Print results

Once all nodes of AST built by PHP-Parser were traversed, you have to publish your results with the getMetrics() method.

Results must be organized as a key/values pair array, where key is the fully qualified name of your analyser (E.g: YourNamespace\YourAnalyser), and values are your metrics (free organization).

At end of API analyser/run, your metrics are returned, and may be exploited as you want. You are free to create a custom render or not. If no output formatter is provided in namespace YourNamespace\Console\Formatter, a simple PHP print_r() format is returned.

This is the default format in debug mode (verbose level 3 in CLI).

Here is a pattern of skeleton.

<?php

namespace YourNamespace\Console\Formatter;

use Symfony\Component\Console\Output\OutputInterface;

class YourAnalyserOutputFormatter
{
    // $output   === instance of console output
    // $response === your metrics returned by the +YourNamespace\YourAnalyser\getMetrics()+

    public function __invoke(OutputInterface $output, $response)
    {
    }
}