Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve namespaced views #9

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
class: Illuminate\View\FileViewFinder
arguments:
files: Illuminate\Filesystem\Filesystem()
paths: %templatePaths%
paths: @Vural\PHPStanBladeRule\Support\DirectoryHelper::absolutizePaths(%templatePaths%)
extensions: ['blade.php', 'svg']
- Vural\PHPStanBladeRule\Rules\ViewRuleHelper
- Vural\PHPStanBladeRule\Blade\PhpLineToTemplateLineResolver
Expand All @@ -32,7 +32,8 @@ services:
-
class: Vural\PHPStanBladeRule\Compiler\FileNameAndLineNumberAddingPreCompiler
arguments:
templatePaths: %templatePaths%
templatePaths: @Vural\PHPStanBladeRule\Support\DirectoryHelper::absolutizePaths(%templatePaths%)
- Vural\PHPStanBladeRule\Compiler\BladeToPHPCompiler
- Vural\PHPStanBladeRule\Compiler\PhpContentExtractor
- Vural\PHPStanBladeRule\PHPParser\NodeVisitor\BladeLineNumberNodeVisitor
- class: Vural\PHPStanBladeRule\Support\DirectoryHelper
15 changes: 13 additions & 2 deletions src/NodeAnalyzer/TemplateFilePathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace Vural\PHPStanBladeRule\NodeAnalyzer;

use Illuminate\View\FileViewFinder;
use Illuminate\View\ViewName;
use Illuminate\View\ViewFinderInterface;
use InvalidArgumentException;
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;

use function explode;
use function file_exists;
use function is_string;
use function str_contains;
use function str_replace;

/** @see \Symplify\TemplatePHPStanCompiler\NodeAnalyzer\TemplateFilePathResolver */
final class TemplateFilePathResolver
Expand Down Expand Up @@ -48,7 +51,15 @@ public function resolveExistingFilePaths(Expr $expr, Scope $scope): array

private function normalizeName(string $name): string
{
return ViewName::normalize($name);
$delimiter = ViewFinderInterface::HINT_PATH_DELIMITER;

if (! str_contains($name, $delimiter)) {
return str_replace('/', '.', $name);
}

[$namespace, $name] = explode($delimiter, $name);

return str_replace('/', '.', $name);
}

private function findView(string $view): ?string
Expand Down
31 changes: 31 additions & 0 deletions src/Support/DirectoryHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Vural\PHPStanBladeRule\Support;

use PHPStan\File\FileHelper;

use function array_map;
use function str_replace;

final class DirectoryHelper
{
public function __construct(private FileHelper $fileHelper)
{
}

/**
* @param string[] $paths
*
* @return string[]
*/
public function absolutizePaths(array $paths): array
{
return array_map(function (string $path) {
$path = $this->fileHelper->absolutizePath($path);

return str_replace(['phar://', 'vendor/phpstan/phpstan/phpstan.phar/'], ['', ''], $path);
}, $paths);
}
}
4 changes: 4 additions & 0 deletions tests/Rules/LaravelViewFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function testRule(): void
'Binary operation "+" between string and 10 results in an error.',
11,
],
[
'Binary operation "+" between string and 6 results in an error.',
13,
],
]);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Rules/config/configWithTemplatePaths.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ includes:
- ../../tests.neon
parameters:
templatePaths:
- tests/Rules/templates
- tests/Rules/templates
- tests/Rules/templates/nested/directory
2 changes: 2 additions & 0 deletions tests/Rules/data/laravel-view-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
view('foo', ['foo' => 'bar']);

view('php_directive_with_comment', []);

view('dummyNamespace::home', ['variable' => 'foobar']);
1 change: 1 addition & 0 deletions tests/Rules/templates/nested/directory/home.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $variable + 6 }}