Skip to content

Commit

Permalink
don't complain about unsafe static call to method known to be public
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Mar 17, 2024
1 parent 3337f31 commit 9a98b7b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/Methods/CallPrivateMethodThroughStaticRule.php
Expand Up @@ -36,7 +36,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$classType = $scope->resolveTypeByName($className);
$classType = $scope->getType(new Node\Expr\ClassConstFetch(new Name('static'), 'class'))->getClassStringObjectType();
if (!$classType->hasMethod($methodName)->yes()) {
return [];
}
Expand Down
Expand Up @@ -26,4 +26,14 @@ public function testRule(): void
]);
}

public function testInstanceof(): void
{
$this->analyse([__DIR__ . '/data/call-private-method-static-instanceof.php'], [
[
'Unsafe call to private method CallPrivateMethodStaticInstanceof\FooBase::fooPrivate() through static::.',
27,
],
]);
}

}
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace CallPrivateMethodStaticInstanceof;

interface FooInterface
{
public static function fooPrivate(): int;
}

class FooBase
{
private static function fooPrivate(): string
{
return 'a';
}

public function bar(): void
{
if ($this instanceof FooInterface) {
static::fooPrivate();
}

if (is_a(static::class, FooInterface::class, true)) {
static::fooPrivate();
}

static::fooPrivate();
}
}

0 comments on commit 9a98b7b

Please sign in to comment.