/
UsedViewInAnotherViewCollector.php
43 lines (33 loc) · 1.11 KB
/
UsedViewInAnotherViewCollector.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
<?php
namespace NunoMaduro\Larastan\Collectors;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Node\FileNode;
/** @implements Collector<FileNode, string[]> */
class UsedViewInAnotherViewCollector implements Collector
{
/** @see https://regex101.com/r/8gosof/1 */
private const VIEW_NAME_REGEX = '/@(extends|include(If|Unless|When|First)?)(\(.*?\'(.*?)\'(\)|,))/m';
public function getNodeType(): string
{
return FileNode::class;
}
public function processNode(Node $node, Scope $scope): ?array
{
$nodes = array_filter($node->getNodes(), function (Node $node) {
return $node instanceof Node\Stmt\InlineHTML;
});
if (count($nodes) === 0) {
return null;
}
$usedViews = [];
foreach ($nodes as $node) {
preg_match_all(self::VIEW_NAME_REGEX, $node->value, $matches, PREG_SET_ORDER, 0);
$usedViews = array_merge($usedViews, array_map(function ($match) {
return $match[4];
}, $matches));
}
return $usedViews;
}
}