Skip to content

Tokenizer/PHP: add tests for tokenization of yield and yield from + minor bug fix #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 25, 2024

Description

Tokenizer/PHP: add tests for tokenization of yield and yield from

.. to document and safeguard the existing behaviour.

Includes skipping one particular assertion on PHP 5.4.

This assertion would fail on PHP 5.4 with PHPUnit 4 as PHPUnit polyfills the T_YIELD token too, but with a different value, which causes the token 'type' to be set to UNKNOWN.

This issue only occurs when running the tests, not when running PHPCS outside of a test situation, so this is not a real problem when running PHPCS on PHP 5.4.

For reference: the PHPUnit polyfilled token is declared in the PHP_CodeCoverage_Report_HTML_Renderer_File class in vendor/phpunit/php-code-coverage/src/CodeCoverage/Report/HTML/Renderer/File.php

Tokenizer/PHP: simplify the "yield"/"yield from" polyfill code

By moving the check a T_STRING "yield" keyword up and always adjusting the token in the original token stream, we can remove a lot of duplicate code.

Tokenizer/PHP: bug fix - "yield" vs function return by ref (PHP 5.4)

On PHP 5.4, where PHP doesn't natively have the T_YIELD token yet, a yield function name for a function declared to return by reference would be tokenized as T_YIELD, instead of T_STRING due to the T_YIELD polyfill happening after the context sensitive keyword check has already run.

By moving the check for a T_STRING "yield" keyword up to above the check for context sensitive keywords, this bug is fixed.

Includes additional test.

Suggested changelog entry

Fixed bug: on PHP 5.4, if yield was used as the declaration name for a function declared to return by reference, the function name would incorrectly be tokenized as T_YIELD instead of T_STRING.

jrfnl added 3 commits October 25, 2024 03:11
.. to document and safeguard the existing behaviour.

Includes skipping one particular assertion on PHP 5.4.

This assertion would fail on PHP 5.4 with PHPUnit 4 as PHPUnit polyfills the `T_YIELD` token too, but with a different value, which causes the token 'type' to be set to `UNKNOWN`.

This issue _only_ occurs when running the tests, not when running PHPCS outside of a test situation, so this is not a real problem when running PHPCS on PHP 5.4.

For reference: the PHPUnit polyfilled token is declared in the PHP_CodeCoverage_Report_HTML_Renderer_File class in vendor/phpunit/php-code-coverage/src/CodeCoverage/Report/HTML/Renderer/File.php
By moving the check a `T_STRING` "yield" keyword up and always adjusting the token in the original token stream, we can remove a lot of duplicate code.
On PHP 5.4, where PHP doesn't natively have the `T_YIELD` token yet, a `yield` function name for a function declared to return by reference would be tokenized as `T_YIELD`, instead of `T_STRING` due to the `T_YIELD` polyfill happening _after_ the context sensitive keyword check has already run.

By moving the check for a `T_STRING` "yield" keyword up to above the check for context sensitive keywords, this bug is fixed.

Includes additional test.
@jrfnl jrfnl added this to the 3.11.0 milestone Oct 25, 2024
@jrfnl jrfnl merged commit aafc72f into master Oct 25, 2024
52 checks passed
@jrfnl jrfnl deleted the feature/tokenizer-php-yield-from-add-tests+minor-bug-fix branch October 25, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant