Skip to content

Commit 5edf987

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

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

src/Rules/Methods/MethodCallableRule.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Php\PhpVersion;
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleErrorBuilder;
13+
use stdClass;
1314
use function sprintf;
1415

1516
/**
@@ -46,23 +47,7 @@ public function processNode(Node $node, Scope $scope): array
4647

4748
$methodNameName = $methodName->toString();
4849

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;
50+
return $this->methodCallCheck->check($scope, $methodNameName, $node->getVar(), $node->getName())[0];
6651
}
6752

6853
}

src/Rules/Methods/StaticMethodCallableRule.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Php\PhpVersion;
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleErrorBuilder;
13+
use stdClass;
1314
use function sprintf;
1415

1516
/**
@@ -46,23 +47,7 @@ public function processNode(Node $node, Scope $scope): array
4647

4748
$methodNameName = $methodName->toString();
4849

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;
50+
return $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName())[0];
6651
}
6752

6853
}

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)