Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add collector to collect used views from other views
- Loading branch information
Showing
7 changed files
with
75 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>Base view</h1> | ||
|
||
@include('users.index') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
// ignore here | ||
?> | ||
|
||
@extends('base') | ||
|
||
Lorem ipsum | ||
|
||
@include('home') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters