forked from szepeviktor/phpstan-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HookDocBlock.php
55 lines (43 loc) · 1.54 KB
/
HookDocBlock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Set return type of apply_filters() based on its optional preceding docblock.
*/
declare(strict_types=1);
namespace SzepeViktor\PHPStan\WordPress;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Type\FileTypeMapper;
class HookDocBlock
{
/** @var \PHPStan\Type\FileTypeMapper */
protected $fileTypeMapper;
public function __construct(FileTypeMapper $fileTypeMapper)
{
$this->fileTypeMapper = $fileTypeMapper;
}
public function getNullableHookDocBlock(FuncCall $functionCall, Scope $scope): ?ResolvedPhpDocBlock
{
$comment = self::getNullableNodeComment($functionCall);
if ($comment === null) {
return null;
}
// Fetch the docblock contents.
$code = $comment->getText();
// Resolve the docblock in scope.
$classReflection = $scope->getClassReflection();
$traitReflection = $scope->getTraitReflection();
return $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
($scope->isInClass() && $classReflection !== null) ? $classReflection->getName() : null,
($scope->isInTrait() && $traitReflection !== null) ? $traitReflection->getName() : null,
$scope->getFunctionName(),
$code
);
}
private static function getNullableNodeComment(FuncCall $node): ?\PhpParser\Comment\Doc
{
/** @var \PhpParser\Comment\Doc|null */
return $node->getAttribute('latestDocComment');
}
}