|
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\New_; |
|
7
|
|
|
use PHPStan\Analyser\Scope; |
|
|
|
|
|
|
8
|
|
|
use PHPStan\Reflection\ReflectionProvider; |
|
|
|
|
|
|
9
|
|
|
use PHPStan\Rules\Rule; |
|
|
|
|
|
|
10
|
|
|
use PHPStan\Rules\RuleError; |
|
|
|
|
|
|
11
|
|
|
use PHPStan\Rules\RuleErrorBuilder; |
|
|
|
|
|
|
12
|
|
|
use PHPStan\Type\Constant\ConstantStringType; |
|
|
|
|
|
|
13
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField; |
|
14
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField; |
|
15
|
|
|
use Shopware\Core\Framework\Log\Package; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @implements Rule<New_> |
|
19
|
|
|
* |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
|
|
#[Package('core')] |
|
23
|
|
|
class NoDALAutoload implements Rule |
|
24
|
|
|
{ |
|
25
|
|
|
private const ASSOCIATIONS_WITH_AUTOLOAD = [ |
|
26
|
|
|
OneToOneAssociationField::class, |
|
27
|
|
|
ManyToOneAssociationField::class, |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
private ReflectionProvider $reflectionProvider; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(ReflectionProvider $reflectionProvider) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->reflectionProvider = $reflectionProvider; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getNodeType(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return New_::class; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param New_ $node |
|
44
|
|
|
* |
|
45
|
|
|
* @return array<array-key, RuleError|string> |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
public function processNode(Node $node, Scope $scope): array |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$scope->isInClass()) { |
|
50
|
|
|
return []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$definitionClassReflection = $scope->getClassReflection()->getNativeReflection(); |
|
54
|
|
|
$className = $definitionClassReflection->getName(); |
|
55
|
|
|
if (str_contains($className, 'Test')) { |
|
56
|
|
|
//if in a test namespace, don't care |
|
57
|
|
|
return []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!$node->class instanceof Node\Name) { |
|
|
|
|
|
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!\in_array($node->class->toString(), self::ASSOCIATIONS_WITH_AUTOLOAD, true)) { |
|
65
|
|
|
return []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$classReflection = $this->reflectionProvider |
|
69
|
|
|
->getClass($node->class->toString()) |
|
70
|
|
|
->getNativeReflection(); |
|
71
|
|
|
|
|
72
|
|
|
$construct = $classReflection->getMethod('__construct'); |
|
73
|
|
|
|
|
74
|
|
|
$autoloadParamPosition = null; |
|
75
|
|
|
$propertyNameParamPosition = null; |
|
76
|
|
|
foreach ($construct->getParameters() as $param) { |
|
77
|
|
|
if ($param->name === 'autoload') { |
|
78
|
|
|
$autoloadParamPosition = $param->getPosition(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($param->name === 'propertyName') { |
|
82
|
|
|
$propertyNameParamPosition = $param->getPosition(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($autoloadParamPosition === null || $propertyNameParamPosition === null) { |
|
87
|
|
|
//cannot find autoload or propertyName parameter |
|
88
|
|
|
return []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (!isset($node->getArgs()[$autoloadParamPosition])) { |
|
|
|
|
|
|
92
|
|
|
//autoload parameter not passed |
|
93
|
|
|
return []; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$autoloadValueExpr = $node->getArgs()[$autoloadParamPosition]->value; |
|
97
|
|
|
$propertyNameValueExpr = $node->getArgs()[$propertyNameParamPosition]->value; |
|
98
|
|
|
|
|
99
|
|
|
if ($scope->getType($autoloadValueExpr)->isTrue()->yes()) { |
|
100
|
|
|
$constant = $definitionClassReflection->getConstant('ENTITY_NAME'); |
|
101
|
|
|
|
|
102
|
|
|
if ($constant === false) { |
|
103
|
|
|
return []; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$propType = $scope->getType($propertyNameValueExpr); |
|
107
|
|
|
if (!$propType instanceof ConstantStringType) { |
|
108
|
|
|
return []; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return [ |
|
112
|
|
|
RuleErrorBuilder::message(sprintf( |
|
113
|
|
|
'%s.%s association has a configured autoload===true, this is forbidden for platform integrations', |
|
114
|
|
|
$constant, |
|
115
|
|
|
$propType->getValue() |
|
116
|
|
|
))->build(), |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths