Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unused views collector rule (#1423)
- Loading branch information
Showing
30 changed files
with
698 additions
and
90 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
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Collectors; | ||
|
||
use Illuminate\Mail\Mailable; | ||
use Illuminate\View\ViewName; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use PHPStan\Type\ObjectType; | ||
|
||
/** @implements Collector<Node\Expr\MethodCall, string> */ | ||
final class UsedEmailViewCollector implements Collector | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\MethodCall::class; | ||
} | ||
|
||
/** @param Node\Expr\MethodCall $node */ | ||
public function processNode(Node $node, Scope $scope): ?string | ||
{ | ||
$name = $node->name; | ||
|
||
if (! $name instanceof Identifier) { | ||
return null; | ||
} | ||
|
||
if (! in_array($name->name, ['markdown', 'view'], true)) { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) === 0) { | ||
return null; | ||
} | ||
|
||
$class = $node->var; | ||
|
||
if (! (new ObjectType(Mailable::class))->isSuperTypeOf($scope->getType($class))->yes()) { | ||
return null; | ||
} | ||
|
||
$template = $node->getArgs()[0]->value; | ||
|
||
if (! $template instanceof Node\Scalar\String_) { | ||
return null; | ||
} | ||
|
||
return ViewName::normalize($template->value); | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Collectors; | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\View\ViewName; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use PHPStan\Type\ObjectType; | ||
|
||
/** @implements Collector<Node\Expr\StaticCall, string> */ | ||
final class UsedRouteFacadeViewCollector implements Collector | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\StaticCall::class; | ||
} | ||
|
||
/** @param Node\Expr\StaticCall $node */ | ||
public function processNode(Node $node, Scope $scope): ?string | ||
{ | ||
$name = $node->name; | ||
|
||
if (! $name instanceof Node\Identifier) { | ||
return null; | ||
} | ||
|
||
if ($name->name !== 'view') { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) < 2) { | ||
return null; | ||
} | ||
|
||
$class = $node->class; | ||
|
||
if (! $class instanceof Node\Name) { | ||
return null; | ||
} | ||
|
||
$class = $scope->resolveName($class); | ||
|
||
if (! (new ObjectType(Route::class))->isSuperTypeOf(new ObjectType($class))->yes()) { | ||
return null; | ||
} | ||
|
||
$template = $node->getArgs()[1]->value; | ||
|
||
if (! $template instanceof Node\Scalar\String_) { | ||
return null; | ||
} | ||
|
||
return ViewName::normalize($template->value); | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Collectors; | ||
|
||
use Illuminate\Support\Facades\View; | ||
use Illuminate\View\ViewName; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use PHPStan\Type\ObjectType; | ||
|
||
/** @implements Collector<Node\Expr\StaticCall, string> */ | ||
final class UsedViewFacadeMakeCollector implements Collector | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\StaticCall::class; | ||
} | ||
|
||
/** @param Node\Expr\StaticCall $node */ | ||
public function processNode(Node $node, Scope $scope): ?string | ||
{ | ||
$name = $node->name; | ||
|
||
if (! $name instanceof Node\Identifier) { | ||
return null; | ||
} | ||
|
||
if ($name->name !== 'make') { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) < 1) { | ||
return null; | ||
} | ||
|
||
$class = $node->class; | ||
|
||
if (! $class instanceof Node\Name) { | ||
return null; | ||
} | ||
|
||
$class = $scope->resolveName($class); | ||
|
||
if (! (new ObjectType(View::class))->isSuperTypeOf(new ObjectType($class))->yes()) { | ||
return null; | ||
} | ||
|
||
$template = $node->getArgs()[0]->value; | ||
|
||
if (! $template instanceof Node\Scalar\String_) { | ||
return null; | ||
} | ||
|
||
return ViewName::normalize($template->value); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Collectors; | ||
|
||
use Illuminate\View\ViewName; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
|
||
/** @implements Collector<Node\Expr\FuncCall, string> */ | ||
final class UsedViewFunctionCollector implements Collector | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\FuncCall::class; | ||
} | ||
|
||
/** @param Node\Expr\FuncCall $node */ | ||
public function processNode(Node $node, Scope $scope): ?string | ||
{ | ||
$funcName = $node->name; | ||
|
||
if (! $funcName instanceof Node\Name) { | ||
return null; | ||
} | ||
|
||
$funcName = $scope->resolveName($funcName); | ||
|
||
if ($funcName !== 'view') { | ||
return null; | ||
} | ||
|
||
// TODO: maybe make sure this function is coming from Laravel | ||
|
||
if (count($node->getArgs()) < 1) { | ||
return null; | ||
} | ||
|
||
$template = $node->getArgs()[0]->value; | ||
|
||
if (! $template instanceof Node\Scalar\String_) { | ||
return null; | ||
} | ||
|
||
return ViewName::normalize($template->value); | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
namespace NunoMaduro\Larastan\Collectors; | ||
|
||
use NunoMaduro\Larastan\Support\ViewFileHelper; | ||
use PhpParser\Node; | ||
use PHPStan\Parser\Parser; | ||
use PHPStan\Parser\ParserErrorsException; | ||
|
||
final class UsedViewInAnotherViewCollector | ||
{ | ||
/** @see https://regex101.com/r/OyHHCY/1 */ | ||
private const VIEW_NAME_REGEX = '/@(extends|include(If|Unless|When|First)?)(\(.*?([\'"])(.*?)([\'"])([),]))/m'; | ||
|
||
public function __construct(private Parser $parser, private ViewFileHelper $viewFileHelper) | ||
{ | ||
} | ||
|
||
/** @return list<string> */ | ||
public function getUsedViews(): array | ||
{ | ||
$usedViews = []; | ||
foreach ($this->viewFileHelper->getAllViewFilePaths() as $viewFile) { | ||
try { | ||
$parserNodes = $this->parser->parseFile($viewFile); | ||
|
||
$usedViews = array_merge($usedViews, $this->processNodes($parserNodes)); | ||
} catch (ParserErrorsException $e) { | ||
continue; | ||
} | ||
} | ||
|
||
return $usedViews; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt[] $nodes | ||
* @return list<string> | ||
*/ | ||
private function processNodes(array $nodes): array | ||
{ | ||
$nodes = array_filter($nodes, function (Node $node) { | ||
return $node instanceof Node\Stmt\InlineHTML; | ||
}); | ||
|
||
if (count($nodes) === 0) { | ||
return []; | ||
} | ||
|
||
$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[5]; | ||
}, $matches)); | ||
} | ||
|
||
return $usedViews; | ||
} | ||
} |
Oops, something went wrong.