Skip to content

PHP 7.4/8.0: new RemovedTernaryAssociativity sniff#810

Merged
wimg merged 3 commits intomasterfrom
php-7.4/deprecated-nested-ternaries-without-parentheses
Jun 3, 2019
Merged

PHP 7.4/8.0: new RemovedTernaryAssociativity sniff#810
wimg merged 3 commits intomasterfrom
php-7.4/deprecated-nested-ternaries-without-parentheses

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 2, 2019

PHP 7.4 deprecates nesting of ternaries without explicit use of parentheses.
PHP 8.0 will remove the (left-)associativity of ternaries completely. This will become a compile-time error.

Refs:

Includes unit tests.

To test this sniff, I have run it over some 200.000 files. While I can't say for sure whether there were any missed problem cases (false negatives), all the issues detected by the sniff were actual cases of nested ternaries which would become problematic in PHP 7.4/8.0 (and most, if not all, were bugs).

Related to #808


This PR includes adding a new Sniff::isShortTernary() method to determine whether a ternary is a short ternary, i.e. without the middle.

This method will 🤞 also be introduced into PHPCS itself in version 3.5.0.

Upstream, the method will be pulled with unit tests. As the intention is for this method to primarily be hosted within PHPCS, it seemed redundant to duplicate those here.

Refs:

This PR also implements the use of this new Sniff::isShortTernary() method in the existing NewShortTernary sniff.

jrfnl added 2 commits June 2, 2019 20:21
... to determine whether a ternary is a short ternary, i.e. without the middle.

This method will :fingers_crossed: also be introduced into PHPCS itself in version 3.5.0.

Upstream, the method will be pulled with unit tests. As the intention is for this method to primarily be hosted within PHPCS, it seemed redundant to duplicate those here.

Refs:
* squizlabs/PHP_CodeSniffer#2456
PHP 7.4 deprecates nesting of ternaries without explicit use of parentheses.
PHP 8.0 will remove the (left-)associativity of ternaries completely. This will become a compile-time error.

Refs:
* https://wiki.php.net/rfc/ternary_associativity
* php/php-src@09ea55c (PHP 7.4 deprecation)
* https://gist.github.com/nikic/b6214f87b0e4a7c6fe26919ac849194f

Includes unit tests.

To test this sniff, I have run it over some 200.000 files. While I can't say for sure whether there were any missed problem cases (false negatives), all the issues detected by the sniff were actual cases of nested ternaries which would become problematic in PHP 7.4/8.0.

Related to 808
@jrfnl jrfnl force-pushed the php-7.4/deprecated-nested-ternaries-without-parentheses branch from 8be24c8 to d6d7ba4 Compare June 2, 2019 21:47
@wimg wimg merged commit a1d0178 into master Jun 3, 2019
@delete-merged-branch delete-merged-branch bot deleted the php-7.4/deprecated-nested-ternaries-without-parentheses branch June 3, 2019 15:04
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