Skip to content

Commit 95584e1

Browse files
authored
Merge branch refs/heads/2.1.x into 2.2.x
2 parents dc7d857 + 56c1e37 commit 95584e1

File tree

5 files changed

+39
-54
lines changed

5 files changed

+39
-54
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 & 8 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
{
@@ -68,14 +74,6 @@ public function testRule(): void
6874
'Call to private method doFoo() of class MethodCallable\ParentClass.',
6975
53,
7076
],
71-
[
72-
'Creating callable from a non-native method MethodCallable\Lorem::doBar().',
73-
66,
74-
],
75-
[
76-
'Creating callable from a non-native method MethodCallable\Ipsum::doBar().',
77-
85,
78-
],
7977
]);
8078
}
8179

tests/PHPStan/Rules/Methods/StaticMethodCallableRuleTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ public function testRule(): void
9393
'Cannot call static method doFoo() on int.',
9494
22,
9595
],
96-
[
97-
'Creating callable from a non-native static method StaticMethodCallable\Lorem::doBar().',
98-
47,
99-
],
100-
[
101-
'Creating callable from a non-native static method StaticMethodCallable\Ipsum::doBar().',
102-
66,
103-
],
10496
]);
10597
}
10698

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)