Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support 'HasMethodType' on StaticCall #1969

Merged
merged 4 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public function check(

$classType = $scope->resolveTypeByName($class);
} else {
$classStringType = $scope->getType(new Expr\ClassConstFetch($class, 'class'));
if ($classStringType->hasMethod($methodName)->yes()) {
return [[], null];
}

if (!$this->reflectionProvider->hasClass($className)) {
if ($scope->isInClassExists($className)) {
return [[], null];
Expand Down
8 changes: 8 additions & 0 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function getValue(): string
return $this->value;
}

public function hasMethod(string $methodName): TrinaryLogic
{
if ($this->isClassString()) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createNo();
}

public function isClassString(): bool
{
if ($this->isClassString) {
Expand Down
24 changes: 17 additions & 7 deletions src/Type/Php/MethodExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\UnionType;
use function count;
Expand Down Expand Up @@ -47,15 +46,26 @@ public function specifyTypes(
TypeSpecifierContext $context,
): SpecifiedTypes
{
$methodNameType = $scope->getType($node->getArgs()[1]->value);
if (!$methodNameType instanceof ConstantStringType) {
return new SpecifiedTypes([], []);
}

$objectType = $scope->getType($node->getArgs()[0]->value);
if (!$objectType instanceof ObjectType) {
if ($objectType->isString()->yes()) {
return new SpecifiedTypes([], []);
if ($objectType->isString()->yes()) {
if ($objectType instanceof ConstantStringType && $objectType->isClassString()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add isClassString on Type as a followup?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have ConstantStringType->isClassString(): bool,

public function isClassString(): bool
{
if ($this->isClassString) {
return true;
}
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
return $reflectionProvider->hasClass($this->value);
}

therefore we need a different name for this new method... any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isClassStringType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And make it nicer for 2.0

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
new IntersectionType([
$objectType,
new HasMethodType($methodNameType->getValue()),
]),
$context,
false,
$scope,
);
}
}

$methodNameType = $scope->getType($node->getArgs()[1]->value);
if (!$methodNameType instanceof ConstantStringType) {
return new SpecifiedTypes([], []);
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8272.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-8277.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/strtr.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/static-has-method.php');
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/data/static-has-method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace StaticHasMethod;

use function PHPStan\Testing\assertType;

class rex_var {}

class HelloWorld
{
public function sayHello(): void
{
if (!method_exists(rex_var::class, 'varsIterator')) {
return;
}
assertType("'StaticHasMethod\\\\rex_var'&hasMethod(varsIterator)", rex_var::class);

$it = rex_var::varsIterator();
}
}

16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,20 @@ public function testBug7489(): void
$this->analyse([__DIR__ . '/data/bug-7489.php'], []);
}

public function testHasMethodStaticCall(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/static-has-method.php'], [
[
'Call to an undefined static method StaticHasMethodCall\rex_var::doesNotExist().',
38,
],
[
'Call to an undefined static method StaticHasMethodCall\rex_var::doesNotExist().',
48,
],
]);
}

}
51 changes: 51 additions & 0 deletions tests/PHPStan/Rules/Methods/data/static-has-method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace StaticHasMethodCall;

class rex_var {
public static function aMethod() {}
}

class HelloWorld
{
public function sayHello(): void
{
if (!method_exists(rex_var::class, 'doesNotExist')) {
return;
}

// should not error
$it = rex_var::doesNotExist();
}

public function sayHello2(): void
{
if (!method_exists(rex_var::class, 'doesNotExist')) {
return;
}

// should not error
$it = rex_var::aMethod();
}

public function sayHello3(): void
{
if (!method_exists(rex_var::class, 'anotherNotExistingMethod')) {
return;
}

// should error
$it = rex_var::doesNotExist();
}

public function sayHello4(): void
{
if (!method_exists(notExistentClass::class, 'doesNotExist')) {
return;
}

// should error
$it = rex_var::doesNotExist();
}
}