PHP 7.3: New DiscouragedSwitchContinue sniff#687
Merged
Conversation
As of PHP 7.3, using `continue` to target `switch` will start throwing an `E_WARNING`. > . "continue" statements targeting "switch" control flow structures will now > generate a warning. In PHP such "continue" statements are equivalent to > "break", while they behave as "continue 2" in other languages. Refs: * https://github.com/php/php-src/blob/04e3523b7d095341f65ed5e71a3cac82fca690e4/UPGRADING#L47-L58 * php/php-src@04e3523 * https://wiki.php.net/rfc/continue_on_switch_deprecation This PR adds a new sniff to detect this. Notes: * This sniff runs into the same difficulties with unscoped control structures as the `ForbiddenBreakContinueOutsideLoop` sniff. For this sniff, I've elected to prevent the false positives which plague the other sniff, though this does mean that the sniff will underreport (false negatives) for `continue` targeting `switch` when nested control structures without braces are encountered. This underreporting is minimized by examining each `case` individually. `Case`s with unscoped control structures will be ignored by this sniff. `Case`s within the same `switch` _without_ unscoped control structures will be handled correctly. * Due to differences in the tokenizer array in the various PHPCS versions, this new sniff will only work on PHPCS 2.3.0+. The minimum PHPCS requirement in Composer has been adjusted to reflect this. As PHPCS 2.3.0 was released over three years ago, I would expect this not to be an issue. * Includes one fix for an instance in PHPCompatibility itself which is affected by this PHP 7.3 change (in a copy of an upstream PHPCS method). * Note: PHPCS 3.x is also affected by this and until the PR I've pulled upstream to fix this has been merged, the build for PHPCompatibility against PHPCS 3.x on PHP `nightly` will fail. Fixes 686
DiscouragedSwitchContinue sniffDiscouragedSwitchContinue sniff
This was referenced Jul 9, 2018
wimg
approved these changes
Jul 13, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As of PHP 7.3, using
continueto targetswitchwill start throwing anE_WARNING.Refs:
This PR adds a new sniff to detect this.
Notes:
ForbiddenBreakContinueOutsideLoopsniff.For this sniff, I've elected to prevent the false positives which plague the other sniff, though this does mean that the sniff will underreport (false negatives) for
continuetargetingswitchwhen nested control structures without braces are encountered.This underreporting is minimized by examining each
caseindividually.Cases with unscoped control structures will be ignored by this sniff.Cases within the sameswitchwithout unscoped control structures will be handled correctly.The minimum PHPCS requirement in Composer has been adjusted to reflect this.
As PHPCS 2.3.0 was released over three years ago, I would expect this not to be an issue.
nightlywill fail.Fixes #686
As this issue will start breaking build for anyone running unit tests against PHP
nightly, I've targeted the8.2.0release for this sniff.