Completed
Pull Request — master (#251)
by
unknown
11:10
created

Analyzer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.75%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 50
cts 59
cp 0.8475
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A bind() 0 20 1
B registerScalarPasses() 0 17 5
B registerExpressionPasses() 0 17 5
B registerStatementPasses() 0 17 5
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
7
namespace PHPSA;
8
9
use PHPSA\Analyzer\EventListener\ExpressionListener;
10
use PHPSA\Analyzer\EventListener\ScalarListener;
11
use PHPSA\Analyzer\EventListener\StatementListener;
12
use PHPSA\Analyzer\Pass\AnalyzerPassInterface;
13
use Webiny\Component\EventManager\EventManager;
14
15
class Analyzer
16
{
17
    /**
18
     * @var EventManager
19
     */
20
    protected $eventManager;
21
22
    /**
23
     * @var []AnalyzerPassInterface[]
24
     */
25
    protected $bindOnExpressions = [];
26
27
    /**
28
     * @var []AnalyzerPassInterface[]
29
     */
30
    protected $bindOnStatements = [];
31
32
    /**
33
     * @var []AnalyzerPassInterface[]
34
     */
35
    protected $bindOnScalars = [];
36
37
    /**
38
     * @param EventManager $eventManager
39
     */
40 43
    public function __construct(EventManager $eventManager)
41
    {
42 43
        $this->eventManager = $eventManager;
43 43
    }
44
45
    /**
46
     * @param array $expressionPasses all the expression analyzers
47
     * @throws \RuntimeException if the analyzer does not implement the required interface
48
     */
49 43
    public function registerExpressionPasses(array $expressionPasses)
50
    {
51 43
        foreach ($expressionPasses as $pass) {
52 22
            if (!$pass instanceof AnalyzerPassInterface) {
53
                throw new \RuntimeException('Analyzer pass must implement AnalyzerPassInterface');
54
            }
55
56 22
            $bindOnExpressions = $pass->getRegister();
57 22
            foreach ($bindOnExpressions as $bindOnExpression) {
58 22
                if (isset($this->bindOnExpressions[$bindOnExpression])) {
59
                    $this->bindOnExpressions[$bindOnExpression][] = $pass;
60
                } else {
61 22
                    $this->bindOnExpressions[$bindOnExpression] = [$pass];
62
                }
63 22
            }
64 43
        }
65 43
    }
66
67
    /**
68
     * @param array $statementPasses all the statement analyzers
69
     * @throws \RuntimeException if the analyzer does not implement the required interface
70
     */
71 43
    public function registerStatementPasses(array $statementPasses)
72
    {
73 43
        foreach ($statementPasses as $pass) {
74 20
            if (!$pass instanceof AnalyzerPassInterface) {
75
                throw new \RuntimeException('Analyzer pass must implement AnalyzerPassInterface');
76
            }
77
78 20
            $bindOnStatements = $pass->getRegister();
79 20
            foreach ($bindOnStatements as $bindOnStatement) {
80 20
                if (isset($this->bindOnStatements[$bindOnStatement])) {
81
                    $this->bindOnStatements[$bindOnStatement][] = $pass;
82
                } else {
83 20
                    $this->bindOnStatements[$bindOnStatement] = [$pass];
84
                }
85 20
            }
86 43
        }
87 43
    }
88
89
    /**
90
     * @param array $scalarPasses all the scalar analyzers
91
     * @throws \RuntimeException if the analyzer does not implement the required interface
92
     */
93 43
    public function registerScalarPasses(array $scalarPasses)
94
    {
95 43
        foreach ($scalarPasses as $pass) {
96 1
            if (!$pass instanceof AnalyzerPassInterface) {
97
                throw new \RuntimeException('Analyzer pass must implement AnalyzerPassInterface');
98
            }
99
100 1
            $bindOnScalars = $pass->getRegister();
101 1
            foreach ($bindOnScalars as $bindOnScalar) {
102 1
                if (isset($this->bindOnScalars[$bindOnScalar])) {
103
                    $this->bindOnScalars[$bindOnScalar][] = $pass;
104
                } else {
105 1
                    $this->bindOnScalars[$bindOnScalar] = [$pass];
106
                }
107 1
            }
108 43
        }
109 43
    }
110
111
    /**
112
     * binds the listeners
113
     */
114 43
    public function bind()
115
    {
116 43
        $this->eventManager->listen(Compiler\Event\ExpressionBeforeCompile::EVENT_NAME)
117 43
            ->handler(
118 43
                new ExpressionListener($this->bindOnExpressions)
119 43
            )
120 43
            ->method('beforeCompile');
121
122 43
        $this->eventManager->listen(Compiler\Event\StatementBeforeCompile::EVENT_NAME)
123 43
            ->handler(
124 43
                new StatementListener($this->bindOnStatements)
125 43
            )
126 43
            ->method('beforeCompile');
127
128 43
        $this->eventManager->listen(Compiler\Event\ScalarBeforeCompile::EVENT_NAME)
129 43
            ->handler(
130 43
                new ScalarListener($this->bindOnScalars)
131 43
            )
132 43
            ->method('beforeCompile');
133 43
    }
134
}
135