Skip to content

Commit a995cf7

Browse files
committed
Report unmatched ignoreErrors entries with a directory or pattern path only under the analysed paths
When the paths passed on the command line do not cover all the paths from the configuration, an entry whose path is a directory or a fnmatch pattern is reported as unmatched only when that path is under the analysed paths, comparing whole path segments (src/ does not cover srcc/). A pattern is judged by the directory before its first wildcard. An entry with several paths is reported with the analysed ones only. An entry without a path is not reported, what it matches can be anywhere. Without paths in the configuration, the analysed directories are still taken as the whole project, so the entries without a path keep being reported for those who always pass the paths on the command line. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ba2c-2885-703b-90d3-0df9bf93b430
1 parent c588ead commit a995cf7

12 files changed

Lines changed: 331 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
parameters:
2+
ignoreErrors:
3+
# matched whenever src/Plain/Foo.php is analysed, but fewer times than expected
4+
-
5+
identifier: return.type
6+
path: src/Plain/Foo.php
7+
count: 2
8+
-
9+
identifier: return.type
10+
path: srcc/Bar.php
11+
-
12+
message: '#never in Clean#'
13+
path: src/Plain/Clean.php
14+
-
15+
message: '#never in srcc#'
16+
path: srcc/Bar.php
17+
-
18+
message: '#never in Plain directory#'
19+
path: src/Plain
20+
-
21+
message: '#never in srcc directory#'
22+
path: srcc
23+
-
24+
message: '#never in Plain glob#'
25+
path: src/Plain/*.php
26+
-
27+
message: '#never in src glob#'
28+
path: src/*/Clean.php
29+
-
30+
message: '#never in leading glob#'
31+
path: '*/Plain/Clean.php'
32+
-
33+
message: '#never in two paths#'
34+
paths:
35+
- src/Plain/Clean.php
36+
- srcc/Bar.php
37+
- '#never globally#'
38+
-
39+
message: '#never globally either#'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up() {
4+
e2e::set_up
5+
}
6+
7+
function tear_down() {
8+
e2e::tear_down
9+
}
10+
11+
# Without paths in the configuration, it is not known what the whole project is. The directories
12+
# passed on the command line are taken as the whole project, so that the entries without a path
13+
# are still reported for those who always pass the paths on the command line.
14+
function test_directories_are_the_whole_project() {
15+
OUTPUT=$(../../bin/phpstan analyse -c phpstan-no-paths.neon --error-format raw src 2>&1) && CODE=0 || CODE=$?
16+
echo "$OUTPUT"
17+
assert_same "1" "$CODE"
18+
assert_contains '#never in Clean# in path' "$OUTPUT"
19+
assert_contains '#never in Plain directory# in path' "$OUTPUT"
20+
assert_contains 'Ignored error pattern #never globally# was not matched' "$OUTPUT"
21+
assert_contains 'Ignored error pattern #never globally either# was not matched' "$OUTPUT"
22+
# a file that is not analysed reports no errors either way
23+
assert_not_contains '#never in srcc# in path' "$OUTPUT"
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
includes:
2+
- ignore-errors.neon
3+
4+
parameters:
5+
level: 8
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- ignore-errors.neon
3+
4+
parameters:
5+
level: 8
6+
paths:
7+
- src
8+
- srcc
9+
- tests
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace IgnoreErrorsPartialAnalysis;
4+
5+
class Clean
6+
{
7+
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace IgnoreErrorsPartialAnalysis;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(): int
9+
{
10+
return 'doFoo';
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace IgnoreErrorsPartialAnalysis;
4+
5+
class Bar
6+
{
7+
8+
public function doBar(): int
9+
{
10+
return 'doBar';
11+
}
12+
13+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up() {
4+
e2e::set_up
5+
}
6+
7+
function tear_down() {
8+
e2e::tear_down
9+
}
10+
11+
function test_whole_project_reports_all_unmatched_ignores() {
12+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
13+
echo "$OUTPUT"
14+
assert_same "1" "$CODE"
15+
assert_contains '#never in Clean# in path' "$OUTPUT"
16+
assert_contains '#never in srcc# in path' "$OUTPUT"
17+
assert_contains '#never in Plain directory# in path' "$OUTPUT"
18+
assert_contains '#never in srcc directory# in path' "$OUTPUT"
19+
assert_contains '#never in Plain glob# in path' "$OUTPUT"
20+
assert_contains '#never in src glob# in path' "$OUTPUT"
21+
assert_contains '#never in leading glob# in path' "$OUTPUT"
22+
assert_contains '#never in two paths# in paths:' "$OUTPUT"
23+
assert_contains 'Ignored error pattern #never globally# was not matched' "$OUTPUT"
24+
assert_contains 'Ignored error pattern #never globally either# was not matched' "$OUTPUT"
25+
assert_contains 'is expected to occur 2 times, but occurred only 1 time.' "$OUTPUT"
26+
}
27+
28+
function test_cli_paths_covering_config_paths_are_the_whole_project() {
29+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src srcc tests 2>&1) && CODE=0 || CODE=$?
30+
echo "$OUTPUT"
31+
assert_same "1" "$CODE"
32+
assert_contains '#never in srcc directory# in path' "$OUTPUT"
33+
assert_contains '#never in leading glob# in path' "$OUTPUT"
34+
assert_contains 'Ignored error pattern #never globally# was not matched' "$OUTPUT"
35+
}
36+
37+
function test_subdirectory_reports_only_ignores_under_it() {
38+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src 2>&1) && CODE=0 || CODE=$?
39+
echo "$OUTPUT"
40+
assert_same "1" "$CODE"
41+
assert_contains '#never in Clean# in path' "$OUTPUT"
42+
assert_contains '#never in Plain directory# in path' "$OUTPUT"
43+
assert_contains '#never in Plain glob# in path' "$OUTPUT"
44+
assert_contains '#never in src glob# in path' "$OUTPUT"
45+
assert_contains 'is expected to occur 2 times, but occurred only 1 time.' "$OUTPUT"
46+
# only the path under the analysed paths is left in the entry
47+
assert_contains "#never in two paths# in path $PWD/src/Plain/Clean.php was not matched" "$OUTPUT"
48+
# srcc/ starts with src, but it is not under src/
49+
assert_not_contains '#never in srcc#' "$OUTPUT"
50+
assert_not_contains '#never in srcc directory#' "$OUTPUT"
51+
# a pattern starting with a wildcard can match anywhere
52+
assert_not_contains '#never in leading glob#' "$OUTPUT"
53+
# what an entry without a path matches can be anywhere
54+
assert_not_contains '#never globally#' "$OUTPUT"
55+
assert_not_contains '#never globally either#' "$OUTPUT"
56+
}
57+
58+
function test_deeper_subdirectory() {
59+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Plain 2>&1) && CODE=0 || CODE=$?
60+
echo "$OUTPUT"
61+
assert_same "1" "$CODE"
62+
assert_contains '#never in Clean# in path' "$OUTPUT"
63+
assert_contains '#never in Plain directory# in path' "$OUTPUT"
64+
assert_contains '#never in Plain glob# in path' "$OUTPUT"
65+
# src/*/Clean.php can match files in other directories under src/
66+
assert_not_contains '#never in src glob#' "$OUTPUT"
67+
assert_not_contains '#never globally#' "$OUTPUT"
68+
}
69+
70+
function test_sibling_directory() {
71+
OUTPUT=$(../../bin/phpstan analyse --error-format raw srcc 2>&1) && CODE=0 || CODE=$?
72+
echo "$OUTPUT"
73+
assert_same "1" "$CODE"
74+
assert_contains '#never in srcc# in path' "$OUTPUT"
75+
assert_contains '#never in srcc directory# in path' "$OUTPUT"
76+
assert_contains "#never in two paths# in path $PWD/srcc/Bar.php was not matched" "$OUTPUT"
77+
assert_not_contains '#never in Clean#' "$OUTPUT"
78+
assert_not_contains '#never in Plain directory#' "$OUTPUT"
79+
assert_not_contains '#never in Plain glob#' "$OUTPUT"
80+
assert_not_contains '#never in src glob#' "$OUTPUT"
81+
assert_not_contains 'is expected to occur' "$OUTPUT"
82+
assert_not_contains '#never globally#' "$OUTPUT"
83+
}
84+
85+
function test_directory_without_ignores() {
86+
OUTPUT=$(../../bin/phpstan analyse --error-format raw tests 2>&1) && CODE=0 || CODE=$?
87+
echo "$OUTPUT"
88+
assert_same "0" "$CODE"
89+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace IgnoreErrorsPartialAnalysis;
4+
5+
class FooTest
6+
{
7+
8+
}

‎src/Analyser/Ignore/IgnoredErrorHelper.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\File\FileHelper;
1010
use PHPStan\ShouldNotHappenException;
1111
use function array_key_exists;
12+
use function array_map;
1213
use function array_values;
1314
use function is_array;
1415
use function is_file;
@@ -34,6 +35,8 @@ final class IgnoredErrorHelper
3435

3536
/**
3637
* @param (string|IgnoredErrorData)[] $ignoreErrors
38+
* @param string[] $analysedPaths
39+
* @param string[] $analysedPathsFromConfig
3740
*/
3841
public function __construct(
3942
private FileHelper $fileHelper,
@@ -42,6 +45,10 @@ public function __construct(
4245
private array $ignoreErrors,
4346
#[AutowiredParameter]
4447
private bool $reportUnmatchedIgnoredErrors,
48+
#[AutowiredParameter]
49+
private array $analysedPaths,
50+
#[AutowiredParameter]
51+
private array $analysedPathsFromConfig,
4552
)
4653
{
4754
}
@@ -171,7 +178,19 @@ public function initialize(): IgnoredErrorHelperResult
171178
}
172179
}
173180

174-
return new IgnoredErrorHelperResult($this->fileHelper, $this->configFixer, $errors, $otherIgnoreErrors, $ignoreErrorsByFile, $expandedIgnoreErrors, $this->reportUnmatchedIgnoredErrors);
181+
$normalizePath = fn (string $path): string => $this->fileHelper->normalizePath($path);
182+
183+
return new IgnoredErrorHelperResult(
184+
$this->fileHelper,
185+
$this->configFixer,
186+
$errors,
187+
$otherIgnoreErrors,
188+
$ignoreErrorsByFile,
189+
$expandedIgnoreErrors,
190+
$this->reportUnmatchedIgnoredErrors,
191+
array_map($normalizePath, $this->analysedPaths),
192+
array_map($normalizePath, $this->analysedPathsFromConfig),
193+
);
175194
}
176195

177196
}

0 commit comments

Comments
 (0)