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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Type/Php/MethodExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
Expand All @@ -20,6 +22,7 @@
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\UnionType;
use function count;
use function ltrim;

#[AutowiredService]
final class MethodExistsTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
Expand Down Expand Up @@ -64,7 +67,7 @@ public function specifyTypes(
$objectType = $scope->getType($args[0]->value);
if ($objectType->isString()->yes()) {
if ($objectType->isClassString()->yes()) {
return $this->typeSpecifier->create(
$result = $this->typeSpecifier->create(
$args[0]->value,
new IntersectionType([
$objectType,
Expand All @@ -73,6 +76,31 @@ public function specifyTypes(
$context,
$scope,
);

if (!$args[0]->value instanceof ClassConstFetch) {
foreach ($objectType->getConstantStrings() as $constantString) {
$className = ltrim($constantString->getValue(), '\\');
if ($className === '') {
continue;
}
$classConstFetch = new Expr\ClassConstFetch(
new FullyQualified($className),
'class',
);
$classConstFetchType = $scope->getType($classConstFetch);
$result = $result->unionWith($this->typeSpecifier->create(
$classConstFetch,
new IntersectionType([
$classConstFetchType,
new HasMethodType($methodNameType->getValue()),
]),
$context,
$scope,
));
}
}

return $result;
}

return new SpecifiedTypes([], []);
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9592.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug9592Nsrt;

use function PHPStan\Testing\assertType;

class rex_response {}

class HelloWorld
{
public function sayHello(): void
{
if (!method_exists('Bug9592Nsrt\rex_response', 'getNonce')) {
return;
}
// The ClassConstFetch expression gets narrowed via the fix
assertType("'Bug9592Nsrt\\\\rex_response'&hasMethod(getNonce)", rex_response::class);
}
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@ public function testHasMethodStaticCall(): void
]);
}

public function testBug9592(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-9592.php'], []);
}

public function testBug1267(): void
{
$this->checkThisOnly = false;
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9592.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug9592;

class rex_response {}

class HelloWorld
{
public function sayHello(): void
{
// method_exists with string literal, then static call
$nonce = method_exists('Bug9592\rex_response', 'getNonce') ? rex_response::getNonce() : '';
}

public function sayHello2(): void
{
if (!method_exists('Bug9592\rex_response', 'getNonce')) {
return;
}

rex_response::getNonce();
}

public function sayHello3(): void
{
if (method_exists('Bug9592\rex_response', 'getNonce')) {
rex_response::getNonce();
}
}
}
Loading