Tokenizer/PHP: arrow function tokenization broken when true/false used in return type #453
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.
Description
BackfillFnTokenTest: use data providers when appropriate
These two tests were testing multiple test cases, but not using a data provider, which means that if one of the test cases would fail, the ones after the failing test case wouldn't even be tested.
By using a data provider, that issue is avoided.
It also allows for simplifying some of the test code for the
testKeywordReturnTypes()
test which duplicated the logic for thescopePositionTestHelper()
just to allow for custom error messages mentioning the test marker. This is no longer needed when each marker is tested individually via the data provider.Tokenizer/PHP: arrow function tokenization broken when true/false used in return type
Since PHP 8.0,
false
andnull
can be included in a union return type.As of PHP 8.2, both
true
,false
andnull
can be used as a stand-alone return type.The tokenizer layer handling arrow functions did not take this into account correctly. While
null
was handled correctly,true
andfalse
was not and would result in the arrow functionfn
keyword being tokenized asT_STRING
across all PHP versions.As a result of that, the other typical tokenizer changes related to arrow functions (
=>
asT_FN_ARROW
, scope/parenthesis owners etc) would also not be executed correctly.In practice, I suspect few people will have run into this bug as, after all, what's the point of declaring an arrow function which will only ever return
true
orfalse
? so in practice, it is likely to only have come into play for people usingtrue
orfalse
as part of an arrow function union type.All the same, PHPCS should handle this correctly.
Includes unit tests proving the bug and safeguarding the fix.
Suggested changelog entry
Fixed broken arrow function tokenization when the return type contained
true
orfalse
Types of changes