Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions src/Rules/Methods/MethodCallableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\MethodCallableNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
* @implements Rule<MethodCallableNode>
Expand Down Expand Up @@ -46,23 +44,7 @@ public function processNode(Node $node, Scope $scope): array

$methodNameName = $methodName->toString();

[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getVar(), $node->getName());
if ($methodReflection === null) {
return $errors;
}

$declaringClass = $methodReflection->getDeclaringClass();
if ($declaringClass->hasNativeMethod($methodNameName)) {
return $errors;
}

$messagesMethodName = SprintfHelper::escapeFormatString($declaringClass->getDisplayName() . '::' . $methodReflection->getName() . '()');

$errors[] = RuleErrorBuilder::message(sprintf('Creating callable from a non-native method %s.', $messagesMethodName))
->identifier('callable.nonNativeMethod')
->build();

return $errors;
return $this->methodCallCheck->check($scope, $methodNameName, $node->getVar(), $node->getName())[0];
Copy link
Member

Choose a reason for hiding this comment

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

I think the returning of the array shape with the method reflection might have been introduced exactly because of this. It might be possible to simplify the return type and let it just returns errors instead of the tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked and the shape seems to always have existed, and the methodReflection is used somewhere else

private function processSingleMethodCall(Scope $scope, MethodCall $node, string $methodName): array

}

}
20 changes: 1 addition & 19 deletions src/Rules/Methods/StaticMethodCallableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Node\StaticMethodCallableNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
* @implements Rule<StaticMethodCallableNode>
Expand Down Expand Up @@ -46,23 +44,7 @@ public function processNode(Node $node, Scope $scope): array

$methodNameName = $methodName->toString();

[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName());
if ($methodReflection === null) {
return $errors;
}

$declaringClass = $methodReflection->getDeclaringClass();
if ($declaringClass->hasNativeMethod($methodNameName)) {
return $errors;
}

$messagesMethodName = SprintfHelper::escapeFormatString($declaringClass->getDisplayName() . '::' . $methodReflection->getName() . '()');

$errors[] = RuleErrorBuilder::message(sprintf('Creating callable from a non-native static method %s.', $messagesMethodName))
->identifier('callable.nonNativeMethod')
->build();

return $errors;
return $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName())[0];
}

}
14 changes: 6 additions & 8 deletions tests/PHPStan/Rules/Methods/MethodCallableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function testNotSupportedOnOlderVersions(): void
]);
}

#[RequiresPhp('>= 8.1')]
public function testBug13596(): void
{
$this->analyse([__DIR__ . '/data/bug-13596.php'], []);
}

#[RequiresPhp('>= 8.1')]
public function testRule(): void
{
Expand Down Expand Up @@ -68,14 +74,6 @@ public function testRule(): void
'Call to private method doFoo() of class MethodCallable\ParentClass.',
53,
],
[
'Creating callable from a non-native method MethodCallable\Lorem::doBar().',
66,
],
[
'Creating callable from a non-native method MethodCallable\Ipsum::doBar().',
85,
],
]);
}

Expand Down
8 changes: 0 additions & 8 deletions tests/PHPStan/Rules/Methods/StaticMethodCallableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ public function testRule(): void
'Cannot call static method doFoo() on int.',
22,
],
[
'Creating callable from a non-native static method StaticMethodCallable\Lorem::doBar().',
47,
],
[
'Creating callable from a non-native static method StaticMethodCallable\Ipsum::doBar().',
66,
],
]);
}

Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-13596.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php // lint >= 8.1

namespace Bug13596;

use Closure;

class BaseClass
{
public function getCallable(): ?Closure
{
return method_exists($this, 'myCallable') ? $this->myCallable(...) : null;
}

public function getCallableWithIsCallable(): ?Closure
{
return is_callable([$this, 'myCallable']) ? $this->myCallable(...) : null;
}
}

class ChildOne extends BaseClass
{
//
}

class ChildTwo extends BaseClass
{
public function myCallable(): string
{
return 'I exist on child two.';
}
}
Loading