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
13 changes: 13 additions & 0 deletions src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -200,6 +201,18 @@ public function check(
if (!$classType->isObject()->yes()) {
return [[], null];
}
} elseif ($classType->isClassString()->yes()) {
$nativeMethodTypes = [];
foreach ($classType->getClassStringObjectType()->getObjectClassReflections() as $classReflection) {
if ($classReflection->hasNativeMethod($methodName)) {
$nativeMethodTypes[] = new ObjectType($classReflection->getName());
}
}
if ($nativeMethodTypes !== []) {
$classType = TypeCombinator::union(...$nativeMethodTypes);
} else {
return [[], null];
}
} elseif ($classType->isString()->yes()) {
return [[], null];
}
Expand Down
13 changes: 12 additions & 1 deletion src/Type/Php/MethodExistsTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,21 @@ public function specifyTypes(
$objectOrStringType = $scope->getType($args[0]->value);
if ($objectOrStringType->isString()->yes()) {
if ($objectOrStringType->isClassString()->yes()) {
foreach ($objectOrStringType->getClassStringObjectType()->getObjectClassReflections() as $classReflection) {
$allNative = true;
$classReflections = $objectOrStringType->getClassStringObjectType()->getObjectClassReflections();
foreach ($classReflections as $classReflection) {
if ($classReflection->hasMethod($methodNameType->getValue()) && !$classReflection->hasNativeMethod($methodNameType->getValue())) {
return $this->createFuncCallSpec($node, $context, $scope);
}
if ($classReflection->hasNativeMethod($methodNameType->getValue())) {
continue;
}

$allNative = false;
}

if ($allNative && $classReflections !== []) {
return $this->createFuncCallSpec($node, $context, $scope);
}

return $this->typeSpecifier->create(
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,4 +1033,48 @@ public function testBug14596(): void
]);
}

public function testBug14684(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-14684.php'], [
[
'Call to private static method privateFoo() of class Bug14684\X.',
25,
],
[
'Call to protected static method protectedFoo() of class Bug14684\X.',
29,
],
[
'Call to private static method privateFoo() of class Bug14684\SubX.',
41,
],
[
'Call to protected static method protectedFoo() of class Bug14684\X.',
45,
],
[
'Call to private static method privateFoo() of class Bug14684\X.',
52,
],
[
'Call to protected static method protectedFoo() of class Bug14684\X.',
56,
],
[
'Call to private static method privateFoo() of class Bug14684\SubX.',
60,
],
[
'Call to private static method privateFoo() of class Bug14684\X.',
71,
],
[
'Call to protected static method protectedFoo() of class Bug14684\X.',
75,
],
]);
}

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

namespace Bug14684;

class X {
static public function publicFoo():void {}

final static private function privateFoo():void {}

static protected function protectedFoo():void {}
}

final class SubX extends X {
static private function privateFoo():void {}
}

/** @param class-string<X> $row */
function testClassStringFinalMethod(string $row): void
{
if (method_exists($row, 'publicFoo')) {
$row::publicFoo();
}

if (method_exists($row, 'privateFoo')) {
$row::privateFoo();
}

if (method_exists($row, 'protectedFoo')) {
$row::protectedFoo();
}
}

/** @param class-string<SubX> $row */
function testClassStringFinalClass(string $row): void
{
if (method_exists($row, 'publicFoo')) {
$row::publicFoo();
}

if (method_exists($row, 'privateFoo')) {
$row::privateFoo();
}

if (method_exists($row, 'protectedFoo')) {
$row::protectedFoo();
}
}

function testLiteralClassCall(): void
{
if (method_exists(X::class, 'privateFoo')) {
X::privateFoo();
}

if (method_exists(X::class, 'protectedFoo')) {
X::protectedFoo();
}

if (method_exists(SubX::class, 'privateFoo')) {
SubX::privateFoo();
}
}

class Y {
}

/** @param class-string<X|Y> $row */
function testClassStringUnion(string $row): void
{
if (method_exists($row, 'privateFoo')) {
$row::privateFoo();
}

if (method_exists($row, 'protectedFoo')) {
$row::protectedFoo();
}

if (method_exists($row, 'publicFoo')) {
$row::publicFoo();
}
}
Loading