Analyzer::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Solidifier\Analyzers;
4
5
use Gaufrette\Filesystem;
6
use Solidifier\Events\TraverseEnd;
7
use Gaufrette\File;
8
use PhpParser\Parser;
9
use PhpParser\Lexer;
10
use PhpParser\NodeTraverser;
11
use Solidifier\Events\ChangeFile;
12
use Solidifier\Dispatcher;
13
use Solidifier\VisitableAnalyzer;
14
use Solidifier\Visitor;
15
16
class Analyzer implements VisitableAnalyzer
17
{
18
    private
19
        $nodeTraversers,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $nodeTraversers.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
20
        $dispatcher,
21
        $fs;
22
    
23
    public function __construct(Dispatcher $dispatcher, Filesystem $fs)
24
    {
25
        $this->nodeTraversers = array(
26
            'preAnalyze' => new NodeTraverser(),
27
            'analyze' => new NodeTraverser(),
28
        );
29
        
30
        $this->dispatcher = $dispatcher;
31
        $this->fs = $fs;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32
    }
33
    
34
    public function addVisitor($traverseName, Visitor $visitor)
35
    {
36
        if(! isset($this->nodeTraversers[$traverseName]))
37
        {
38
            throw new \RuntimeException("$traverseName is not a valid traverse step");
39
        }
40
        
41
        $visitor->setDispatcher($this->dispatcher);
42
        $this->nodeTraversers[$traverseName]->addVisitor($visitor);
43
        
44
        return $this;
45
    }
46
    
47
    public function run()
48
    {
49
        $nodes = $this->parseFiles();
50
        
51
        $this->preAnalyze($nodes);
52
        $this->analyze($nodes);
53
        
54
        $this->dispatcher->dispatch(new TraverseEnd());
55
    }
56
    
57
    private function parseFiles()
58
    {
59
        $nodes = array();
60
        
61
        $adapter = $this->fs->getAdapter();
62
        
63
        $iterator = new \RegexIterator(
64
            new \ArrayIterator($this->fs->keys()), 
65
            '~.php$~'
66
        );
67
        
68
        foreach($iterator as $key)
69
        {
70
            if($adapter->isDirectory($key) === false)
71
            {
72
                $nodes[$key] = $this->parseFile($this->fs->get($key));
73
            }
74
        }
75
        
76
        return $nodes;
77
    }
78
    
79
    private function parseFile(File $file)
80
    {
81
        $parser = new Parser(new Lexer());
82
    
83
        return $parser->parse($file->getContent());
84
    }    
85
    
86
    private function preAnalyze(array $nodes)
87
    {
88
        $this->traverse($nodes, $this->nodeTraversers['preAnalyze']);
89
    }
90
    
91
    private function analyze(array $nodes)
92
    {
93
        $this->traverse($nodes, $this->nodeTraversers['analyze']);
94
    }
95
    
96
    private function traverse(array $nodes, NodeTraverser $traverser)
97
    {
98
        foreach($nodes as $file => $stmts)
99
        {
100
            $this->dispatcher->dispatch(new ChangeFile($file));
101
            $traverser->traverse($stmts);
102
        }
103
    }
104
}