Properties

$parser

$parser : \PhpParser\Parser

The instance of Parser used to run this analysis.

Type

\PhpParser\Parser

$nodeTraverser

$nodeTraverser : \PhpParser\NodeTraverserInterface

The NodeTraverserInterface used to run this analysis.

Type

\PhpParser\NodeTraverserInterface

$result

$result : \Pvra\AnalysisResult

The result-instance used to store results from this analysis.

Type

\Pvra\AnalysisResult

$filePath

$filePath : string

The relative or absolute path to the file target of this analysis.

Type

string

Methods

__construct()

__construct(string  $file, boolean  $registerNameResolver = true) 

FileAnalyser constructor

Validates the given file path and calls the parent's constructor.

Parameters

string $file

The code to analyse

boolean $registerNameResolver

If set to true PhpParser\NodeVisitor\NameResolver will be added as the first

visitor. This may negatively affect performance, some Visitors depend on resolved names, however.

getNodeTraverser()

getNodeTraverser() : \PhpParser\NodeTraverserInterface

Gets the associated NodeTraverser

This method returns the associated NodeTraverser. If no NodeTraverser has been attached a default one will be created.

Returns

\PhpParser\NodeTraverserInterface —

The NodeTraverser associated to this instance.

hasNodeTraverserAttached()

hasNodeTraverserAttached() : boolean

Check whether a NodeTraverser has been attached to the current Analyser

Returns

boolean —

Returns true when Analyser::$nodeTraverser has been initialized.

attachRequirementVisitor()

attachRequirementVisitor(\PhpParser\NodeVisitor|\Pvra\AnalyserAwareInterface  $visitor) : $this

Attaches a RequirementVisitor.

This method should be used to attach an instance of Pvra\PhpParser\RequirementAnalyserAwareInterface to the current analyser. This method makes sure that RequirementAnalyserAwareInterface::setOwningAnalyser is called using the correct parameters.

Parameters

\PhpParser\NodeVisitor|\Pvra\AnalyserAwareInterface $visitor

Returns

$this —

Returns the current instance to allow chained calls.

attachRequirementVisitors()

attachRequirementVisitors(array<mixed,\Pvra\AnalyserAwareInterface>  $visitors) : $this

Attach an array of RequirementVisitors

This method can be used to attach multiple RequirementAnalyserAwareInterface instances at once. Analyser::attachRequirementVisitor is called upon each instance.

Parameters

array<mixed,\Pvra\AnalyserAwareInterface> $visitors

An array of requirement visitors

Returns

$this —

Returns the current instance to allow chained calls.

run()

run() : \Pvra\AnalysisResult

Execute the current Analysis

Parses the given code and runs the currently attached visitors. If run has already been called the previously generated result will be returned. The result instance returned by this method is sealed. Visitors that are attached after run is called are ignored on subsequent calls.

Returns

\Pvra\AnalysisResult

The sealed result.

isAnalyserRun()

isAnalyserRun() : boolean

Determines of this analyser is already run

The result is based on the presence of a AnalysisResult instance and its state.

Returns

boolean —

Whether the analyser has a result instance and it was sealed.

getResult()

getResult() : \Pvra\AnalysisResult

Get the result instance associated with this Analyser

If a AnalysisResult instance is attached it will be returned. If none was attached a default one is attached. Please be aware that you cannot set a custom MessageFormatter in an instance created using this.

Returns

\Pvra\AnalysisResult

The AnalysisResult instance attached with this analyser

setResultInstance()

setResultInstance(\Pvra\AnalysisResult  $result) : $this

Attach a (new) ResultInstance to this analyser

Set a new result instance to this analyser. If a result was already attached or the to be attached result is already sealed an exception is thrown.

Parameters

\Pvra\AnalysisResult $result

The AnalysisResult to be attached to this analyser

Throws

\Exception

Thrown in case that the given result is sealed or an Result instance was already attached.

Returns

$this —

Returns the current instance to allow method chaining

getParser()

getParser() : \PhpParser\Parser

Returns

\PhpParser\Parser

hasParserAttached()

hasParserAttached() : boolean

Returns

boolean

setTraverser()

setTraverser(\PhpParser\NodeTraverser|\PhpParser\NodeTraverserInterface  $nodeTraverser) 

Set the NodeTraverser used by this instance

This method allows you to set a NodeTraverser before the default one is initialized. This should usually happen in the constructor of a child class.

Parameters

\PhpParser\NodeTraverser|\PhpParser\NodeTraverserInterface $nodeTraverser

parse()

parse() : array<mixed,\PhpParser\Node>

Parse the given code.

Implementations of this method should parse the given code and return a list of Node instances. The simplest implementation may directly return return $this->getParser()->getParse($this->getCode());.

Returns

array<mixed,\PhpParser\Node> —

List of nodes representing the analysing target.

createAnalysisTargetId()

createAnalysisTargetId() : string

Create an identifier for the parsed content.

Implementations of this method should return a string that can be used to identify a given source. This may be achieved by hashing a given string or returning an absolute path.

Returns

string —

Identifier to identify the target of this analyser.

setParser()

setParser(\PhpParser\Parser  $parser) 

Parameters

\PhpParser\Parser $parser

initDefaultTraverser()

initDefaultTraverser() 

Initiate this instance using the default Traverser delivered by PHP-Parser

initDefaultParser()

initDefaultParser() 

isFileValid()

isFileValid(string  $file) : boolean

Validate a given file path.

Parameters

string $file

Path to the file

Returns

boolean —

Returns true if $file is a file and is readable. Returns fails otherwise.