diff --git a/config/extension.neon b/config/extension.neon index 6e5ef6c..834df9b 100644 --- a/config/extension.neon +++ b/config/extension.neon @@ -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 @@ -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 diff --git a/src/NodeAnalyzer/TemplateFilePathResolver.php b/src/NodeAnalyzer/TemplateFilePathResolver.php index 33dd592..cc0b516 100644 --- a/src/NodeAnalyzer/TemplateFilePathResolver.php +++ b/src/NodeAnalyzer/TemplateFilePathResolver.php @@ -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 @@ -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 diff --git a/src/Support/DirectoryHelper.php b/src/Support/DirectoryHelper.php new file mode 100644 index 0000000..802688e --- /dev/null +++ b/src/Support/DirectoryHelper.php @@ -0,0 +1,31 @@ +fileHelper->absolutizePath($path); + + return str_replace(['phar://', 'vendor/phpstan/phpstan/phpstan.phar/'], ['', ''], $path); + }, $paths); + } +} diff --git a/tests/Rules/LaravelViewFunctionRuleTest.php b/tests/Rules/LaravelViewFunctionRuleTest.php index 32ec772..4c45b96 100644 --- a/tests/Rules/LaravelViewFunctionRuleTest.php +++ b/tests/Rules/LaravelViewFunctionRuleTest.php @@ -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, + ], ]); } diff --git a/tests/Rules/config/configWithTemplatePaths.neon b/tests/Rules/config/configWithTemplatePaths.neon index 0613928..5e393d1 100644 --- a/tests/Rules/config/configWithTemplatePaths.neon +++ b/tests/Rules/config/configWithTemplatePaths.neon @@ -2,4 +2,5 @@ includes: - ../../tests.neon parameters: templatePaths: - - tests/Rules/templates \ No newline at end of file + - tests/Rules/templates + - tests/Rules/templates/nested/directory \ No newline at end of file diff --git a/tests/Rules/data/laravel-view-function.php b/tests/Rules/data/laravel-view-function.php index 8c8596f..9d8e1a0 100644 --- a/tests/Rules/data/laravel-view-function.php +++ b/tests/Rules/data/laravel-view-function.php @@ -9,3 +9,5 @@ view('foo', ['foo' => 'bar']); view('php_directive_with_comment', []); + +view('dummyNamespace::home', ['variable' => 'foobar']); diff --git a/tests/Rules/templates/nested/directory/home.blade.php b/tests/Rules/templates/nested/directory/home.blade.php new file mode 100644 index 0000000..ae32f3f --- /dev/null +++ b/tests/Rules/templates/nested/directory/home.blade.php @@ -0,0 +1 @@ +{{ $variable + 6 }} \ No newline at end of file