Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Type/Php/MethodExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;

class MethodExistsTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
Expand Down Expand Up @@ -44,6 +46,13 @@ public function specifyTypes(
TypeSpecifierContext $context
): SpecifiedTypes
{
$objectType = $scope->getType($node->args[0]->value);
if (!$objectType instanceof ObjectType) {
if ((new StringType())->isSuperTypeOf($objectType)->yes()) {
return new SpecifiedTypes([], []);
}
}

$methodNameType = $scope->getType($node->args[1]->value);
if (!$methodNameType instanceof ConstantStringType) {
return new SpecifiedTypes([], []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ public function testImpossibleCheckTypeFunctionCall(): void
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'testWithStringFirst…\' will always evaluate to true.',
585,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'undefinedMethod\' will always evaluate to false.',
588,
],
[
'Call to function method_exists() with \'UndefinedClass\' and string will always evaluate to false.',
594,
Expand All @@ -151,43 +147,43 @@ public function testImpossibleCheckTypeFunctionCall(): void
],
[
'Call to function method_exists() with CheckTypeFunctionCall\MethodExists and \'testWithNewObjectIn…\' will always evaluate to true.',
606,
609,
],
[
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'method\' will always evaluate to true.',
621,
624,
],
[
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'someAnother\' will always evaluate to true.',
624,
627,
],
[
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'unknown\' will always evaluate to false.',
627,
630,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'method\' will always evaluate to true.',
630,
633,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'someAnother\' will always evaluate to true.',
633,
636,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'unknown\' will always evaluate to false.',
636,
639,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'method\' will always evaluate to true.',
639,
642,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'someAnother\' will always evaluate to true.',
642,
645,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'unknown\' will always evaluate to false.',
645,
648,
],
]
);
Expand Down Expand Up @@ -259,10 +255,6 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
'Call to function is_callable() with mixed will always evaluate to false.',
571,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'undefinedMethod\' will always evaluate to false.',
588,
],
[
'Call to function method_exists() with \'UndefinedClass\' and string will always evaluate to false.',
594,
Expand All @@ -273,15 +265,15 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
],
[
'Call to function method_exists() with $this(CheckTypeFunctionCall\MethodExistsWithTrait) and \'unknown\' will always evaluate to false.',
627,
630,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'unknown\' will always evaluate to false.',
636,
639,
],
[
'Call to function method_exists() with \'CheckTypeFunctionCa…\' and \'unknown\' will always evaluate to false.',
645,
648,
],
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ public function testWithStringFirstArgument(): void

if (method_exists('UndefinedClass', 'test')) {
}

if (method_exists($string, 'test')) {
}
}

public function testWithNewObjectInFirstArgument(): void
Expand Down