|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\DevOps\StaticAnalyze\PHPStan\Rules\Deprecation; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
7
|
|
|
use PHPStan\Analyser\Scope; |
|
|
|
|
|
|
8
|
|
|
use PHPStan\Reflection\ClassReflection; |
|
|
|
|
|
|
9
|
|
|
use PHPStan\Rules\Rule; |
|
|
|
|
|
|
10
|
|
|
use PHPStan\Rules\RuleError; |
|
|
|
|
|
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @implements Rule<ClassMethod> |
|
15
|
|
|
* |
|
16
|
|
|
* @deprecated tag:v6.5.0 - reason:becomes-internal - will be internal in 6.5.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class DeprecatedMethodsThrowDeprecationRule implements Rule |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* There are some exceptions to this rule, where deprecated methods should not throw a deprecation notice. |
|
22
|
|
|
* This is mainly the reason if the deprecated code is still called from inside the core due to BC reasons. |
|
23
|
|
|
*/ |
|
24
|
|
|
private const RULE_EXCEPTIONS = [ |
|
25
|
|
|
// Subscribers still need to be called for BC reasons, therefore they do not trigger deprecations. |
|
26
|
|
|
'reason:remove-subscriber', |
|
27
|
|
|
// Decorators still need to be called for BC reasons, therefore they do not trigger deprecations. |
|
28
|
|
|
'reason:remove-decorator', |
|
29
|
|
|
// Entities still need to be present in the DI container, therefore they do not trigger deprecations. |
|
30
|
|
|
'reason:remove-entity', |
|
31
|
|
|
// Classes that will be internal are still called from inside the core, therefore they do not trigger deprecations. |
|
32
|
|
|
'reason:becomes-internal', |
|
33
|
|
|
// Classes that will be final, can only be changed with the next major |
|
34
|
|
|
'reason:becomes-final', |
|
35
|
|
|
// If the return type change, the functionality itself is not deprecated, therefore they do not trigger deprecations. |
|
36
|
|
|
'reason:return-type-change', |
|
37
|
|
|
// If there will be in the class hierarchy of a class we mark the whole class as deprecated, but the functionality itself is not deprecated, therefore they do not trigger deprecations. |
|
38
|
|
|
'reason:class-hierarchy-change', |
|
39
|
|
|
// If we change the visibility of a method we can't know from where it was called and whether the call will be valid in the future, therefore they do not trigger deprecations. |
|
40
|
|
|
'reason:visibility-change', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
public function getNodeType(): string |
|
44
|
|
|
{ |
|
45
|
|
|
return ClassMethod::class; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ClassMethod $node |
|
50
|
|
|
* |
|
51
|
|
|
* @return array<array-key, RuleError|string> |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
public function processNode(Node $node, Scope $scope): array |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$scope->isInClass()) { |
|
56
|
|
|
// skip |
|
57
|
|
|
return []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$class = $scope->getClassReflection(); |
|
61
|
|
|
|
|
62
|
|
|
if ($class === null || $class->isInterface() || $this->isTestClass($class)) { |
|
63
|
|
|
return []; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!($node->isPublic() || $node->isProtected()) || $node->isAbstract() || $node->isMagic()) { |
|
|
|
|
|
|
67
|
|
|
return []; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$methodContent = $this->getMethodContent($node, $scope, $class); |
|
71
|
|
|
$method = $class->getMethod($node->name->name, $scope); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$classDeprecation = $class->getDeprecatedDescription(); |
|
74
|
|
|
if ($classDeprecation && !$this->handlesDeprecationCorrectly($classDeprecation, $methodContent)) { |
|
75
|
|
|
return [ |
|
76
|
|
|
\sprintf( |
|
77
|
|
|
'Class "%s" is marked as deprecated, but method "%s" does not call "Feature::triggerDeprecationOrThrow". All public methods of deprecated classes need to trigger a deprecation warning.', |
|
78
|
|
|
$class->getName(), |
|
79
|
|
|
$method->getName() |
|
80
|
|
|
), |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$methodDeprecation = $method->getDeprecatedDescription() ?? ''; |
|
85
|
|
|
|
|
86
|
|
|
// by default deprecations from parent methods are also available on all implementing methods |
|
87
|
|
|
// we will copy the deprecation to the implementing method, if they also have an affect there |
|
88
|
|
|
$deprecationOfParentMethod = !str_contains($method->getDocComment() ?? '', $methodDeprecation) && !str_contains($method->getDocComment() ?? '', 'inheritdoc'); |
|
89
|
|
|
|
|
90
|
|
|
if (!$deprecationOfParentMethod && $methodDeprecation && !$this->handlesDeprecationCorrectly($methodDeprecation, $methodContent)) { |
|
91
|
|
|
return [ |
|
92
|
|
|
\sprintf( |
|
93
|
|
|
'Method "%s" of class "%s" is marked as deprecated, but does not call "Feature::triggerDeprecationOrThrow". All deprecated methods need to trigger a deprecation warning.', |
|
94
|
|
|
$method->getName(), |
|
95
|
|
|
$class->getName() |
|
96
|
|
|
), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return []; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function getMethodContent(Node $node, Scope $scope, ClassReflection $class): string |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var string $filename */ |
|
106
|
|
|
$filename = $class->getFileName(); |
|
107
|
|
|
|
|
108
|
|
|
$trait = $scope->getTraitReflection(); |
|
109
|
|
|
|
|
110
|
|
|
if ($trait) { |
|
111
|
|
|
/** @var string $filename */ |
|
112
|
|
|
$filename = $trait->getFileName(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$file = new \SplFileObject($filename); |
|
116
|
|
|
$file->seek($node->getStartLine() - 1); |
|
117
|
|
|
|
|
118
|
|
|
$content = ''; |
|
119
|
|
|
for ($i = 0; $i <= ($node->getEndLine() - $node->getStartLine()); ++$i) { |
|
120
|
|
|
$content .= $file->current(); |
|
121
|
|
|
$file->next(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $content; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function handlesDeprecationCorrectly(string $deprecation, string $method): bool |
|
128
|
|
|
{ |
|
129
|
|
|
foreach (self::RULE_EXCEPTIONS as $exception) { |
|
130
|
|
|
if (\str_contains($deprecation, $exception)) { |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return \str_contains($method, 'Feature::triggerDeprecationOrThrow('); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function isTestClass(ClassReflection $class): bool |
|
139
|
|
|
{ |
|
140
|
|
|
$namespace = $class->getName(); |
|
141
|
|
|
|
|
142
|
|
|
if (\str_contains($namespace, '\\Test\\')) { |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (\str_contains($namespace, '\\Tests\\')) { |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if ($class->getParentClass() === null) { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $class->getParentClass()->getName() === TestCase::class; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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