Skip to content

Commit 727dcd4

Browse files
SanderMullerclaude
andcommitted
Report inheritance cycles with a rule instead of a reflection error
A class that extends itself, interfaces that extend each other and traits that use each other cannot be declared in PHP. A class on a parent class cycle now has no parent instead of throwing CircularReference, and a trait inside a trait on a cycle processes only the traits reflection says it uses. CircularInheritanceRule reports the cycle on each of its members, at level 0. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent cb3b7e9 commit 727dcd4

21 files changed

Lines changed: 646 additions & 42 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,12 +1352,13 @@ jobs:
13521352
OUTPUT=$(../bashunit -a exit_code "0" "../../bin/phpstan analyse -vvv --error-format raw")
13531353
echo "$OUTPUT"
13541354
../bashunit -a not_contains 'not found' "$OUTPUT"
1355-
# A reflection error, here a class that extends itself, still keeps the result cache from
1356-
# being saved.
1355+
# A class that extends itself is reported by a rule and is not a reflection error, so the
1356+
# result cache is saved.
13571357
cp loop/Loop.php src/Loop.php
13581358
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vvv --error-format raw")
13591359
echo "$OUTPUT"
1360-
../bashunit -a contains 'Result cache was not saved because of non-ignorable exception: Reflection error: Circular reference' "$OUTPUT"
1360+
../bashunit -a contains 'Loop.php:5:Class ResultCacheE2EParseError\Loop extends itself.' "$OUTPUT"
1361+
../bashunit -a contains 'Result cache is saved.' "$OUTPUT"
13611362
- script: |
13621363
cd e2e/result-cache-internal-error
13631364
composer dump-autoload
@@ -1367,6 +1368,24 @@ jobs:
13671368
../bashunit -a contains 'Internal error: The rule crashed.' "$OUTPUT"
13681369
../bashunit -a contains 'Result cache was not saved' "$OUTPUT"
13691370
test ! -f tmp/resultCache.php
1371+
- script: |
1372+
cd e2e/result-cache-circular-inheritance
1373+
# src/First.php, src/Second.php and src/Third.php extend each other in a cycle. src/TraitA.php
1374+
# uses TraitB, which uses TraitC, without a cycle.
1375+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
1376+
echo "$OUTPUT"
1377+
../bashunit -a contains 'First.php:5:Class ResultCacheE2ECircularInheritance\First extends ResultCacheE2ECircularInheritance\Second, which extends ResultCacheE2ECircularInheritance\Third, which extends ResultCacheE2ECircularInheritance\First.' "$OUTPUT"
1378+
../bashunit -a not_contains 'TraitA' "$OUTPUT"
1379+
# Breaking the cycle in src/Third.php has to re-analyse src/First.php, although a class on a
1380+
# cycle has no parents and the file depends only on its direct parent otherwise.
1381+
cp patched/Third.php src/Third.php
1382+
# src/TraitC.php now uses TraitA, which closes a cycle. A trait declaration is not analysed on
1383+
# its own, so src/TraitA.php depends on the traits it uses only through the rule.
1384+
cp patched/TraitC.php src/TraitC.php
1385+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
1386+
echo "$OUTPUT"
1387+
../bashunit -a not_contains 'First.php' "$OUTPUT"
1388+
../bashunit -a contains 'TraitA.php:5:Trait ResultCacheE2ECircularInheritance\TraitA uses ResultCacheE2ECircularInheritance\TraitB, which uses ResultCacheE2ECircularInheritance\TraitC, which uses ResultCacheE2ECircularInheritance\TraitA.' "$OUTPUT"
13701389
- script: |
13711390
cd e2e/result-cache-string-class
13721391
../../bin/phpstan analyse
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 ResultCacheE2ECircularInheritance;
4+
5+
class Third
6+
{
7+
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ECircularInheritance;
4+
5+
trait TraitC
6+
{
7+
8+
use TraitA;
9+
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 0
3+
tmpDir: tmp
4+
paths:
5+
- src
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 ResultCacheE2ECircularInheritance;
4+
5+
class First extends Second
6+
{
7+
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 ResultCacheE2ECircularInheritance;
4+
5+
class Second extends Third
6+
{
7+
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 ResultCacheE2ECircularInheritance;
4+
5+
class Third extends First
6+
{
7+
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ECircularInheritance;
4+
5+
trait TraitA
6+
{
7+
8+
use TraitB;
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ECircularInheritance;
4+
5+
trait TraitB
6+
{
7+
8+
use TraitC;
9+
10+
}
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 ResultCacheE2ECircularInheritance;
4+
5+
trait TraitC
6+
{
7+
8+
}

0 commit comments

Comments
 (0)