Skip to content

Commit

Permalink
Report invalid exclude paths in PHP config too
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 3, 2024
1 parent 44efcb2 commit 9718c14
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,24 @@ jobs:
echo "$OUTPUT"
../bashunit -a contains 'Invalid entry in ignoreErrors' "$OUTPUT"
../bashunit -a contains 'tests is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
- script: |
cd e2e/bad-exclude-paths
OUTPUT=$(../../bin/phpstan analyse -c phpneon.php || true)
echo "$OUTPUT"
../bashunit -a contains 'Invalid entry in ignoreErrors' "$OUTPUT"
../bashunit -a contains 'src/test.php is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
- script: |
cd e2e/bad-exclude-paths
OUTPUT=$(../../bin/phpstan analyse -c excludePaths.neon || true)
echo "$OUTPUT"
../bashunit -a contains 'Invalid entry in excludePaths' "$OUTPUT"
../bashunit -a contains 'tests is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
- script: |
cd e2e/bad-exclude-paths
OUTPUT=$(../../bin/phpstan analyse -c phpneon2.php || true)
echo "$OUTPUT"
../bashunit -a contains 'Invalid entry in excludePaths' "$OUTPUT"
../bashunit -a contains 'src/test.php is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
steps:
- name: "Checkout"
Expand Down
17 changes: 17 additions & 0 deletions e2e/bad-exclude-paths/phpneon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'includes' => [
__DIR__ . '/../../conf/bleedingEdge.neon',
],
'parameters' => [
'level' => '8',
'paths' => [__DIR__ . '/src'],
'ignoreErrors' => [
[
'message' => '#aaa#',
'path' => 'src/test.php', // not absolute path - invalid in .php config
],
],
],
];
16 changes: 16 additions & 0 deletions e2e/bad-exclude-paths/phpneon2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [
'includes' => [
__DIR__ . '/../../conf/bleedingEdge.neon',
],
'parameters' => [
'level' => '8',
'paths' => [__DIR__ . '/src'],
'excludePaths' => [
'analyse' => [
'src/test.php', // not absolute path - invalid in .php config
],
],
],
];
12 changes: 7 additions & 5 deletions src/DependencyInjection/ValidateExcludePathsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public function loadConfiguration(): void

$errors = [];
foreach (array_unique($paths) as $path) {
if (is_dir($path)) {
continue;
}
if (is_file($path)) {
continue;
if (FileExcluder::isAbsolutePath($path)) {
if (is_dir($path)) {
continue;
}
if (is_file($path)) {
continue;
}
}
if (FileExcluder::isFnmatchPattern($path)) {
continue;
Expand Down
12 changes: 7 additions & 5 deletions src/DependencyInjection/ValidateIgnoredErrorsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry
}

foreach ($ignorePaths as $ignorePath) {
if (is_dir($ignorePath)) {
continue;
}
if (is_file($ignorePath)) {
continue;
if (FileExcluder::isAbsolutePath($ignorePath)) {
if (is_dir($ignorePath)) {
continue;
}
if (is_file($ignorePath)) {
continue;
}
}
if (FileExcluder::isFnmatchPattern($ignorePath)) {
continue;
Expand Down
14 changes: 14 additions & 0 deletions src/File/FileExcluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function preg_match;
use function str_starts_with;
use function strlen;
use function substr;
use const DIRECTORY_SEPARATOR;
use const FNM_CASEFOLD;
use const FNM_NOESCAPE;
Expand Down Expand Up @@ -119,6 +120,19 @@ public function isExcludedFromAnalysing(string $file): bool
return false;
}

public static function isAbsolutePath(string $path): bool
{
if (DIRECTORY_SEPARATOR === '/') {
if (str_starts_with($path, '/')) {
return true;
}
} elseif (substr($path, 1, 1) === ':') {
return true;
}

return false;
}

public static function isFnmatchPattern(string $path): bool
{
return preg_match('~[*?[\]]~', $path) > 0;
Expand Down

0 comments on commit 9718c14

Please sign in to comment.