Skip to content

Commit af1247c

Browse files
phpstan-botclaude
andcommitted
Handle static access, nullsafe access, and $this:: in trait $this check
Extend isExpressionDependentOnThis() to also recognize: - NullsafePropertyFetch ($this?->foo) - NullsafeMethodCall ($this?->method()) - StaticPropertyFetch (self::$foo, static::$foo, $this::$foo) - StaticCall (self::method(), static::method(), $this::method()) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4fc6007 commit af1247c

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,23 @@ private static function isExpressionDependentOnThis(Expr $expr): bool
397397
return true;
398398
}
399399

400-
if ($expr instanceof Expr\PropertyFetch) {
400+
if ($expr instanceof Expr\PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
401401
return self::isExpressionDependentOnThis($expr->var);
402402
}
403403

404-
if ($expr instanceof Expr\MethodCall) {
404+
if ($expr instanceof Expr\MethodCall || $expr instanceof Expr\NullsafeMethodCall) {
405405
return self::isExpressionDependentOnThis($expr->var);
406406
}
407407

408+
if ($expr instanceof Expr\StaticPropertyFetch || $expr instanceof Expr\StaticCall) {
409+
if ($expr->class instanceof Expr) {
410+
return self::isExpressionDependentOnThis($expr->class);
411+
}
412+
413+
$className = $expr->class->toString();
414+
return in_array($className, ['self', 'static', 'parent'], true);
415+
}
416+
408417
return false;
409418
}
410419

tests/PHPStan/Rules/Comparison/data/bug-13023.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,63 @@ public function getRandom(): int
5050
return $value;
5151
}
5252
}
53+
54+
class SomeClass5
55+
{
56+
use MyTrait3;
57+
58+
public static string $bar = 'bar';
59+
}
60+
61+
class SomeClass6
62+
{
63+
use MyTrait3;
64+
65+
public static int $bar = 1;
66+
}
67+
68+
trait MyTrait3
69+
{
70+
public function getRandom(): int
71+
{
72+
$value = random_int(1, 100);
73+
if (\is_int(self::$bar)) {
74+
return $value * $value;
75+
}
76+
if (\is_int(static::$bar)) {
77+
return $value * $value;
78+
}
79+
if (\is_int($this::$bar)) {
80+
return $value * $value;
81+
}
82+
83+
return $value;
84+
}
85+
}
86+
87+
class SomeClass7
88+
{
89+
use MyTrait4;
90+
91+
public ?string $baz = 'baz';
92+
}
93+
94+
class SomeClass8
95+
{
96+
use MyTrait4;
97+
98+
public ?int $baz = 1;
99+
}
100+
101+
trait MyTrait4
102+
{
103+
public function getRandom(): int
104+
{
105+
$value = random_int(1, 100);
106+
if (\is_int($this?->baz)) {
107+
return $value * $value;
108+
}
109+
110+
return $value;
111+
}
112+
}

0 commit comments

Comments
 (0)