Skip to content

Commit 88d2607

Browse files
committed
Detect accessing static property on a trait
1 parent e089754 commit 88d2607

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Rules/Properties/AccessStaticPropertiesRule.php

+11
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,18 @@ public function processNode(Node $node, Scope $scope): array
117117
} else {
118118
$messages = $this->classCaseSensitivityCheck->checkClassNames([new ClassNameNodePair($class, $node->class)]);
119119
}
120+
121+
$classReflection = $this->reflectionProvider->getClass($class);
120122
$className = $this->reflectionProvider->getClass($class)->getName();
123+
if ($classReflection->isTrait()) {
124+
return [
125+
RuleErrorBuilder::message(sprintf(
126+
'Access to static property $%s on trait %s.',
127+
$name,
128+
$className
129+
))->build(),
130+
];
131+
}
121132
}
122133

123134
$classType = new ObjectType($className);

tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ public function testAccessStaticProperties(): void
167167
'Access to an undefined static property AccessInIsset::$foo.',
168168
185,
169169
],
170+
[
171+
'Access to static property $foo on trait TraitWithStaticProperty.',
172+
204,
173+
],
174+
[
175+
'Access to static property $foo on an unknown class TraitWithStaticProperty.',
176+
209,
177+
],
170178
]);
171179
}
172180

tests/PHPStan/Rules/Properties/data/access-static-properties.php

+22
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,25 @@ public function doBar()
188188
}
189189

190190
}
191+
192+
trait TraitWithStaticProperty
193+
{
194+
195+
public static $foo;
196+
197+
}
198+
199+
class MethodAccessingTraitProperty
200+
{
201+
202+
public function doFoo(): void
203+
{
204+
echo TraitWithStaticProperty::$foo;
205+
}
206+
207+
public function doBar(TraitWithStaticProperty $a): void
208+
{
209+
echo $a::$foo;
210+
}
211+
212+
}

0 commit comments

Comments
 (0)