Skip to content
New issue

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

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

Already on GitHub? # to your account

BCFile/FunctionDeclarations::get[Method]Properties(): skip over closure use statements #573

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Mar 29, 2024

Sister-PR to upstream PR PHPCSStandards/PHP_CodeSniffer#421

This PR improves performance of the BCFile::getMethodProperties() and the FunctionDeclarations::getProperties() methods and prevents incorrect return type information for closures use clauses containing invalid variable imports in the use clause (defensive coding).

Closure use statements can only import plain variables, not properties or other more complex variables.

As things were, when such "illegal" variables were imported in a closure use, the information for the return type could get mangled.
While this would be a parse error, for the purposes of static analysis, the BCFile::getMethodProperties() method and the FunctionDeclarations::getProperties() method should still handle this correctly.

This commit updates both methods to always skip over the complete use clause, which prevents the issue and improves performance as the same time (less token walking).

Includes unit tests.

@jrfnl jrfnl added this to the 1.0.x Next milestone Mar 29, 2024
@jrfnl jrfnl changed the title BCFile/FunctionDeclarations::get[Method]Properties(): bug fix - skip over closure use statements BCFile/FunctionDeclarations::get[Method]Properties(): skip over closure use statements Mar 30, 2024
@jrfnl jrfnl force-pushed the feature/getmethodproperties-bugfix-closure-use-vs-return-type branch from 9f63afb to 4e2bbe3 Compare March 30, 2024 10:21
@jrfnl
Copy link
Member Author

jrfnl commented Mar 30, 2024

As per PHPCSStandards/PHP_CodeSniffer#421 (comment):

Hmm.. turns out the code sample which triggered me to look into this was actually a parse error 🤦🏻‍♀️. I do still believe the change has value, but I've updated the PR description to reflect the reality (and simplified the tests a little).

@jrfnl jrfnl force-pushed the feature/getmethodproperties-bugfix-closure-use-vs-return-type branch from 4e2bbe3 to f777405 Compare March 31, 2024 13:19
…over closure use statements

Sister-PR to upstream PR PHPCSStandards/PHP_CodeSniffer 421

This PR improves performance of the `BCFile::getMethodProperties()` and the `FunctionDeclarations::getProperties()` methods and prevents incorrect return type information for closure `use` clauses containing invalid variable imports in the `use` clause (defensive coding).

Closure `use` statements can only import plain variables, not properties or other more complex variables.

As things were, when such "illegal" variables were imported in a closure `use`, the information for the return type could get mangled.
While this would be a parse error, for the purposes of static analysis, the `BCFile::getMethodProperties()` method and the `FunctionDeclarations::getProperties()` method should still handle this correctly.

This commit updates both methods to always skip over the complete `use` clause, which prevents the issue and improves performance as the same time (less token walking).

Includes unit tests.
@jrfnl jrfnl force-pushed the feature/getmethodproperties-bugfix-closure-use-vs-return-type branch from f777405 to 7e01385 Compare April 3, 2024 01:58
@jrfnl
Copy link
Member Author

jrfnl commented Apr 3, 2024

Upstream PR has been merged and will be included in PHPCS 3.9.2.

@jrfnl jrfnl enabled auto-merge April 3, 2024 01:59
@jrfnl jrfnl merged commit 1c913e4 into develop Apr 3, 2024
54 checks passed
@jrfnl jrfnl deleted the feature/getmethodproperties-bugfix-closure-use-vs-return-type branch April 3, 2024 02:04
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant