Skip to content

Commit 9718c14

Browse files
committed
Report invalid exclude paths in PHP config too
1 parent 44efcb2 commit 9718c14

File tree

6 files changed

+73
-10
lines changed

6 files changed

+73
-10
lines changed

.github/workflows/e2e-tests.yml

+12
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,24 @@ jobs:
220220
echo "$OUTPUT"
221221
../bashunit -a contains 'Invalid entry in ignoreErrors' "$OUTPUT"
222222
../bashunit -a contains 'tests is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
223+
- script: |
224+
cd e2e/bad-exclude-paths
225+
OUTPUT=$(../../bin/phpstan analyse -c phpneon.php || true)
226+
echo "$OUTPUT"
227+
../bashunit -a contains 'Invalid entry in ignoreErrors' "$OUTPUT"
228+
../bashunit -a contains 'src/test.php is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
223229
- script: |
224230
cd e2e/bad-exclude-paths
225231
OUTPUT=$(../../bin/phpstan analyse -c excludePaths.neon || true)
226232
echo "$OUTPUT"
227233
../bashunit -a contains 'Invalid entry in excludePaths' "$OUTPUT"
228234
../bashunit -a contains 'tests is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
235+
- script: |
236+
cd e2e/bad-exclude-paths
237+
OUTPUT=$(../../bin/phpstan analyse -c phpneon2.php || true)
238+
echo "$OUTPUT"
239+
../bashunit -a contains 'Invalid entry in excludePaths' "$OUTPUT"
240+
../bashunit -a contains 'src/test.php is neither a directory, nor a file path, nor a fnmatch pattern.' "$OUTPUT"
229241
230242
steps:
231243
- name: "Checkout"

e2e/bad-exclude-paths/phpneon.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
'includes' => [
5+
__DIR__ . '/../../conf/bleedingEdge.neon',
6+
],
7+
'parameters' => [
8+
'level' => '8',
9+
'paths' => [__DIR__ . '/src'],
10+
'ignoreErrors' => [
11+
[
12+
'message' => '#aaa#',
13+
'path' => 'src/test.php', // not absolute path - invalid in .php config
14+
],
15+
],
16+
],
17+
];

e2e/bad-exclude-paths/phpneon2.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
return [
4+
'includes' => [
5+
__DIR__ . '/../../conf/bleedingEdge.neon',
6+
],
7+
'parameters' => [
8+
'level' => '8',
9+
'paths' => [__DIR__ . '/src'],
10+
'excludePaths' => [
11+
'analyse' => [
12+
'src/test.php', // not absolute path - invalid in .php config
13+
],
14+
],
15+
],
16+
];

src/DependencyInjection/ValidateExcludePathsExtension.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public function loadConfiguration(): void
4545

4646
$errors = [];
4747
foreach (array_unique($paths) as $path) {
48-
if (is_dir($path)) {
49-
continue;
50-
}
51-
if (is_file($path)) {
52-
continue;
48+
if (FileExcluder::isAbsolutePath($path)) {
49+
if (is_dir($path)) {
50+
continue;
51+
}
52+
if (is_file($path)) {
53+
continue;
54+
}
5355
}
5456
if (FileExcluder::isFnmatchPattern($path)) {
5557
continue;

src/DependencyInjection/ValidateIgnoredErrorsExtension.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry
151151
}
152152

153153
foreach ($ignorePaths as $ignorePath) {
154-
if (is_dir($ignorePath)) {
155-
continue;
156-
}
157-
if (is_file($ignorePath)) {
158-
continue;
154+
if (FileExcluder::isAbsolutePath($ignorePath)) {
155+
if (is_dir($ignorePath)) {
156+
continue;
157+
}
158+
if (is_file($ignorePath)) {
159+
continue;
160+
}
159161
}
160162
if (FileExcluder::isFnmatchPattern($ignorePath)) {
161163
continue;

src/File/FileExcluder.php

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function preg_match;
1010
use function str_starts_with;
1111
use function strlen;
12+
use function substr;
1213
use const DIRECTORY_SEPARATOR;
1314
use const FNM_CASEFOLD;
1415
use const FNM_NOESCAPE;
@@ -119,6 +120,19 @@ public function isExcludedFromAnalysing(string $file): bool
119120
return false;
120121
}
121122

123+
public static function isAbsolutePath(string $path): bool
124+
{
125+
if (DIRECTORY_SEPARATOR === '/') {
126+
if (str_starts_with($path, '/')) {
127+
return true;
128+
}
129+
} elseif (substr($path, 1, 1) === ':') {
130+
return true;
131+
}
132+
133+
return false;
134+
}
135+
122136
public static function isFnmatchPattern(string $path): bool
123137
{
124138
return preg_match('~[*?[\]]~', $path) > 0;

0 commit comments

Comments
 (0)