/
UnusedViewsRule.php
66 lines (54 loc) · 2.02 KB
/
UnusedViewsRule.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
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace NunoMaduro\Larastan\Rules;
use function collect;
use Illuminate\Support\Facades\File;
use Illuminate\View\Factory;
use NunoMaduro\Larastan\Collectors\UsedEmailViewCollector;
use NunoMaduro\Larastan\Collectors\UsedViewFunctionCollector;
use NunoMaduro\Larastan\Collectors\UsedViewInAnotherViewCollector;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\CollectedDataNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symfony\Component\Finder\SplFileInfo;
/** @implements Rule<CollectedDataNode> */
final class UnusedViewsRule implements Rule
{
public function getNodeType(): string
{
return CollectedDataNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$usedViews = collect([
$node->get(UsedViewFunctionCollector::class),
$node->get(UsedEmailViewCollector::class),
$node->get(UsedViewInAnotherViewCollector::class),
])->flatten()->unique()->toArray();
$allViews = array_map(function (SplFileInfo $file) {
return $file->getPathname();
}, array_filter(File::allFiles(resource_path('views')), function (SplFileInfo $file) {
return ! str_contains($file->getPathname(), 'views/vendor') && str_ends_with($file->getFilename(), '.blade.php');
}));
$existingViews = [];
/** @var Factory $view */
$view = view();
foreach ($usedViews as $viewName) {
// Not existing views are reported with `view-string` type
if ($view->exists($viewName)) {
$existingViews[] = $view->getFinder()->find($viewName);
}
}
$unusedViews = array_diff($allViews, $existingViews);
$errors = [];
foreach ($unusedViews as $file) {
$errors[] = RuleErrorBuilder::message('This view is not used in the project.')
->file($file)
->line(0)
->build();
}
return $errors;
}
}