Skip to content

Commit 75c93e1

Browse files
Fix
1 parent 497c2fd commit 75c93e1

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/Rules/Properties/AccessPropertiesCheck.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
130130
}
131131

132132
$has = $type->hasInstanceProperty($name);
133-
if ($has->maybe()) {
133+
$hasStatic = $type->hasStaticProperty($name);
134+
if ($has->maybe() && !$hasStatic->yes()) {
134135
if ($scope->isUndefinedExpressionAllowed($node)) {
135136
if (!$this->checkDynamicProperties) {
136137
return [];
@@ -202,7 +203,7 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
202203
}
203204
}
204205

205-
if ($type->hasStaticProperty($name)->yes()) {
206+
if ($hasStatic->yes()) {
206207
return [
207208
RuleErrorBuilder::message(sprintf(
208209
'Non-static access to static property %s::$%s.',

src/Type/ObjectType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ public function getUnresolvedInstancePropertyPrototype(string $propertyName, Cla
328328
throw new ClassNotFoundException($this->className);
329329
}
330330

331-
$property = RecursionGuard::run($this, static fn () => $nakedClassReflection->getInstanceProperty($propertyName, $scope));
331+
if (!$nakedClassReflection->hasInstanceProperty($propertyName)) {
332+
$property = new ErrorType();
333+
} else {
334+
$property = RecursionGuard::run($this, static fn () => $nakedClassReflection->getInstanceProperty($propertyName, $scope));
335+
}
336+
332337
if ($property instanceof ErrorType) {
333338
$property = new DummyPropertyReflection($propertyName);
334339

@@ -410,7 +415,12 @@ public function getUnresolvedStaticPropertyPrototype(string $propertyName, Class
410415
throw new ClassNotFoundException($this->className);
411416
}
412417

413-
$property = RecursionGuard::run($this, static fn () => $nakedClassReflection->getStaticProperty($propertyName));
418+
if (!$nakedClassReflection->hasStaticProperty($propertyName)) {
419+
$property = new ErrorType();
420+
} else {
421+
$property = RecursionGuard::run($this, static fn () => $nakedClassReflection->getStaticProperty($propertyName));
422+
}
423+
414424
if ($property instanceof ErrorType) {
415425
$property = new DummyPropertyReflection($propertyName);
416426

tests/PHPStan/Rules/Properties/AccessStaticPropertiesRuleTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,26 @@ public function testBug12775(): void
317317
$this->analyse([__DIR__ . '/data/bug-12775.php'], []);
318318
}
319319

320+
public function testBug8668Bis(): void
321+
{
322+
$this->analyse([__DIR__ . '/data/bug-8668-bis.php'], [
323+
[
324+
'Static access to instance property Bug8668Bis\Sample::$sample.',
325+
9,
326+
],
327+
[
328+
'Static access to instance property Bug8668Bis\Sample::$sample.',
329+
10,
330+
],
331+
[
332+
'Static access to instance property Bug8668Bis\Sample2::$sample.',
333+
20,
334+
],
335+
[
336+
'Static access to instance property Bug8668Bis\Sample2::$sample.',
337+
21,
338+
],
339+
]);
340+
}
341+
320342
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Bug8668Bis;
4+
5+
class Sample {
6+
private string $sample = 'abc';
7+
8+
public function test(): void {
9+
echo self::$sample;
10+
echo isset(self::$sample);
11+
12+
echo $this->sample; // ok
13+
}
14+
}
15+
16+
class Sample2 {
17+
private $sample = 'abc';
18+
19+
public function test(): void {
20+
echo self::$sample;
21+
echo isset(self::$sample);
22+
23+
echo $this->sample; // ok
24+
}
25+
}

0 commit comments

Comments
 (0)