Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Nov 23, 2022
1 parent 9d3361c commit 709fe3b
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 50 deletions.
8 changes: 6 additions & 2 deletions bootstrap.php
Expand Up @@ -8,7 +8,9 @@
use NunoMaduro\Larastan\ApplicationResolver;
use Orchestra\Testbench\Concerns\CreatesApplication;

define('LARAVEL_START', microtime(true));
if (! defined('LARAVEL_START')) {
define('LARAVEL_START', microtime(true));
}

if (file_exists($applicationPath = getcwd().'/bootstrap/app.php')) { // Applications and Local Dev
$app = require $applicationPath;
Expand All @@ -26,4 +28,6 @@
$app->boot();
}

define('LARAVEL_VERSION', $app->version());
if (! defined('LARAVEL_VERSION')) {
define('LARAVEL_VERSION', $app->version());
}
11 changes: 9 additions & 2 deletions extension.neon
Expand Up @@ -21,6 +21,7 @@ parameters:
viewDirectories: []
checkModelProperties: false
checkPhpDocMissingReturn: false
checkUnusedViews: false

parametersSchema:
checkOctaneCompatibility: bool()
Expand All @@ -32,6 +33,7 @@ parametersSchema:
viewDirectories: listOf(string())
squashedMigrationsPath: listOf(string())
checkModelProperties: bool()
checkUnusedViews: bool()

conditionalTags:
NunoMaduro\Larastan\Rules\NoModelMakeRule:
Expand All @@ -44,6 +46,8 @@ conditionalTags:
phpstan.rules.rule: %checkModelProperties%
NunoMaduro\Larastan\Rules\ModelProperties\ModelPropertyStaticCallRule:
phpstan.rules.rule: %checkModelProperties%
NunoMaduro\Larastan\Rules\UnusedViewsRule:
phpstan.rules.rule: %checkUnusedViews%

services:
-
Expand Down Expand Up @@ -442,8 +446,6 @@ services:

-
class: NunoMaduro\Larastan\Rules\UnusedViewsRule
tags:
- phpstan.rules.rule

-
class: NunoMaduro\Larastan\Collectors\UsedViewFunctionCollector
Expand All @@ -464,6 +466,11 @@ services:
class: NunoMaduro\Larastan\Collectors\UsedViewFacadeMakeCollector
tags:
- phpstan.collector

-
class: NunoMaduro\Larastan\Collectors\UsedRouteFacadeViewCollector
tags:
- phpstan.collector
-
class: NunoMaduro\Larastan\Collectors\UsedViewInAnotherViewCollector
arguments:
Expand Down
59 changes: 59 additions & 0 deletions src/Collectors/UsedRouteFacadeViewCollector.php
@@ -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);
}
}
6 changes: 3 additions & 3 deletions src/Collectors/UsedViewInAnotherViewCollector.php
Expand Up @@ -9,8 +9,8 @@

final class UsedViewInAnotherViewCollector
{
/** @see https://regex101.com/r/8gosof/1 */
private const VIEW_NAME_REGEX = '/@(extends|include(If|Unless|When|First)?)(\(.*?\'(.*?)\'(\)|,))/m';
/** @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)
{
Expand Down Expand Up @@ -53,7 +53,7 @@ private function processNodes(array $nodes): array
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];
return $match[5];
}, $matches));
}

Expand Down
2 changes: 2 additions & 0 deletions src/Rules/UnusedViewsRule.php
Expand Up @@ -7,6 +7,7 @@
use function collect;
use Illuminate\View\Factory;
use NunoMaduro\Larastan\Collectors\UsedEmailViewCollector;
use NunoMaduro\Larastan\Collectors\UsedRouteFacadeViewCollector;
use NunoMaduro\Larastan\Collectors\UsedViewFacadeMakeCollector;
use NunoMaduro\Larastan\Collectors\UsedViewFunctionCollector;
use NunoMaduro\Larastan\Collectors\UsedViewInAnotherViewCollector;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function processNode(Node $node, Scope $scope): array
$node->get(UsedEmailViewCollector::class),
$node->get(UsedViewMakeCollector::class),
$node->get(UsedViewFacadeMakeCollector::class),
$node->get(UsedRouteFacadeViewCollector::class),
$this->viewsUsedInOtherViews,
])->flatten()->unique()->toArray();

Expand Down
2 changes: 1 addition & 1 deletion tests/Application/resources/views/index.blade.php
Expand Up @@ -6,4 +6,4 @@

Lorem ipsum

@include('home')
@include("home")
1 change: 1 addition & 0 deletions tests/Application/resources/views/route-view.blade.php
@@ -0,0 +1 @@
This is used in a Route::view
21 changes: 0 additions & 21 deletions tests/Features/ReturnTypes/Helpers/ViewExtension.php

This file was deleted.

Expand Up @@ -89,11 +89,4 @@ public function testEventDispatch(): void
['Event class Tests\Rules\Data\LaravelEventWithoutConstructor does not have a constructor and must be dispatched without any parameters.', 18],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__.'/phpstan-rules.neon',
];
}
}
11 changes: 6 additions & 5 deletions tests/Rules/Data/FooController.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\View\View;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Route;
use Illuminate\View\Factory;

class FooController
Expand Down Expand Up @@ -36,11 +37,6 @@ public function build(): self
return $this->markdown('emails.markdown');
}

public function foo(): self
{
return $this->markdown('home');
}

public function bar(): self
{
return $this->view('emails.view');
Expand All @@ -61,3 +57,8 @@ function viewStaticMake(): View
{
return \Illuminate\Support\Facades\View::make('view-static-make');
}

function routeView(): void
{
Route::view('/welcome', 'route-view');
}
Expand Up @@ -47,11 +47,4 @@ protected function getRule(): Rule
{
return new DeferrableServiceProviderMissingProvidesRule();
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__.'/phpstan-rules.neon',
];
}
}
13 changes: 11 additions & 2 deletions tests/Rules/UnusedViewsRuleTest.php
Expand Up @@ -3,13 +3,13 @@
namespace Rules;

use NunoMaduro\Larastan\Collectors\UsedEmailViewCollector;
use NunoMaduro\Larastan\Collectors\UsedRouteFacadeViewCollector;
use NunoMaduro\Larastan\Collectors\UsedViewFacadeMakeCollector;
use NunoMaduro\Larastan\Collectors\UsedViewFunctionCollector;
use NunoMaduro\Larastan\Collectors\UsedViewInAnotherViewCollector;
use NunoMaduro\Larastan\Collectors\UsedViewMakeCollector;
use NunoMaduro\Larastan\Rules\UnusedViewsRule;
use NunoMaduro\Larastan\Support\ViewFileHelper;
use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

Expand All @@ -18,7 +18,7 @@ class UnusedViewsRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
$viewFileHelper = new ViewFileHelper([__DIR__.'/../Application/resources/views'], $this->getContainer()->getByType(FileHelper::class));
$viewFileHelper = new ViewFileHelper([__DIR__.'/../Application/resources/views'], $this->getFileHelper());

return new UnusedViewsRule(new UsedViewInAnotherViewCollector(
$this->getContainer()->getService('currentPhpVersionSimpleDirectParser'),
Expand All @@ -33,9 +33,18 @@ protected function getCollectors(): array
new UsedEmailViewCollector,
new UsedViewMakeCollector,
new UsedViewFacadeMakeCollector,
new UsedRouteFacadeViewCollector,
];
}

protected function setUp(): void
{
parent::setUp();

// This is a workaround for a weird PHPStan container cache issue.
require __DIR__.'/../../bootstrap.php';
}

public function testRule(): void
{
$this->analyse([__DIR__.'/Data/FooController.php'], [
Expand Down

0 comments on commit 709fe3b

Please sign in to comment.