Skip to content

Commit 2c53ed6

Browse files
committed
Add PhpFunctionFromParserNodeReflection::isMethodOrPropertyHook()
1 parent de98821 commit 2c53ed6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Reflection/Php/PhpFunctionFromParserNodeReflection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ public function __construct(
7575
$this->functionLike = $functionLike;
7676
}
7777

78+
/**
79+
* @phpstan-assert-if-true PhpMethodFromParserNodeReflection $this
80+
*/
81+
public function isMethodOrPropertyHook(): bool
82+
{
83+
return $this instanceof PhpMethodFromParserNodeReflection;
84+
}
85+
7886
protected function getFunctionLike(): FunctionLike
7987
{
8088
return $this->functionLike;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ScopeFunctionInMethod;
6+
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
9+
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
10+
use function PHPStan\Testing\assertType;
11+
12+
class SomeRule
13+
{
14+
15+
public function doFoo(Scope $scope): void
16+
{
17+
$function = $scope->getFunction();
18+
if ($function === null) {
19+
return;
20+
}
21+
22+
assertType(PhpFunctionFromParserNodeReflection::class, $function);
23+
24+
if ($function->isMethodOrPropertyHook()) {
25+
assertType(PhpMethodFromParserNodeReflection::class, $function);
26+
} else {
27+
assertType(PhpFunctionFromParserNodeReflection::class . '~' . PhpMethodFromParserNodeReflection::class, $function);
28+
}
29+
}
30+
31+
}

0 commit comments

Comments
 (0)