Skip to content

Commit aa986e8

Browse files
committed
Do not report ignoreErrors entries as unmatched when what they ignore depends on files that are not analysed
Rules on CollectedDataNode report errors in a file based on data collected in other files, like a call to a function that is pure only when its declaration is analysed too. In a partial analysis, the ignoreErrors entries of such a file were reported as unmatched, and removed by --fix. The collectors of possibly pure calls, the trait files of classes and calls with named arguments emit the files what is reported in the analysed file depends on as AnalysedFileDependencyCollector data, or null for the whole project. It is kept in the result cache with relative paths, and UnmatchedIgnoredErrorJudge follows it transitively. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ba2c-2885-703b-90d3-0df9bf93b430
1 parent a9434a5 commit aa986e8

26 files changed

Lines changed: 454 additions & 64 deletions

‎e2e/bug-15659/bug-15659_test.sh‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
# An analysis of a directory that is a part of the configured paths does not report the entries
12+
# whose errors are in the other directories as unmatched, so --fix does not remove them.
13+
function test_directory_runs_report_no_unmatched_ignores() {
14+
for DIRECTORY in lib src; do
15+
OUTPUT=$(../../bin/phpstan analyse --error-format raw $DIRECTORY 2>&1) && CODE=0 || CODE=$?
16+
echo "$OUTPUT"
17+
assert_same "0" "$CODE"
18+
done
19+
}
20+
21+
function test_fix_in_directory_keeps_ignores_needed_elsewhere() {
22+
ORIGINAL_CONFIG=$(cat phpstan.neon)
23+
OUTPUT=$(../../bin/phpstan analyse --fix --fix-ignored lib 2>&1) && CODE=0 || CODE=$?
24+
echo "$OUTPUT"
25+
assert_same "0" "$CODE"
26+
assert_contains '[OK] 1 error fixed in 1 file' "$OUTPUT"
27+
assert_contains '#[\Override]' "$(cat lib/U.php)"
28+
assert_same "$ORIGINAL_CONFIG" "$(cat phpstan.neon)"
29+
OUTPUT=$(../../bin/phpstan analyse --fix src 2>&1) && CODE=0 || CODE=$?
30+
echo "$OUTPUT"
31+
assert_same "$ORIGINAL_CONFIG" "$(cat phpstan.neon)"
32+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
33+
echo "$OUTPUT"
34+
assert_same "0" "$CODE"
35+
}

‎e2e/bug-15659/lib/U.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php // lib/U.php
2+
3+
namespace Lib;
4+
5+
class U extends \App\P
6+
{
7+
use \App\T;
8+
9+
public function m(): void
10+
{
11+
}
12+
}

‎e2e/bug-15659/phpstan.neon‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
parameters:
2+
level: 0
3+
phpVersion: 80300
4+
checkMissingOverrideMethodAttribute: true
5+
paths: [src, lib]
6+
ignoreErrors:
7+
-
8+
message: '#undefinedTwo#'
9+
-
10+
message: '#undefinedWildcard#'
11+
path: src/*
12+
-
13+
message: '#undefinedInTrait#'
14+
path: src/T.php
15+
-
16+
identifier: method.missingOverride
17+
-
18+
message: '#undefinedList#'
19+
paths: [src/B.php]

‎e2e/bug-15659/src/B.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php // src/B.php
2+
3+
namespace App;
4+
5+
function b(): void
6+
{
7+
undefinedTwo();
8+
undefinedWildcard();
9+
undefinedList();
10+
}

‎e2e/bug-15659/src/P.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php // src/P.php
2+
3+
namespace App;
4+
5+
class P
6+
{
7+
public function m(): void
8+
{
9+
}
10+
}
11+
12+
class A extends P
13+
{
14+
public function m(): void
15+
{
16+
}
17+
}

‎e2e/bug-15659/src/T.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php // src/T.php
2+
3+
namespace App;
4+
5+
trait T
6+
{
7+
public function t(): void
8+
{
9+
undefinedInTrait();
10+
}
11+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
# A rule on CollectedDataNode reports that a call has no effect only when the declaration of the called
12+
# function is analysed too. The collector tracks the file of the declaration, see
13+
# DependencyTracker::trackAnalysedFileDependency().
14+
function test_call_to_pure_function_declared_in_directory_not_analysed() {
15+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls 2>&1) && CODE=0 || CODE=$?
16+
echo "$OUTPUT"
17+
assert_same "0" "$CODE"
18+
}
19+
20+
function test_call_to_pure_function_with_its_declaration() {
21+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls srcc 2>&1) && CODE=0 || CODE=$?
22+
echo "$OUTPUT"
23+
assert_same "1" "$CODE"
24+
assert_not_contains 'function.resultUnused' "$OUTPUT"
25+
assert_contains '#never in CallsPureFunction# in path' "$OUTPUT"
26+
# the called function calls a function from src/Pure, which is not analysed
27+
assert_not_contains 'CallsTransitivelyPureFunction' "$OUTPUT"
28+
}
29+
30+
# The called function is pure only when the function it calls is pure, so the call is reported
31+
# only when both declarations are analysed.
32+
function test_call_to_transitively_pure_function() {
33+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls src/Pure 2>&1) && CODE=0 || CODE=$?
34+
echo "$OUTPUT"
35+
assert_same "0" "$CODE"
36+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls src/Pure srcc 2>&1) && CODE=0 || CODE=$?
37+
echo "$OUTPUT"
38+
assert_same "1" "$CODE"
39+
assert_not_contains 'function.resultUnused' "$OUTPUT"
40+
assert_contains '#never in CallsPureFunction# in path' "$OUTPUT"
41+
assert_contains '#never in CallsTransitivelyPureFunction# in path' "$OUTPUT"
42+
}
43+
44+
function test_call_to_pure_function_in_only_files() {
45+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls/CallsPureFunction.php 2>&1) && CODE=0 || CODE=$?
46+
echo "$OUTPUT"
47+
assert_same "0" "$CODE"
48+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls/CallsPureFunction.php srcc/pure-function.php 2>&1) && CODE=0 || CODE=$?
49+
echo "$OUTPUT"
50+
assert_same "1" "$CODE"
51+
assert_not_contains 'function.resultUnused' "$OUTPUT"
52+
assert_contains '#never in CallsPureFunction# in path' "$OUTPUT"
53+
}
54+
55+
# The dependencies of the files restored from the result cache come from the cache.
56+
function test_result_cache_of_call_to_pure_function() {
57+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Calls src/Pure 2>&1) && CODE=0 || CODE=$?
58+
echo "$OUTPUT"
59+
assert_same "0" "$CODE"
60+
echo '// changed' >> src/Pure/transitively-pure-function.php
61+
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw src/Calls src/Pure 2>&1) && CODE=0 || CODE=$?
62+
echo "$OUTPUT"
63+
assert_same "0" "$CODE"
64+
assert_contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
65+
}
66+
67+
# Whether a call with a named argument is reported depends on the subclasses renaming the parameter,
68+
# which can be anywhere, see DependencyTracker::trackAnalysedProjectDependency().
69+
function test_named_argument_depends_on_the_whole_project() {
70+
OUTPUT=$(../../bin/phpstan analyse --error-format raw src/Named srcc 2>&1) && CODE=0 || CODE=$?
71+
echo "$OUTPUT"
72+
assert_same "1" "$CODE"
73+
assert_not_contains 'CallsWithNamedArgument' "$OUTPUT"
74+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
75+
echo "$OUTPUT"
76+
assert_same "1" "$CODE"
77+
assert_not_contains 'argument.parameterRenamedInSubtype' "$OUTPUT"
78+
assert_contains '#never in CallsWithNamedArgument# in path' "$OUTPUT"
79+
assert_contains '#never in CallsPureFunction# in path' "$OUTPUT"
80+
assert_contains '#never in CallsTransitivelyPureFunction# in path' "$OUTPUT"
81+
}

‎e2e/ignore-errors-partial-analysis/ignore-errors.neon‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,21 @@ parameters:
8787
-
8888
identifier: function.resultUnused
8989
path: src/Calls/CallsPureFunction.php
90+
# reported only when the declarations of the called function and of the function it calls are analysed too
91+
-
92+
identifier: function.resultUnused
93+
path: src/Calls/CallsTransitivelyPureFunction.php
94+
# reported only when the subclass renaming the parameter is analysed too
95+
-
96+
identifier: argument.parameterRenamedInSubtype
97+
path: src/Named/CallsWithNamedArgument.php
98+
# stale, reported only when what is reported in the file is known
99+
-
100+
message: '#never in CallsPureFunction#'
101+
path: src/Calls/CallsPureFunction.php
102+
-
103+
message: '#never in CallsTransitivelyPureFunction#'
104+
path: src/Calls/CallsTransitivelyPureFunction.php
105+
-
106+
message: '#never in CallsWithNamedArgument#'
107+
path: src/Named/CallsWithNamedArgument.php
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+
function callTransitivelyPureFunction(): void
6+
{
7+
transitivelyPureFunction();
8+
}
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+
function callWithNamedArgument(NamedArgumentBase $base): void
6+
{
7+
$base->doFoo(value: 1);
8+
}

0 commit comments

Comments
 (0)