-
Notifications
You must be signed in to change notification settings - Fork 574
Mark class_exists, interface_exists, trait_exists, and enum_exists as having no side effects in function metadata
#5607
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
staabm
merged 9 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-625e02d
May 9, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92c4354
Mark `class_exists`, `interface_exists`, `trait_exists`, and `enum_ex…
VincentLanglet dc77162
Use AlwaysRememberedExpr to bypass impurity check for class_exists ty…
phpstan-bot 38f2dac
Add test ensuring class_exists false result is not always remembered
phpstan-bot 79bbb99
Convert Bug8579Test to standard rule test and assertType test
phpstan-bot d421a55
Add assertType inside if branches for class_exists do-not-remember test
phpstan-bot 0afc48a
Add interface_exists/trait_exists/enum_exists tests and fix nsrt asse…
phpstan-bot f6ea092
Document AlwaysRememberedExpr with class-level PHPDoc
phpstan-bot 8318b1a
Add tests for interface_exists, trait_exists, and enum_exists with re…
phpstan-bot e69564b
Shorten AlwaysRememberedExpr PHPDoc to be generic about its purpose
phpstan-bot 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
Some comments aren't visible on the classic Files Changed page.
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
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
35 changes: 35 additions & 0 deletions
35
tests/PHPStan/Rules/Classes/InstantiationDoNotRememberPossiblyImpureValuesRuleTest.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,35 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Classes; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use function array_merge; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<InstantiationRule> | ||
| */ | ||
| class InstantiationDoNotRememberPossiblyImpureValuesRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return self::getContainer()->getByType(InstantiationRule::class); | ||
| } | ||
|
|
||
| public function testBug8579(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/bug-8579.php'], []); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return array_merge( | ||
| parent::getAdditionalConfigFiles(), | ||
| [ | ||
| __DIR__ . '/doNotRememberPossiblyImpureValues.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,13 @@ | ||
| <?php | ||
|
|
||
| if (!class_exists('NonexistentClassBug8579')) throw new \Exception('nonexistentclass'); | ||
| $x = new \NonexistentClassBug8579(); | ||
|
|
||
| if (!interface_exists('NonexistentInterfaceBug8579')) throw new \Exception('nonexistentinterface'); | ||
| $x = new \NonexistentInterfaceBug8579(); | ||
|
|
||
| if (!trait_exists('NonexistentTraitBug8579')) throw new \Exception('nonexistenttrait'); | ||
| $x = new \NonexistentTraitBug8579(); | ||
|
|
||
| if (!enum_exists('NonexistentEnumBug8579')) throw new \Exception('nonexistentenum'); | ||
| $x = new \NonexistentEnumBug8579(); |
2 changes: 2 additions & 0 deletions
2
tests/PHPStan/Rules/Classes/doNotRememberPossiblyImpureValues.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,2 @@ | ||
| parameters: | ||
| rememberPossiblyImpureFunctionValues: false |
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.