Skip to content

Comments

Fix phpstan/phpstan#8744: Fix deprecated string interpolation syntax#5038

Open
codebymikey wants to merge 2 commits intophpstan:2.1.xfrom
codebymikey:feat/string-interpolation
Open

Fix phpstan/phpstan#8744: Fix deprecated string interpolation syntax#5038
codebymikey wants to merge 2 commits intophpstan:2.1.xfrom
codebymikey:feat/string-interpolation

Conversation

@codebymikey
Copy link

@codebymikey codebymikey commented Feb 24, 2026

Adds support for flagging deprecated string interpolation syntaxes, as well as fixing them.

It involves fetching the token from the parser, and using that to infer the original syntax used in the source code.

Fixes phpstan/phpstan#8744

@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x.

@ondrejmirtes
Copy link
Member

I'm not fan of this approach of polluting all the APIs with parser tokens.

@codebymikey codebymikey force-pushed the feat/string-interpolation branch from 3e82644 to 1741462 Compare February 24, 2026 12:12
@codebymikey codebymikey changed the base branch from 2.2.x to 2.1.x February 24, 2026 12:13
}

return [
RuleErrorBuilder::message($deprecatedMessage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a deprecation, and not a real error, should it be in deprecation-rules instead ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially, my finding was that the rule needs access to the parser tokens in some form to check the original syntax.

So as long as that API is exposed in the main phpstan library, then the rule could be moved into deprecation-rules if that's the most sensible part for it.

@codebymikey
Copy link
Author

I'm not fan of this approach of polluting all the APIs with parser tokens.

Wasn't sure of a better way to expose the tokens received by the parser since the parser interface is currently pretty simple.

And tokens are the only way I can think of to address it (that's how Rector fixes it too)

Having access to the token as additional context seems like a general improvement for rules that might need it in the future.

Any thoughts on how to work around it without the API pollution?

Unless only specific parsers have to implement a new TokenAwareParser interface. But I'm not familiar enough with the codebase to know which ones would require that additional interface and which ones should be left untouched, so just updated all the ones I was aware of.

@codebymikey codebymikey force-pushed the feat/string-interpolation branch from 1741462 to 7c454ec Compare February 24, 2026 12:36
@codebymikey codebymikey force-pushed the feat/string-interpolation branch from 7c454ec to f059229 Compare February 24, 2026 13:36
return [];
}

$sourceTokens = $scope->getTokens();
Copy link
Contributor

@staabm staabm Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we instead add a new NodeVisitor which inspects the tokens at Ast-Parsing time and add necessary attributes onto the InterpolatedString-node, so we can just work on e.g. $interpolatedString->getAttribute(...) at rule level?

see other visitors in src/Parser which do similar things

Copy link
Author

@codebymikey codebymikey Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, but I believe we only have access to the same Node object in the AST as we do in the custom rules.

edit: I see what you mean.

@codebymikey codebymikey force-pushed the feat/string-interpolation branch from 4b168f5 to 04d83e1 Compare February 25, 2026 09:14
@codebymikey codebymikey force-pushed the feat/string-interpolation branch from 04d83e1 to 4f209d6 Compare February 25, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use of deprecated string interpolation syntax not reported

5 participants