-
Notifications
You must be signed in to change notification settings - Fork 534
Introduce ForbiddenClassNameExtension for append additional forbidden… #2979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ondrejmirtes
merged 1 commit into
phpstan:1.10.x
from
kamil-zacek:kamil-proxy-class-detection
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.