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

[WIP] PHP 8.0: the throw statement as an expression #1275

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft

[WIP] PHP 8.0: the throw statement as an expression #1275

wants to merge 2 commits into from

Conversation

coderimus
Copy link

Since PHP 8.0, the throw statement can be used as an expression. This Pull Request provides a compatibility check for the newly added functionality:

  • a context where throw as an expression is accepted
  • operator precedence becomes relevant

RFC: PHP RFC: throw expression
Implementation: functionalituy implementation

Related to #809

Copy link
Member

@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.

@coderimus Hi Alex,

I've just had a quick look so far - so this is not a real review yet.

Aside from the sniff missing tests as discussed via Twitter DM, here are some other things to consider when you continue working on this:

  • The PHPCompatibility\PHPCSHelper class has been removed in favour of the PHPCSUtils\BackCompat\BCFile class.
    You'll probably want to use the findStartOfStatement() and findEndOfStatement() methods from that class (which will be updated for upstream changes in PHPCS 3.6.0 soon).
    Ref: https://phpcsutils.com/phpdoc/classes/PHPCSUtils-BackCompat-BCFile.html
  • Category-wise: I'm wondering if this sniff would fit into the existing Syntax category.... ?
  • Token arrays:
    • Tokens like T_FN_ARROW and T_COALESCE_EQUAL will not be available in all the PHPCS versions supported by PHPCompatibility. To get round the "undefined constant" part you can check against the token 'type' (= token name as a text string) instead of against the token 'code', but some extra work-arounds may be needed to make the sniffs cross-version compatible with older PHPCS versions for those tokens. PHPCSUtils may be able to help there.
      We can discuss this in more detail in our call this Friday.
    • Typically, it is generally a good idea to use a token => true or token => token pattern for these type of token arrays and then to use isset() instead of in_array() in the code. This improves the performance of the sniff.
    • You may also want to have a look at the PHPCS native Utils\Tokens class which contains some useful predefined token arrays and the sister-class in PHPCSUtils PHPCSUtils\BackCompat\BCTokens, as well as the PHPCSUtils\Tokens\Collections class.
      As the token arrays in those classes are actively updated and those in PHPCSUtils are cross-version compatible with multiple PHPCS versions, it can help to use those arrays instead of building your own (or to build your own by combining some of the available arrays with some extra tokens).
  • Looking at the loop - I'm wondering why you are using an error counter.
    The if ($errorsCounter > 0) { condition seems to imply that any "error" encountered in the loop is sufficient for the sniff to throw the error.
    If that's the case, $errorCounter before the loop could be changed to $isError = false, $errorsCounter++; in the loop to $isError = true; break; and then the condition to if ($isError === true).
    I know you will probably have choosen to use the counter for a reason, so what am I missing here ?
  • Regarding the error message:
    • As this sniff will only ever throw an error, you can go ahead and use the $phpcsFile->addError() method directly.
      The addMessage() method is only there for convenience when a sniff throws either a warning (deprecated) or an error (removed) with some complicated logic to determine which and is not needed here.
    • As for the error message itself, messages for new features in PHPCompatibility generally follow a pattern like .... is not allowed/supported/available in PHP x.x. or earlier, which is this case would translate to something along the lines of "Using throw as an expression is not supported in PHP 7.4 or earlier."

@jrfnl
Copy link
Member

jrfnl commented May 14, 2024

@coderimus Do you still intend to finish this PR or should this be closed ? (and unclaimed in the PHP 8.0 ticket)

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.

None yet

2 participants