You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Traits are evaluated based on static knowledge in assembled classes #4570
The logic in a trait is evaluated together with static knowledge that is only present in its assembled form as used within a class.
This leads to problems where traits contain logic that is conditional based on which class the trait is included in. For the classes where the conditional logic would not be needed, PHPStan complains about these conditionals, although they could not be left out of the trait in its stand-alone form.
As an example, when a trait looks at a class constant to see if it contains a certain value or key, PHPStan will evaluate this with the actual class constant present for each class, and complain that conditional would always yield the same result.
Conditional logic in traits cannot make use of knowledge that is only present once the trait is compiled into a class, and no errors like the above should be raised.
@@ @@- 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'valueToFetch' => '123') will always evaluate to true.-10: Unreachable statement - code above always terminates.- 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'someOtherValue' => '123') will always evaluate to false.+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40+Run PHPStan with --debug option and post the stack trace to:+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line
Error
-1
Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
@@ @@- 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'valueToFetch' => '123') will always evaluate to true.-10: Unreachable statement - code above always terminates.- 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'someOtherValue' => '123') will always evaluate to false.+18: Constant FirstConsumer::MY_CONST_ARRAY is unused.+28: Constant SecondConsumer::MY_CONST_ARRAY is unused.+ 5: Method FirstConsumer::fetchValue() should return string but return statement is missing.+ 5: Method SecondConsumer::fetchValue() should return string but return statement is missing.
Full report
Line
Error
18
Constant FirstConsumer::MY_CONST_ARRAY is unused.
28
Constant SecondConsumer::MY_CONST_ARRAY is unused.
5
Method FirstConsumer::fetchValue() should return string but return statement is missing.
5
Method SecondConsumer::fetchValue() should return string but return statement is missing.
@@ @@- 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'valueToFetch' => '123') will always evaluate to true.+ 6: Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', valueToFetch: '123'} will always evaluate to true.
10: Unreachable statement - code above always terminates.
- 6: Call to function array_key_exists() with 'valueToFetch' and array('someValue' => 'abc', 'someOtherValue' => '123') will always evaluate to false.+ 6: Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', someOtherValue: '123'} will always evaluate to false.
Full report
Line
Error
6
Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', valueToFetch: '123'} will always evaluate to true.
Activity
phpstan-bot commentedon Sep 23, 2021
@schlessera After the latest commit in dev-master, PHPStan now reports different result with your code snippet:
Full report
Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.mdphpstan-bot commentedon Oct 4, 2021
@schlessera After the latest commit in dev-master, PHPStan now reports different result with your code snippet:
Full report
Constant FirstConsumer::MY_CONST_ARRAY is unused.Constant SecondConsumer::MY_CONST_ARRAY is unused.Method FirstConsumer::fetchValue() should return string but return statement is missing.Method SecondConsumer::fetchValue() should return string but return statement is missing.phpstan-bot commentedon Oct 13, 2021
@schlessera After the latest commit in dev-master, PHPStan now reports different result with your code snippet:
Full report
Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', valueToFetch: '123'} will always evaluate to true.Unreachable statement - code above always terminates.Call to function array_key_exists() with 'valueToFetch' and array{someValue: 'abc', someOtherValue: '123'} will always evaluate to false.LastDragon-ru commentedon Sep 20, 2022
Really annoying issue :(
paranoiq commentedon Sep 7, 2023
probably related or the same issue: https://phpstan.org/r/89bc3b55-f22e-4a0b-bf92-84db066c899a (static::class used in trait)
Regression tests
github-actions commentedon Apr 28, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.