Skip to content

Commit 5ac5e5f

Browse files
phpstan-botclaude
andcommitted
Move isExpressionDependentOnThis to ExpressionDependsOnThisHelper
Extract the static method into a standalone helper class to avoid ConstantConditionRuleHelper depending on ImpossibleCheckTypeHelper. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6b9535d commit 5ac5e5f

3 files changed

Lines changed: 43 additions & 29 deletions

File tree

src/Rules/Comparison/ConstantConditionRuleHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function shouldSkip(Scope $scope, Expr $expr): bool
6464

6565
if ($scope->isInTrait()) {
6666
foreach ($expr->getArgs() as $arg) {
67-
if (ImpossibleCheckTypeHelper::isExpressionDependentOnThis($arg->value)) {
67+
if (ExpressionDependsOnThisHelper::isExpressionDependentOnThis($arg->value)) {
6868
return true;
6969
}
7070
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Comparison;
4+
5+
use PhpParser\Node\Expr;
6+
7+
final class ExpressionDependsOnThisHelper
8+
{
9+
10+
public static function isExpressionDependentOnThis(Expr $expr): bool
11+
{
12+
if ($expr instanceof Expr\Variable && $expr->name === 'this') {
13+
return true;
14+
}
15+
16+
if ($expr instanceof Expr\PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
17+
return self::isExpressionDependentOnThis($expr->var);
18+
}
19+
20+
if ($expr instanceof Expr\MethodCall || $expr instanceof Expr\NullsafeMethodCall) {
21+
return self::isExpressionDependentOnThis($expr->var);
22+
}
23+
24+
if ($expr instanceof Expr\StaticPropertyFetch || $expr instanceof Expr\StaticCall) {
25+
if ($expr->class instanceof Expr) {
26+
return self::isExpressionDependentOnThis($expr->class);
27+
}
28+
29+
$className = $expr->class->toString();
30+
return in_array($className, ['self', 'static', 'parent'], true);
31+
}
32+
33+
return false;
34+
}
35+
36+
}

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ public function findSpecifiedType(
198198
}
199199
} elseif ($functionName === 'method_exists' && $argsCount >= 2) {
200200
$objectArg = $args[0]->value;
201+
202+
if ($scope->isInTrait() && ExpressionDependsOnThisHelper::isExpressionDependentOnThis($objectArg)) {
203+
return null;
204+
}
201205
$objectType = $this->treatPhpDocTypesAsCertain ? $scope->getType($objectArg) : $scope->getNativeType($objectArg);
202206

203207
if ($objectType instanceof ConstantStringType
@@ -310,7 +314,7 @@ public function findSpecifiedType(
310314
continue;
311315
}
312316

313-
if ($scope->isInTrait() && self::isExpressionDependentOnThis($sureType[0])) {
317+
if ($scope->isInTrait() && ExpressionDependsOnThisHelper::isExpressionDependentOnThis($sureType[0])) {
314318
$results[] = TrinaryLogic::createMaybe();
315319
continue;
316320
}
@@ -341,7 +345,7 @@ public function findSpecifiedType(
341345
continue;
342346
}
343347

344-
if ($scope->isInTrait() && self::isExpressionDependentOnThis($sureNotType[0])) {
348+
if ($scope->isInTrait() && ExpressionDependsOnThisHelper::isExpressionDependentOnThis($sureNotType[0])) {
345349
$results[] = TrinaryLogic::createMaybe();
346350
continue;
347351
}
@@ -391,32 +395,6 @@ private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool
391395
) && $scope->hasExpressionType($expr)->yes();
392396
}
393397

394-
public static function isExpressionDependentOnThis(Expr $expr): bool
395-
{
396-
if ($expr instanceof Expr\Variable && $expr->name === 'this') {
397-
return true;
398-
}
399-
400-
if ($expr instanceof Expr\PropertyFetch || $expr instanceof Expr\NullsafePropertyFetch) {
401-
return self::isExpressionDependentOnThis($expr->var);
402-
}
403-
404-
if ($expr instanceof Expr\MethodCall || $expr instanceof Expr\NullsafeMethodCall) {
405-
return self::isExpressionDependentOnThis($expr->var);
406-
}
407-
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-
417-
return false;
418-
}
419-
420398
/**
421399
* @param Node\Arg[] $args
422400
*/

0 commit comments

Comments
 (0)