8000 Restricted usage extensions - do not report false positives for Symfo… · phpstan/phpstan-src@4a907f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a907f1

Browse files
committed
Restricted usage extensions - do not report false positives for Symfony polyfills
1 parent 955a05d commit 4a907f1

15 files changed

+552
-1
lines changed

src/Rules/RestrictedUsage/RestrictedClassConstantUsageRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public function processNode(Node $node, Scope $scope) 10000 : array
8686
continue;
8787
}
8888

89+
if ($classReflection->getName() !== $constantReflection->getDeclaringClass()->getName()) {
90+
$rewrittenConstantReflection = new RewrittenDeclaringClassClassConstantReflection($classReflection, $constantReflection);
91+
$rewrittenRestrictedUsage = $extension->isRestrictedClassConstantUsage($rewrittenConstantReflection, $scope);
92+
if ($rewrittenRestrictedUsage === null) {
93+
continue;
94+
}
95+
}
96+
8997
$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
9098
->identifier($restrictedUsage->identifier)
9199
->build();

src/Rules/RestrictedUsage/RestrictedStaticMethodCallableUsageRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public function processNode(Node $node, Scope $scope): array
8787
continue;
8888
}
8989

90+
if ($classReflection->getName() !== $methodReflection->getDeclaringClass()->getName()) {
91+
$rewrittenMethodReflection = new RewrittenDeclaringClassMethodReflection($classReflection, $methodReflection);
92+
$rewrittenRestrictedUsage = $extension->isRestrictedMethodUsage($rewrittenMethodReflection, $scope);
93+
if ($rewrittenRestrictedUsage === null) {
94+
continue;
95+
}
96+
}
97+
9098
$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
9199
->identifier($restrictedUsage->identifier)
92100
->build();

src/Rules/RestrictedUsage/RestrictedStaticMethodUsageRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public function processNode(Node $node, Scope $scope): array
8686
continue;
8787
}
8888

89+
if ($classReflection->getName() !== $methodReflection->getDeclaringClass()->getName()) {
90+
$rewrittenMethodReflection = new RewrittenDeclaringClassMethodReflection($classReflection, $methodReflection);
91+
$rewrittenRestrictedUsage = $extension->isRestrictedMethodUsage($rewrittenMethodReflection, $scope);
92+
if ($rewrittenRestrictedUsage === null) {
93+
continue;
94+
}
95+
}
96+
8997
$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
9098
->identifier($restrictedUsage->identifier)
9199
->build();

src/Rules/RestrictedUsage/RestrictedStaticPropertyUsageRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public function processNode(Node $node, Scope $scope): array
8686
continue;
8787
}
8888

89+
if ($classReflection->getName() !== $propertyReflection->getDeclaringClass()->getName()) {
90+
$rewrittenPropertyReflection = new RewrittenDeclaringClassPropertyReflection($classReflection, $propertyReflection);
91+
$rewrittenRestrictedUsage = $extension->isRestrictedPropertyUsage($rewrittenPropertyReflection, $scope);
92+
if ($rewrittenRestrictedUsage === null) {
93+
continue;
94+
}
95+
}
96+
8997
$errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
9098
->identifier($restrictedUsage->identifier)
9199
->build();
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RestrictedUsage;
4+
5+
use PhpParser\Node\Expr;
6+
use PHPStan\Reflection\ClassConstantReflection;
7+
use PHPStan\Reflection\ClassReflection;
8+
use PHPStan\TrinaryLogic;
9+
use PHPStan\Type\Type;
10+
11+
final class RewrittenDeclaringClassClassConstantReflection implements ClassConstantReflection
12+
{
13+
14+
public function __construct(
15+
private ClassReflection $declaringClass,
16+
private ClassConstantReflection $constantReflection,
17+
)
18+
{
19+
}
20+
21+
public function getValueExpr(): Expr
22+
{
23+
return $this->constantReflection->getValueExpr();
24+
}
25+
26+
public function isFinal(): bool
27+
{
28+
return $this->constantReflection->isFinal();
29+
}
30+
31+
public function hasPhpDocType(): bool
32+
{
33+
return $this->constantReflection->hasPhpDocType();
34+
}
35+
36+
public function getPhpDocType(): ?Type
37+
{
38+
return $this->constantReflection->getPhpDocType();
39+
}
40+
41+
public function hasNativeType(): bool
42+
{
43+
return $this->constantReflection->hasNativeType();
44+
}
45+
46+
public function getNativeType(): ?Type
47+
{
48+
return $this->constantReflection->getNativeType();
49+
}
50+
51+
public function getAttributes(): array
52+
{
53+
return $this->constantReflection->getAttributes();
54+
}
55+
56+
public function getDeclaringClass(): ClassReflection
57+
{
58+
return $this->declaringClass;
59+
}
60+
61+
public function isStatic(): bool
62+
{
63+
return $this->constantReflection->isStatic();
64+
}
65+
66+
public function isPrivate(): bool
67+
{
68+
return $this->constantReflection->isPrivate();
69+
}
70+
71+
public function isPublic(): bool
72+
{
73+
return $this->constantReflection->isPublic();
74+
}
75+
76+
public function getDocComment(): ?string
77+
{
78+
return $this->constantReflection->getDocComment();
79+
}
80+
81+
public function getName(): string
82+
{
83+
return $this->constantReflection->getName();
84+
}
85+
86+
public function getValueType(): Type
87+
{
88+
return $this->constantReflection->getValueType();
89+
}
90+
91+
public function isDeprecated(): TrinaryLogic
92+
{
93+
return $this->constantReflection->isDeprecated();
94+
}
95+
96+
public function getDeprecatedDescription(): ?string
97+
{
98+
return $this->constantReflection->getDeprecatedDescription();
99+
}
100+
101+
public function isInternal(): TrinaryLogic
102+
{
103+
return $this->constantReflection->isInternal();
104+
}
105+
106+
public function getFileName(): ?string
107+
{
108+
return $this->constantReflection->getFileName();
109+
}
110+
111+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\RestrictedUsage;
4+
5+
use PHPStan\Reflection\Assertions;
6+
use PHPStan\Reflection\ClassMemberReflection;
7+
use PHPStan\Reflection\ClassReflection;
8+
use PHPStan\Reflection\ExtendedMethodReflection;
9+
use PHPStan\Reflection\ExtendedParametersAcceptor;
10+
use PHPStan\TrinaryLogic;
11+
use PHPStan\Type\Type;
12+
13+
final class RewrittenDeclaringClassMethodReflection implements ExtendedMethodReflection
14+
{
15+
16+
public function __construct(
17+
private ClassReflection $declaringClass,
18+
private ExtendedMethodReflection $methodReflection,
19+
)
20+
{
21+
}
22+
23+
public function getDeclaringClass(): ClassReflection
24+
{
25+
return $this->declaringClass;
26+
}
27+
28+
public function isStatic(): bool
29+
{
30+
return $this->methodReflection->isStatic();
31+
}
32+
33+
public function isPrivate(): bool
34+
{
35+
return $this->methodReflection->isPrivate();
36+
}
37+
38+
public function isPublic(): bool
39+
{
40+
return $this->methodReflection->isPublic();
41+
}
42+
43+
public function getDocComment(): ?string
44+
{
45+
return $this->methodReflection->getDocComment();
46+
}
47+
48+
public function getVariants(): array
49+
{
50+
return $this->methodReflection->getVariants();
51+
}
52+
53+
public function getOnlyVariant(): ExtendedParametersAcceptor
54+
{
55+
return $this->methodReflection->getOnlyVariant();
56+
}
57+
58+
public function getNamedArgumentsVariants(): ?array
59+
{
60+
return $this->methodReflection->getNamedArgumentsVariants();
61+
}
62+
63+
public function acceptsNamedArguments(): TrinaryLogic
64+
{
65+
return $this->methodReflection->acceptsNamedArguments();
66+
}
67+
68+
public function getAsserts(): Assertions
69+
{
70+
return $this->methodReflection->getAsserts();
71+
}
72+
73+
public function getSelfOutType(): ?Type
74+
{
75+
return $this->methodReflection->getSelfOutType();
76+
}
77+
78+
public function returnsByReference(): TrinaryLogic
79+
{
80+
return $this->methodReflection->returnsByReference();
81+
}
82+
83+
public function isFinalByKeyword(): TrinaryLogic
84+
{
85+
return $this->methodReflection->isFinalByKeyword();
86+
}
87+
88+
public function isAbstract(): TrinaryLogic|bool
89+
{
90+
return $this->methodReflection->isAbstract();
91+
}
92+
93+
public function isBuiltin(): TrinaryLogic|bool
94+
{
95+
return $this->methodReflection->isBuiltin();
96+
}
97+
98+
public function isPure(): TrinaryLogic
99+
{
100+
return $this->methodReflection->isPure();
101+
}
102+
103+
public function getAttributes(): array
104+
{
105+
return $this->methodReflection->getAttributes();
106+
}
107+
108+
public function getName(): string
109+
{
110+
return $this->methodReflection->getName();
111+
}
112+
113+
public function getPrototype(): ClassMemberReflection
114+
{
115+
return $this->methodReflection->getPrototype();
116+
}
117+
118+
public function isDeprecated(): TrinaryLogic
119+
{
120+
return $this< D304 /span>->methodReflection->isDeprecated();
121+
}
122+
123+
public function getDeprecatedDescription(): ?string
124+
{
125+
return $this->methodReflection->getDeprecatedDescription();
126+
}
127+
128+
public function isFinal(): TrinaryLogic
129+
{
130+
return $this->methodReflection->isFinal();
131+
}
132+
133+
public function isInternal(): TrinaryLogic
134+
{
135+
return $this->methodReflection->isInternal();
136+
}
137+
138+
public function getThrowType(): ?Type
139+
{
140+
return $this->methodReflection->getThrowType();
141+
}
142+
143+
public function hasSideEffects(): TrinaryLogic
144+
{
145+
return $this->methodReflection->hasSideEffects();
146+
}
147+
148+
}

0 commit comments

Comments
 (0)
0