From 2856081e9d4e9bac553398140de0edc50a8acba3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 5 Apr 2024 09:25:59 +0200 Subject: [PATCH] Forbid PHPUnitPHAR prefixed classes --- src/Rules/ClassForbiddenNameCheck.php | 1 + .../Classes/ExistingClassInClassExtendsRuleTest.php | 9 +++++++-- .../Rules/Classes/data/phpstan-internal-class.php | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Rules/ClassForbiddenNameCheck.php b/src/Rules/ClassForbiddenNameCheck.php index f03b87bd72..8dfe6ff141 100644 --- a/src/Rules/ClassForbiddenNameCheck.php +++ b/src/Rules/ClassForbiddenNameCheck.php @@ -19,6 +19,7 @@ class ClassForbiddenNameCheck 'PHPStan' => '_PHPStan_', 'Rector' => 'RectorPrefix', 'PHP-Scoper' => '_PhpScoper', + 'PHPUnit' => 'PHPUnitPHAR', ]; public function __construct(private Container $container) diff --git a/tests/PHPStan/Rules/Classes/ExistingClassInClassExtendsRuleTest.php b/tests/PHPStan/Rules/Classes/ExistingClassInClassExtendsRuleTest.php index 57d99fe613..62697c9c89 100644 --- a/tests/PHPStan/Rules/Classes/ExistingClassInClassExtendsRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ExistingClassInClassExtendsRuleTest.php @@ -108,14 +108,19 @@ public function testPhpstanInternalClass(): void ], [ 'Referencing prefixed Rector class: RectorPrefix202302\AClass.', - 52, + 56, $tip, ], [ 'Referencing prefixed PHP-Scoper class: _PhpScoper19ae93be897e\AClass.', - 55, + 59, $tip, ], + [ + 'Referencing prefixed PHPUnit class: PHPUnitPHAR\SebastianBergmann\Diff\Exception.', + 62, + 'This is most likely unintentional. Did you mean to type \SebastianBergmann\Diff\Exception?', + ], ]); } diff --git a/tests/PHPStan/Rules/Classes/data/phpstan-internal-class.php b/tests/PHPStan/Rules/Classes/data/phpstan-internal-class.php index 7f72911af0..bb96081020 100644 --- a/tests/PHPStan/Rules/Classes/data/phpstan-internal-class.php +++ b/tests/PHPStan/Rules/Classes/data/phpstan-internal-class.php @@ -47,6 +47,10 @@ class AClass { const Test = 1; } +namespace PHPUnitPHAR\SebastianBergmann\Diff; // mimicks a prefixed class, as contained in PHPUnit phar + +class Exception{} + namespace TestPhpstanInternalClass2; class FooBar extends \RectorPrefix202302\AClass @@ -54,3 +58,7 @@ class FooBar extends \RectorPrefix202302\AClass class Baz extends \_PhpScoper19ae93be897e\AClass {} + +class BazBar extends \PHPUnitPHAR\SebastianBergmann\Diff\Exception +{} +