Passed
Push — trunk ( 516571...cb9bb6 )
by Christian
10:00 queued 11s
created

NoEnvironmentHelperInsideCompilerPassRule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 41
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B processNode() 0 29 8
A getNodeType() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules;
4
5
use PhpParser\Node;
6
use PhpParser\Node\Expr\StaticCall;
7
use PhpParser\Node\Identifier;
8
use PhpParser\Node\Name;
9
use PHPStan\Analyser\Scope;
0 ignored issues
show
Bug introduced by
The type PHPStan\Analyser\Scope was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use PHPStan\Rules\Rule;
0 ignored issues
show
Bug introduced by
The type PHPStan\Rules\Rule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PHPStan\Rules\RuleError;
0 ignored issues
show
Bug introduced by
The type PHPStan\Rules\RuleError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Shopware\Core\DevOps\Environment\EnvironmentHelper;
13
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14
15
/**
16
 * @internal
17
 *
18
 * @implements Rule<StaticCall>
19
 */
20
class NoEnvironmentHelperInsideCompilerPassRule implements Rule
21
{
22
    public function getNodeType(): string
23
    {
24
        return StaticCall::class;
25
    }
26
27
    /**
28
     * @param StaticCall $node
29
     *
30
     * @return array<array-key, RuleError|string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, RuleError|string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, RuleError|string>.
Loading history...
31
     */
32
    public function processNode(Node $node, Scope $scope): array
33
    {
34
        $class = $scope->getClassReflection();
35
36
        if ($class === null) {
37
            return [];
38
        }
39
40
        if (!$class->implementsInterface(CompilerPassInterface::class)) {
41
            return [];
42
        }
43
44
        if (!$node->name instanceof Identifier) {
0 ignored issues
show
Bug introduced by
Accessing name on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
45
            return [];
46
        }
47
48
        if ((string) $node->name !== 'getVariable' && (string) $node->name !== 'hasVariable') {
49
            return [];
50
        }
51
52
        if (!$node->class instanceof Name) {
0 ignored issues
show
Bug introduced by
Accessing class on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
            return [];
54
        }
55
56
        if ((string) $node->class !== EnvironmentHelper::class) {
57
            return [];
58
        }
59
60
        return ['Do not use EnvironmentHelper inside compiler passes.'];
61
    }
62
}
63