Add ExprUsedAsStringNode virtual node emitted when expressions are used as strings#5712
Open
phpstan-bot wants to merge 1 commit into
Open
Add ExprUsedAsStringNode virtual node emitted when expressions are used as strings#5712phpstan-bot wants to merge 1 commit into
ExprUsedAsStringNode virtual node emitted when expressions are used as strings#5712phpstan-bot wants to merge 1 commit into
Conversation
…used as strings - Add ExprUsedAsStringNode virtual node class extending NodeAbstract, wrapping the expression being used as string and the original AST node - Emit from Echo_ statement handler in NodeScopeResolver (one per echo expression) - Emit from PrintHandler for print expressions - Emit from CastStringHandler for (string) casts - Emit from InterpolatedStringHandler for interpolated strings and heredocs - Emit from AssignOpHandler for .= concat assignment right-hand side - Emit from NodeScopeResolver for InlineHTML using TypeExpr(ConstantStringType) - For echo/print of concat chains, a single node is emitted wrapping the entire concat expression
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.
Summary
Implements a new
ExprUsedAsStringNodevirtual node that is emitted whenever an expression is used in a string context. This allows rule authors to hook into a single node type instead of having to handle many different AST node types (Echo_, Print_, Cast\String_, BinaryOp\Concat, InterpolatedString, AssignOp\Concat, InlineHTML) individually.Changes
src/Node/ExprUsedAsStringNode.php— new virtual node extendingNodeAbstractwithgetExpression()(the expression being used as string) andgetOriginalNode()(the AST node causing the string usage)src/Analyser/NodeScopeResolver.php— emit for eachechoexpression and forInlineHTMLstatements (usingTypeExprwrapping aConstantStringType)src/Analyser/ExprHandler/PrintHandler.php— emit for theprintexpressionsrc/Analyser/ExprHandler/CastStringHandler.php— emit for(string)cast inner expressionsrc/Analyser/ExprHandler/InterpolatedStringHandler.php— emit for interpolated strings and heredocssrc/Analyser/ExprHandler/AssignOpHandler.php— emit for.=concat assignment right-hand sideFor concat chains used inside echo/print/cast/.= contexts, a single
ExprUsedAsStringNodeis emitted wrapping the entire concat expression — solving the issue reporter's complaint about getting multiple rule invocations per line for nestedBinaryOp\Concatnodes.Root cause
There was no unified virtual node for "expression used as string" contexts. Rule authors had to hook into
Node::classand use instanceof checks for every relevant AST node type, with special handling needed forBinaryOp\Concatchains to avoid duplicate errors.Test
Added
tests/PHPStan/Rules/ExprUsedAsString/with:ExprUsedAsStringTestRule.php— test rule that reports eachExprUsedAsStringNodeemissionExprUsedAsStringRuleTest.php— verifies correct emission for all covered contexts: echo (single and multi-expression), print, (string) cast, interpolated string, concat assignment, echo with concat chain, heredoc, and inline HTMLFixes phpstan/phpstan#13008