-
Bug
-
Resolution: Fixed
-
Minor
-
3.8, 3.9
-
MOODLE_38_STABLE, MOODLE_39_STABLE
-
MOODLE_38_STABLE
-
Nesting ternary operators without explicit parentheses is deprecated:
// Code like
|
$a ? $b : $c ? $d : $e
|
// should be replaced by (current interpretation)
|
($a ? $b : $c) ? $d : $e
|
// or (likely intended interpretation)
|
$a ? $b : ($c ? $d : $e)
|
So this is about to review all the "nested" ternary operators in core, adding the the "current interpretation" parenthesis if missing. We are not changing any behavior here, just making the interpretation explicit.
And yes, the "current interpretation is the "crazy one" (left->right). That's the way PHP works.
<?php
|
$a = -1;
|
echo $a < 0 ? "-1" : $a > 0 ? "+1" : "0";
|
|
+1 (were you expecting a -1, nah!)
|
Here there is the list of files having likely nested ternary operators, calculated with:
$ ag '\?[^:;=]+:[^\?;=]+\?[^:;=]+:[^;]+;' --php -l
|
As far as there are some false positives there, let's use the php semantic searcher to, accurately, look for nested ternaries. Those will be our target for this issue:
ag '\?[^:;=]+:[^\?;=]+\?[^:;=]+:[^;]+;' --php -l | \
|
xargs ../php-search/bin/php-search -e "ternary / ternary" | \
|
grep "found." | sort | uniq
|
46 matches. Here it's the list, let's review it: https://pastebin.com/e7U123MS