Skip to content
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

Fix mis-identification of 'readonly' keyword #3773

Conversation

fredden
Copy link
Contributor

@fredden fredden commented Mar 3, 2023

The test suite for this package is failing on PHP 8.2. This seems to be due to a mis-identification of the readonly keyword when used as a function name. For example, https://github.com/squizlabs/PHP_CodeSniffer/actions/runs/4318800044/jobs/7537540753.

This pull request fixes that test failure.

I have tested this with PHP version 8.2.3. I also noticed that the test case "testReadonlyUsedAsFunctionCallWithSpaceBetweenKeywordAndParens" seems to have been in the wrong category within the test suite.

Screenshot_2023-03-03_12-24-12

@fredden
Copy link
Contributor Author

fredden commented Mar 3, 2023

Using the following test code on https://onlinephp.io/

<?php

function readonly() {
	print "yes\n";
}

readonly();
readonly ();
readonly /* comment */ ();

I get expected results on PHP versions 8.2.3, 8.0.28, 7.4.33, 7.3.33, 7.2.34, 7.1.33, 7.0.33, 5.6.40, 5.5.38, 5.4.45, 5.3.29, 5.2.17, 5.1.6, 5.0.5, 4.4.9, 4.3.11, 4.2.3, 4.1.2, 4.0.6

However, all versions of PHP 8.1.x produce a parse error on the last line with this code sample.

Do we need to add any special handling for PHP 8.1.x to this readonly detection?

@fredden
Copy link
Contributor Author

fredden commented May 16, 2023

@jrfnl as far as I know, this change should make all the automated tests pass. I expect that may help with the triage process for other pull requests. I don't know where this pull request fits in the list of what to review. I'm highlighting this to hopefully help, but please feel free to ignore this comment.

Copy link
Contributor

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@fredden and me looked at this PR in detail together and this needs more work.

Findings:

  • The current fix will break on PHP 8.2 DNF.
  • More tests needs to be added.

Basically, we need to make sure that all tests which are included in this PHP Core commit are also included in the PHPCS Tokenizer BackfillReadonlyTest file and that those tests pass correctly: php/php-src@08b7539

Also, in the original implementation of the backfill, the situation where a comment would be between the readonly and the ( for function declarations/calls was not taken into account.
This is not handled in PHP Core, however, the PHPCS token stream should still be consistent for this as it will otherwise break sniffs which specifically look for the T_READONLY token.

As for DNF: that doesn't need to be handled in this PR, but at the very least, the readonly keyword tokenization should not break on it, while it currently would.

Related: in #3667 there are some comments/analysis about the change in the readonly tokenization in PHP Core and how it relates to DNF.

I look forward to a next iteration on this PR.

@jrfnl
Copy link
Contributor

jrfnl commented Jun 7, 2023

@fredden fredden force-pushed the php82-readonly-keyword-as-function-call branch from 3053135 to f324e5c Compare June 8, 2023 10:45
@fredden fredden marked this pull request as ready for review June 8, 2023 10:52
@fredden fredden requested a review from jrfnl June 8, 2023 10:52
@fredden
Copy link
Contributor Author

fredden commented Oct 6, 2023

@jrfnl is there anything I can do to help progress this?

@jrfnl
Copy link
Contributor

jrfnl commented Dec 3, 2023

An adjusted version of this PR has been merged in PHPCSStandards/PHP_CodeSniffer#34

@fredden
Copy link
Contributor Author

fredden commented Dec 4, 2023

Superseded by PHPCSStandards/PHP_CodeSniffer#34

@fredden fredden closed this Dec 4, 2023
@fredden fredden deleted the php82-readonly-keyword-as-function-call branch December 4, 2023 16:19
@jrfnl jrfnl added this to the 3.8.0 milestone Dec 8, 2023
@jrfnl
Copy link
Contributor

jrfnl commented Dec 8, 2023

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

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.

2 participants