Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor the collection of views used in other views to new class
- Loading branch information
Showing
18 changed files
with
309 additions
and
37 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,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
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Collectors; | ||
|
||
use Illuminate\Contracts\View\Factory; | ||
use Illuminate\View\ViewName; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use PHPStan\Type\ObjectType; | ||
|
||
/** @implements Collector<Node\Expr\MethodCall, string> */ | ||
final class UsedViewMakeCollector 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 Node\Identifier) { | ||
return null; | ||
} | ||
|
||
if ($name->name !== 'make') { | ||
return null; | ||
} | ||
|
||
if (count($node->getArgs()) < 1) { | ||
return null; | ||
} | ||
|
||
$class = $node->var; | ||
|
||
if (! (new ObjectType(Factory::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
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Support; | ||
|
||
use Generator; | ||
use PHPStan\File\FileHelper; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use RegexIterator; | ||
|
||
final class ViewFileHelper | ||
{ | ||
/** | ||
* @param list<non-empty-string> $viewDirectories | ||
*/ | ||
public function __construct(private array $viewDirectories, private FileHelper $fileHelper) | ||
{ | ||
if (count($viewDirectories) === 0) { | ||
$this->viewDirectories = [resource_path('views')]; // @phpstan-ignore-line | ||
} | ||
} | ||
|
||
public function getAllViewFilePaths(): Generator | ||
{ | ||
foreach ($this->viewDirectories as $viewDirectory) { | ||
$absolutePath = $this->fileHelper->absolutizePath($viewDirectory); | ||
|
||
if (! is_dir($absolutePath)) { | ||
continue; | ||
} | ||
|
||
$views = iterator_to_array( | ||
new RegexIterator( | ||
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($absolutePath)), | ||
'/\.blade\.php$/i' | ||
) | ||
); | ||
|
||
foreach ($views as $view) { | ||
yield $view->getPathname(); | ||
} | ||
} | ||
} | ||
|
||
public function getAllViewNames(): Generator | ||
{ | ||
foreach ($this->viewDirectories as $viewDirectory) { | ||
$absolutePath = $this->fileHelper->absolutizePath($viewDirectory); | ||
|
||
if (! is_dir($absolutePath)) { | ||
continue; | ||
} | ||
|
||
$views = iterator_to_array( | ||
new RegexIterator( | ||
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($absolutePath)), | ||
'/\.blade\.php$/i' | ||
) | ||
); | ||
|
||
foreach ($views as $view) { | ||
if (str_contains($view->getPathname(), 'views/vendor')) { | ||
continue; | ||
} | ||
|
||
$viewName = explode($viewDirectory.'/', $view->getPathname()); | ||
|
||
yield str_replace(['/', '.blade.php'], ['.', ''], $viewName[1]); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
// Used with $this->markdown('emails.markdown') |
Oops, something went wrong.