-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ForbiddenClassNameExtension for append additional forbidden…
… class prefixes
- Loading branch information
1 parent
200641e
commit 0f8a352
Showing
47 changed files
with
205 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Classes; | ||
|
||
/** | ||
* This is the extension interface to implement if you want to dynamically | ||
* add forbidden class prefixes to the ClassForbiddenNameCheck rule. | ||
* | ||
* The idea is that you want to report usages of classes that you're not supposed to use in application. | ||
* For example: Generated Doctrine proxies from their configured namespace. | ||
* | ||
* To register it in the configuration file use the `phpstan.forbiddenClassNamesExtension` service tag: | ||
* | ||
* ``` | ||
* services: | ||
* - | ||
* class: App\PHPStan\MyExtension | ||
* tags: | ||
* - phpstan.forbiddenClassNamesExtension | ||
* ``` | ||
* | ||
* @api | ||
*/ | ||
interface ForbiddenClassNameExtension | ||
{ | ||
|
||
public const EXTENSION_TAG = 'phpstan.forbiddenClassNamesExtension'; | ||
|
||
/** @return array<string, string> */ | ||
public function getClassPrefixes(): array; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\ClassCaseSensitivityCheck; | ||
use PHPStan\Rules\ClassForbiddenNameCheck; | ||
use PHPStan\Rules\ClassNameCheck; | ||
use PHPStan\Rules\FunctionCallParametersCheck; | ||
use PHPStan\Rules\NullsafeCheck; | ||
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; | ||
use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
use function array_merge; | ||
|
||
/** | ||
* @extends RuleTestCase<InstantiationRule> | ||
*/ | ||
class ForbiddenNameCheckExtensionRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$reflectionProvider = $this->createReflectionProvider(); | ||
return new InstantiationRule( | ||
$reflectionProvider, | ||
new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, true, false), new NullsafeCheck(), new PhpVersion(80000), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true, true), | ||
new ClassNameCheck( | ||
new ClassCaseSensitivityCheck($reflectionProvider, true), | ||
new ClassForbiddenNameCheck(self::getContainer()), | ||
), | ||
); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return array_merge(parent::getAdditionalConfigFiles(), [ | ||
__DIR__ . '/data/forbidden-name-class-extension.neon', | ||
]); | ||
} | ||
|
||
public function testInternalClassFromExtensions(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/forbidden-name-class-extension.php'], [ | ||
[ | ||
'Referencing prefixed Doctrine class: App\GeneratedProxy\__CG__\App\TestDoctrineEntity.', | ||
31, | ||
'This is most likely unintentional. Did you mean to type \App\TestDoctrineEntity?', | ||
], | ||
[ | ||
'Referencing prefixed PHPStan class: _PHPStan_15755dag8c\TestPhpStanEntity.', | ||
32, | ||
'This is most likely unintentional. Did you mean to type \TestPhpStanEntity?', | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
tests/PHPStan/Rules/Classes/data/forbidden-name-class-extension.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
- | ||
class: ForbiddenNameClassExtension\CGForbiddenNameClassExtension | ||
tags: | ||
- phpstan.forbiddenClassNamesExtension |
32 changes: 32 additions & 0 deletions
32
tests/PHPStan/Rules/Classes/data/forbidden-name-class-extension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace App\GeneratedProxy\__CG__\App; | ||
|
||
class TestDoctrineEntity | ||
{ | ||
} | ||
|
||
namespace _PHPStan_15755dag8c; | ||
|
||
class TestPhpStanEntity | ||
{ | ||
} | ||
|
||
namespace ForbiddenNameClassExtension; | ||
|
||
use App\GeneratedProxy\__CG__\App\TestEntity; | ||
|
||
class CGForbiddenNameClassExtension implements \PHPStan\Classes\ForbiddenClassNameExtension | ||
{ | ||
|
||
public function getClassPrefixes(): array | ||
{ | ||
return [ | ||
'Doctrine' => 'App\GeneratedProxy\__CG__', | ||
]; | ||
} | ||
|
||
} | ||
|
||
$doctrineEntity = new \App\GeneratedProxy\__CG__\App\TestDoctrineEntity(); | ||
$phpStanEntity = new \_PHPStan_15755dag8c\TestPhpStanEntity(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.