Skip to content

Commit 38e8ef2

Browse files
phpstan-botclaude
andcommitted
Simplify method_exists trait context handling
Replace the complex method_exists special case (checking if each method name is defined in the trait reflection) with a simple early return when the object argument depends on the trait context. This trades off not reporting method_exists($this, 'traitDefinedMethod') as always-true in trait context, but the check is harmless and this removes 17 lines of logic. Also make isExpressionDependentOnThis private since it's only used internally. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1c6d393 commit 38e8ef2

File tree

2 files changed

+3
-20
lines changed

2 files changed

+3
-20
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,9 @@ public function findSpecifiedType(
204204
$ignoreTraitContext
205205
&& $this->isExpressionDependentOnTraitContext($scope, $objectArg)
206206
) {
207-
$traitReflection = $scope->getTraitReflection();
208-
if ($traitReflection === null) {
209-
return null;
210-
}
211-
$methodArgValue = $args[1]->value;
212-
$methodArgType = $this->treatPhpDocTypesAsCertain ? $scope->getType($methodArgValue) : $scope->getNativeType($methodArgValue);
213-
$constantMethodNames = $methodArgType->getConstantStrings();
214-
if (count($constantMethodNames) === 0) {
215-
return null;
216-
}
217-
foreach ($constantMethodNames as $constantMethodName) {
218-
if (!$traitReflection->hasNativeMethod($constantMethodName->getValue())) {
219-
return null;
220-
}
221-
}
207+
return null;
222208
}
209+
223210
$objectType = $this->treatPhpDocTypesAsCertain ? $scope->getType($objectArg) : $scope->getNativeType($objectArg);
224211

225212
if ($objectType instanceof ConstantStringType
@@ -419,7 +406,7 @@ private function isExpressionDependentOnTraitContext(Scope $scope, Expr $expr):
419406
return false;
420407
}
421408

422-
public static function isExpressionDependentOnThis(Expr $expr): bool
409+
private static function isExpressionDependentOnThis(Expr $expr): bool
423410
{
424411
if ($expr instanceof Expr\Variable && $expr->name === 'this') {
425412
return true;

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@ public function testImpossibleCheckTypeFunctionCall(): void
183183
'Call to function method_exists() with CheckTypeFunctionCall\MethodExists and \'testWithNewObjectIn…\' will always evaluate to true.',
184184
635,
185185
],
186-
[
187-
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'method\' will always evaluate to true.',
188-
650,
189-
],
190186
[
191187
'Call to function method_exists() with \'CheckTypeFunctionCall\\\\MethodExistsWithTrait\' and \'method\' will always evaluate to true.',
192188
659,

0 commit comments

Comments
 (0)