Repository navigation
Conversation
PHPStan 2.3.5 adds auto-fixable rules. These rules now attach a fix when only one fix is correct: - forbidUnusedClosureParameters removes the trailing unused parameters. - forbidMatchDefaultArmForEnums removes the default arm when the other arms list all cases of the native type. A PHPDoc-only narrowing does not count, because a removed arm then causes UnhandledMatchError at runtime. - forbidUselessNullableReturn removes null from the native return type. It does not fix the error when the PHPDoc return type contains null, because the native type then becomes narrower than the PHPDoc type. - uselessPrivatePropertyDefaultValue removes the default value. PHPStan finds the node to fix by its class and file position. Two rules need other nodes than before: - forbidMatchDefaultArmForEnums processes Match_, because MatchExpressionNode does not expose the MatchArm nodes. - forbidUselessNullableReturn gets the return type node from the new ReturnTypeNodeVisitor. ReturnStatementsNode does not expose the ClassMethod or Function_ node. The PHPStan bump also makes one @PHPStan-Ignore unmatched. `phpstan analyse --fix` removed it. Co-Authored-By: Claude Code
phpstan/phpstan-src#6958 adds MethodReturnStatementsNode::getClassMethod() and FunctionReturnStatementsNode::getFunction(). forbidUselessNullableReturn now gets the native return type node from them, and ReturnTypeNodeVisitor is removed. The getters are not in a tagged release yet, so composer.json requires ^2.3.6@dev. Change it to ^2.3.6 after the tag. Co-Authored-By: Claude Code
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PHPStan 2.3.5 adds auto-fixable rules (
RuleErrorBuilder::fixNode()/removeNode(),phpstan analyse --fix). This PR adds a fix to the rules that have only one correct fix.forbidUnusedClosureParametersforbidMatchDefaultArmForEnumsdefaultarm.UnhandledMatchErrorat runtime.forbidUselessNullableReturnnullfrom the native return type (?int→int,int|string|null→int|string).null, because the native type then becomes narrower than the PHPDoc type.uselessPrivatePropertyDefaultValueImplementation notes
forbidMatchDefaultArmForEnumsnow processesMatch_instead ofMatchExpressionNode, becauseMatchExpressionNodedoes not expose theMatchArmnodes.forbidUselessNullableReturngets the return type node fromMethodReturnStatementsNode::getClassMethod()andFunctionReturnStatementsNode::getFunction(), which Expose ClassMethod and Function_ on MethodReturnStatementsNode and FunctionReturnStatementsNode phpstan/phpstan-src#6958 adds.uselessPrivatePropertyDefaultValuefixes thePropertyItem.ClassPropertyNode::getOriginalNode()returns a syntheticPropertywith the positions of the item, so thePropertyitself does not match.phpstan/phpstan: ^2.3.6@devfor now. The getters from Expose ClassMethod and Function_ on MethodReturnStatementsNode and FunctionReturnStatementsNode phpstan/phpstan-src#6958 are not in a tagged release yet. Do not merge before PHPStan 2.3.6 is tagged; then change the constraint to^2.3.6.2.3.xdev PHAR with #6958. Its "Compile PHAR" workflow currently fails on the Windows Turbo extension build, so it does not commit the PHAR.@phpstan-ignore offsetAccess.notFoundunmatched.phpstan analyse --fixremoved it..php.fixedextension. With.php, they collide with their inputs in the classmap and indetect-collisions.fn() =>instead offn () =>.Rules without a fix in this PR
enforceIteratorToArrayPreserveKeys: bothtrueandfalseare valid. The rule exists to force that decision.enforceReadonlyPublicProperty:readonlyandprivate(set)are both valid, andreadonlybreaks writes outside the constructor.enforceNativeReturnTypehint: possible follow-up. A new native return type on an overridable method breaks child classes without one. Fix rounds can fix those children in later rounds, but this needs its own PR.forbidPhpDocNullabilityMismatchWithNativeTypehint: either side can be the wrong one.Co-Authored-By: Claude Code