Repository navigation
Give the right operand of a multi-line && its own branch - #1407
sebastianbergmann merged 2 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1407 +/- ##
=========================================
Coverage 99.61% 99.61%
- Complexity 2767 2780 +13
=========================================
Files 141 141
Lines 8542 8566 +24
=========================================
+ Hits 8509 8533 +24
Misses 33 33 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
And with PHP < 8.6, I assume? Would be interesting to see how the bytecode-based analysis we use on |
Yes, it ran on 8.5.12. I just retested it with 8.6 and the results are:
|
|
Thank you for this. I merged your two commits into While reviewing this, I ran the change with Xdebug 3.6.0alpha1 and PCOV 1.0.12 (with and without the optimizer of OPcache) on PHP 8.6.0 and found cases in which a line that was executed was reported as not executed, or was no longer reported at all. In all of them, the line of an operand carries no opcode of its own and could no longer get its status from the other lines of the statement after it was split off. The grouping is now kept in these cases. Operands that contain no variable (f243742)
return \defined('CURL_VERSION_SSL') // was reported as not executed
&& $version !== null
&& 0 !== (\CURL_VERSION_SSL & $version['features']);return \function_exists('pcntl_fork') // PCOV with the optimizer of OPcache:
&& \function_exists('pcntl_waitpid') // these two lines were reported as not executed
&& \function_exists('posix_kill');The line of the right operand is now only split off when both operands contain a variable (or a static property). Expressions that are evaluated before they are compiled (6348781, dbd4f74)The compiler tries to evaluate the elements of an array literal, the conditions of the arms of a return match (true) {
$id !== null
&& $enabled !== null => [ // was reported as not executed
'id' => $id,
],
default => [],
};return json_encode(['both' => ($a !== null
&& $b !== null) // was reported as not executed
? 'yes'
: 'no']);Nothing is split inside these expressions now. The body of a closure or of an arrow function inside them is compiled separately and is still split. This made the marking of the operands of a The line of the right operand was grouped with other lines before (83b9032)When the line of the right operand is the first line of the body of an arrow function, or of an arm of a ternary operator or of a $check = static fn (int $i): bool => $i > 0 && (
$i < 10 || $i === 99
); // was reported as not executedThe line is now only split off when it is still in the same branch as the last line of the left operand. Functions and methods that the driver did not report (e0dcd2a)This one was not introduced by your change, but your change made it visible in more cases. With Xdebug 3.6, the lines of a function that was compiled before the collection of code coverage data was started are not reported until the function is executed. Marking the line of a function call in such a function as not executed kept its other lines from being seeded from static analysis, and they were missing from the report. What Xdebug and PCOV reportFor the example from the description, nothing changes with Xdebug and PCOV: after calling the function only with return $x !== null
&& count($x) > 1 // also holds the JMPZ_EX of the && that follows
&& $x[0] === 'a'; // also holds VERIFY_RETURN_TYPE and RETURNThese opcodes are executed when the operand on their line is not evaluated. With Xdebug and PCOV, only the line of the last operand of an assignment or of an expression statement is reported as not executed. PCOV reports more of these lines as not executed when the optimizer of OPcache is enabled. The attribution of With Xdebug, the line of a right operand such as |
|
Thank you for the merge and for your report - I used your snippets to improve our tests |
…pile time (#29) The review of sebastianbergmann/php-code-coverage#1407 found shapes of a multi-line && where a line of an operand has no opcode of its own. PHP evaluates such an operand when it compiles the file. Examples are `\defined('NAME')`, `\PHP_VERSION_ID >= 80100` and a && in a match arm condition, in an array item or in a static initializer. A driver must not report such a line as not executed when the statement runs. No test had these shapes. The new fixture has one function for each shape. The tests print the executable lines of each function and their status, without OPcache and with OPcache. turbocov gives the correct result for each shape, thus the C code does not change.
|
A follow-up, because shipmonk-rnd/turbocov#18 made me look at the other operators that short-circuit.
|
| lines | |
|---|---|
| before this pull request | 34 |
| with this pull request | 36 |
main |
39 |
The other 8 lines:
- 7 lines hold nothing but a variable that is an operand in the condition of an
iforelseif(|| $b,&& $c). The analysis of the syntax does not classify them as executable, so they are not in the report at all. I have not changed that. - 1 line is in an element of an array literal (
?? 'none'). The grouping is kept there, for the reason that I described above: PHP evaluates constants in array literals before it compiles them and then attributes opcodes to other lines.
When the executable lines are determined from bytecode, each line has a status of its own, as you already found.
Where the grouping is kept because of Xdebug
There is one more limit, and it is not caused by turbocov. Xdebug does not report the jump of || (JMPNZ_EX) and the jump of ?-> (JMP_NULL) as executed. A line on which no other opcode is executed is reported as not executed although it was executed:
return match ($flag) {
true => $a // Xdebug: not executed, although $a || $b was evaluated
|| $b,
default => false,
};The same holds when the line ends with isset(), a static property, or clone, or with a property fetch or an array access that PHP reads without warnings. Xdebug 3.5.3 and 3.6.0alpha1 behave the same; PCOV reports these lines, and the jump of && (JMPZ_EX) is reported by both. I will report this to Xdebug.
php-code-coverage has hidden this so far because such a line shares its status with the line that follows it. The right operand of ||, and the member after ?->, therefore only get a status of their own when a statement starts on the line before them, or when the expression before the operator is compiled to an opcode that Xdebug reports and that is executed whenever that line is reached (a function call or a comparison, for instance). In the example above, the two lines of the arm still share one status. The analysis of the syntax does not know which driver is used, so this also applies when the data comes from turbocov.
It would be good to know whether the numbers above hold with the data that turbocov really reports. If you compare a report of turbocov with main, I would be interested in the lines that are still shown as covered although turbocov reports them as not executed.
Hi, I'm trying to improve the precision of coverage reports in turbocov, our new coverage driver. I found that one improvement cannot reach the report, because of how php-code-coverage groups lines by branch.
This PR fixes multi-line
&&.Today these three lines are one branch, so they always share one status. With this PR, the line of each right operand gets its own branch.
I tested it with Xdebug 3.5.3, PCOV 1.0.12 and turbocov.