-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisallowOverridingOfConfigurationPropertyTypeRule.php
134 lines (114 loc) · 3.9 KB
/
DisallowOverridingOfConfigurationPropertyTypeRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace Cambis\Silverstan\Rule\ClassPropertyNode;
use Cambis\Silverstan\Contract\SilverstanRuleInterface;
use Cambis\Silverstan\ReflectionAnalyser\ClassReflectionAnalyser;
use Cambis\Silverstan\ReflectionAnalyser\PropertyReflectionAnalyser;
use Cambis\Silverstan\ReflectionResolver\ReflectionResolver;
use Override;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use function sprintf;
/**
* @implements SilverstanRuleInterface<ClassPropertyNode>
* @see \Cambis\Silverstan\Tests\Rule\ClassPropertyNode\DisallowOverridingOfConfigurationPropertyTypeRuleTest
*/
final readonly class DisallowOverridingOfConfigurationPropertyTypeRule implements SilverstanRuleInterface
{
public function __construct(
private ClassReflectionAnalyser $classReflectionAnalyser,
private PropertyReflectionAnalyser $propertyReflectionAnalyser,
private ReflectionResolver $reflectionResolver
) {
}
#[Override]
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Disallow overriding types of configuration properties.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
namespace App\Model;
class Foo extends \SilverStripe\ORM\DataObject
{
private static string $foo = 'foo';
}
final class Bar extends Foo
{
private static string|bool $foo = false;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
namespace App\Model;
class Foo extends \SilverStripe\ORM\DataObject
{
private static string $foo = 'foo';
}
final class Bar extends Foo
{
private static string $foo = 'bar';
}
CODE_SAMPLE
,
[
'enabled' => true,
]
)],
);
}
#[Override]
public function getNodeType(): string
{
return ClassPropertyNode::class;
}
/**
* @param ClassPropertyNode $node
*/
#[Override]
public function processNode(Node $node, Scope $scope): array
{
if (!$this->propertyReflectionAnalyser->isConfigurationProperty($node)) {
return [];
}
$classReflection = $node->getClassReflection();
if (!$classReflection->hasNativeProperty($node->getName())) {
return [];
}
if (!$this->classReflectionAnalyser->isConfigurable($classReflection)) {
return [];
}
$prototype = $this->reflectionResolver->resolveConfigurationPropertyReflection($classReflection->getParentClass(), $node->getName());
if (!$prototype instanceof PropertyReflection) {
return [];
}
$nativeType = $classReflection->getNativeProperty($node->getName())->getReadableType();
$type = $node->getPhpDocType() ?? $nativeType;
$prototypeType = $prototype->getReadableType();
if ($prototypeType->accepts($type, true)->yes()) {
return [];
}
return [
RuleErrorBuilder::message(
sprintf(
'Type %s of configuration property %s::$%s is not the same as type %s of overridden configuration property %s::$%s.',
$type->describe(VerbosityLevel::typeOnly()),
$classReflection->getDisplayName(),
$node->getName(),
$prototypeType->describe(VerbosityLevel::typeOnly()),
$prototype->getDeclaringClass()->getDisplayName(),
$node->getName()
)
)
->identifier('silverstan.configurationProperty')
->build(),
];
}
}