Completed
Pull Request — master (#232)
by
unknown
05:28
created

Factory::getScalarPasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer;
7
8
use PHPSA\Analyzer;
9
use PHPSA\Configuration;
10
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
11
use Webiny\Component\EventManager\EventManager;
12
use PHPSA\Analyzer\Pass as AnalyzerPass;
13
14
class Factory
15
{
16
    /**
17
     * @return NodeDefinition[]
18
     */
19 1
    public static function getPassesConfigurations()
20
    {
21 1
        $configs = [];
22
23 1
        foreach (self::getExpressionPasses() as $passClass) {
24 1
            $configs[] = $passClass::getMetadata()->getConfiguration();
25 1
        }
26
27 1
        foreach (self::getStatementPasses() as $passClass) {
28 1
            $configs[] = $passClass::getMetadata()->getConfiguration();
29 1
        }
30
31 1
        foreach (self::getScalarPasses() as $passClass) {
32 1
            $configs[] = $passClass::getMetadata()->getConfiguration();
33 1
        }
34
35 1
        return $configs;
36
    }
37
38
    /**
39
     * @param EventManager $eventManager
40
     * @param Configuration $config
41
     * @return Analyzer
42
     */
43 1
    public static function factory(EventManager $eventManager, Configuration $config)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
44
    {
45 1
        $analyzersConfig = $config->getValue('analyzers');
46
47
        $filterEnabled = function ($passClass) use ($config, $analyzersConfig) {
48
            /** @var AnalyzerPass\Metadata $passMetadata */
49 1
            $passMetadata = $passClass::getMetadata();
50
51 1
            if (!$analyzersConfig[$passMetadata->getName()]['enabled']) {
52
                return false;
53
            }
54
55 1
            if (!$passMetadata->allowsPhpVersion($config->getValue('language_level'))) {
56
                return false;
57
            }
58
59 1
            return true;
60 1
        };
61
62 1
        $instanciate = function ($passClass) use ($analyzersConfig) {
63 1
            $passName = $passClass::getMetadata()->getName();
64
65 1
            return new $passClass($analyzersConfig[$passName]);
66 1
        };
67
68 1
        $analyzer = new Analyzer($eventManager);
69 1
        $analyzer->registerExpressionPasses(
70 1
            array_map($instanciate, array_filter(self::getExpressionPasses(), $filterEnabled))
71 1
        );
72 1
        $analyzer->registerStatementPasses(
73 1
            array_map($instanciate, array_filter(self::getStatementPasses(), $filterEnabled))
74 1
        );
75 1
        $analyzer->registerScalarPasses(
76 1
            array_map($instanciate, array_filter(self::getScalarPasses(), $filterEnabled))
77 1
        );
78 1
        $analyzer->bind();
79
80 1
        return $analyzer;
81
    }
82
83
    /**
84
     * @return array
85
     */
86 1
    private static function getStatementPasses()
87
    {
88
        return [
89 1
            AnalyzerPass\Statement\MagicMethodParameters::class,
90 1
            AnalyzerPass\Statement\GotoUsage::class,
91 1
            AnalyzerPass\Statement\GlobalUsage::class,
92 1
            AnalyzerPass\Statement\HasMoreThanOneProperty::class,
93 1
            AnalyzerPass\Statement\MissingBreakStatement::class,
94 1
            AnalyzerPass\Statement\MissingVisibility::class,
95 1
            AnalyzerPass\Statement\MethodCannotReturn::class,
96 1
            AnalyzerPass\Statement\UnexpectedUseOfThis::class,
97 1
            AnalyzerPass\Statement\TestAnnotation::class,
98 1
            AnalyzerPass\Statement\MissingDocblock::class,
99 1
            AnalyzerPass\Statement\OldConstructor::class,
100 1
            AnalyzerPass\Statement\ConstantNaming::class,
101 1
            AnalyzerPass\Statement\MissingBody::class,
102 1
            AnalyzerPass\Statement\InlineHtmlUsage::class,
103 1
            AnalyzerPass\Statement\AssignmentInCondition::class,
104 1
            AnalyzerPass\Statement\StaticUsage::class,
105 1
            AnalyzerPass\Statement\OptionalParamBeforeRequired::class,
106 1
            AnalyzerPass\Statement\YodaCondition::class,
107 1
        ];
108
    }
109
110
    /**
111
     * @return array
112
     */
113 1
    private static function getExpressionPasses()
114
    {
115
        return [
116
            // Another
117 1
            AnalyzerPass\Expression\ErrorSuppression::class,
118 1
            AnalyzerPass\Expression\MultipleUnaryOperators::class,
119 1
            AnalyzerPass\Expression\StupidUnaryOperators::class,
120 1
            AnalyzerPass\Expression\VariableVariableUsage::class,
121 1
            AnalyzerPass\Expression\Casts::class,
122 1
            AnalyzerPass\Expression\EvalUsage::class,
123 1
            AnalyzerPass\Expression\FinalStaticUsage::class,
124 1
            AnalyzerPass\Expression\CompareWithArray::class,
125 1
            AnalyzerPass\Expression\BacktickUsage::class,
126 1
            AnalyzerPass\Expression\LogicInversion::class,
127 1
            AnalyzerPass\Expression\ExitUsage::class,
128
            // Arrays
129 1
            AnalyzerPass\Expression\ArrayShortDefinition::class,
130 1
            AnalyzerPass\Expression\ArrayDuplicateKeys::class,
131 1
            AnalyzerPass\Expression\ArrayIllegalOffsetType::class,
132
            // Function call
133 1
            AnalyzerPass\Expression\FunctionCall\AliasCheck::class,
134 1
            AnalyzerPass\Expression\FunctionCall\DebugCode::class,
135 1
            AnalyzerPass\Expression\FunctionCall\RandomApiMigration::class,
136 1
            AnalyzerPass\Expression\FunctionCall\UseCast::class,
137 1
            AnalyzerPass\Expression\FunctionCall\DeprecatedIniOptions::class,
138 1
            AnalyzerPass\Expression\FunctionCall\RegularExpressions::class,
139 1
            AnalyzerPass\Expression\FunctionCall\ArgumentUnpacking::class,
140 1
            AnalyzerPass\Expression\FunctionCall\DeprecatedFunctions::class,
141 1
        ];
142
    }
143
144
    /**
145
     * @return array
146
     */
147 1
    private static function getScalarPasses()
148
    {
149
        return [
150 1
            AnalyzerPass\Scalar\CheckLNumberKind::class,
151 1
        ];
152
    }
153
}
154