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
base: develop
Are you sure you want to change the base?
[WIP] PHP 8.0: the throw statement as an expression #1275
Conversation
There was a problem hiding this 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 thePHPCSUtils\BackCompat\BCFile
class.
You'll probably want to use thefindStartOfStatement()
andfindEndOfStatement()
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
andT_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
ortoken => token
pattern for these type of token arrays and then to useisset()
instead ofin_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 PHPCSUtilsPHPCSUtils\BackCompat\BCTokens
, as well as thePHPCSUtils\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).
- Tokens like
- Looking at the loop - I'm wondering why you are using an error counter.
Theif ($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 toif ($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.
TheaddMessage()
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 "Usingthrow
as an expression is not supported in PHP 7.4 or earlier."
- As this sniff will only ever throw an error, you can go ahead and use the
@coderimus Do you still intend to finish this PR or should this be closed ? (and unclaimed in the PHP 8.0 ticket) |
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:
RFC: PHP RFC: throw expression
Implementation: functionalituy implementation
Related to #809