Skip to content

Commit 1f24e49

Browse files
staabmVincentLanglet
authored andcommitted
Remove callable.nonNativeMethod
1 parent 9ac6d7a commit 1f24e49

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

src/Rules/Methods/MethodCallableRule.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\RegisteredRule;
8-
use PHPStan\Internal\SprintfHelper;
98
use PHPStan\Node\MethodCallableNode;
109
use PHPStan\Php\PhpVersion;
1110
use PHPStan\Rules\Rule;
1211
use PHPStan\Rules\RuleErrorBuilder;
13-
use function sprintf;
1412

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

4745
$methodNameName = $methodName->toString();
4846

49-
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getVar(), $node->getName());
50-
if ($methodReflection === null) {
51-
return $errors;
52-
}
53-
54-
$declaringClass = $methodReflection->getDeclaringClass();
55-
if ($declaringClass->hasNativeMethod($methodNameName)) {
56-
return $errors;
57-
}
58-
59-
$messagesMethodName = SprintfHelper::escapeFormatString($declaringClass->getDisplayName() . '::' . $methodReflection->getName() . '()');
60-
61-
$errors[] = RuleErrorBuilder::message(sprintf('Creating callable from a non-native method %s.', $messagesMethodName))
62-
->identifier('callable.nonNativeMethod')
63-
->build();
64-
65-
return $errors;
47+
return $this->methodCallCheck->check($scope, $methodNameName, $node->getVar(), $node->getName())[0];
6648
}
6749

6850
}

src/Rules/Methods/StaticMethodCallableRule.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\RegisteredRule;
8-
use PHPStan\Internal\SprintfHelper;
98
use PHPStan\Node\StaticMethodCallableNode;
109
use PHPStan\Php\PhpVersion;
1110
use PHPStan\Rules\Rule;
1211
use PHPStan\Rules\RuleErrorBuilder;
13-
use function sprintf;
1412

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

4745
$methodNameName = $methodName->toString();
4846

49-
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName());
50-
if ($methodReflection === null) {
51-
return $errors;
52-
}
53-
54-
$declaringClass = $methodReflection->getDeclaringClass();
55-
if ($declaringClass->hasNativeMethod($methodNameName)) {
56-
return $errors;
57-
}
58-
59-
$messagesMethodName = SprintfHelper::escapeFormatString($declaringClass->getDisplayName() . '::' . $methodReflection->getName() . '()');
60-
61-
$errors[] = RuleErrorBuilder::message(sprintf('Creating callable from a non-native static method %s.', $messagesMethodName))
62-
->identifier('callable.nonNativeMethod')
63-
->build();
64-
65-
return $errors;
47+
return $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName())[0];
6648
}
6749

6850
}

tests/PHPStan/Rules/Methods/MethodCallableRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public function testNotSupportedOnOlderVersions(): void
3939
]);
4040
}
4141

42+
#[RequiresPhp('>= 8.1')]
43+
public function testBug13596(): void
44+
{
45+
$this->analyse([__DIR__ . '/data/bug-13596.php'], []);
46+
}
47+
4248
#[RequiresPhp('>= 8.1')]
4349
public function testRule(): void
4450
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug13596;
4+
5+
use Closure;
6+
7+
class BaseClass
8+
{
9+
public function getCallable(): ?Closure
10+
{
11+
return method_exists($this, 'myCallable') ? $this->myCallable(...) : null;
12+
}
13+
14+
public function getCallableWithIsCallable(): ?Closure
15+
{
16+
return is_callable([$this, 'myCallable']) ? $this->myCallable(...) : null;
17+
}
18+
}
19+
20+
class ChildOne extends BaseClass
21+
{
22+
//
23+
}
24+
25+
class ChildTwo extends BaseClass
26+
{
27+
public function myCallable(): string
28+
{
29+
return 'I exist on child two.';
30+
}
31+
}

0 commit comments

Comments
 (0)