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

PHP 7.3: New DiscouragedSwitchContinue sniff #687

Merged
merged 1 commit into from Jul 13, 2018

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 9, 2018

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:

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. Cases with unscoped control structures will be ignored by this sniff. Cases 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

As this issue will start breaking build for anyone running unit tests against PHP nightly, I've targeted the 8.2.0 release for this sniff.

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
@jrfnl jrfnl added this to the 8.2.0 milestone Jul 9, 2018
@jrfnl jrfnl requested a review from wimg July 9, 2018 03:15
@jrfnl jrfnl changed the title ✨ PHP 7.3: New DiscouragedSwitchContinue sniff PHP 7.3: New DiscouragedSwitchContinue sniff Jul 9, 2018
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.

PHP 7.3: using continue within switch - warning for something not deprecated ?
2 participants