Skip to content

Give the right operand of a multi-line && its own branch - #1407

Merged
sebastianbergmann merged 2 commits into
sebastianbergmann:mainfrom
janedbal:jn-boolean-operand-branches
Nov 21, 2026
Merged

sebastianbergmann merged 2 commits into
sebastianbergmann:mainfrom
janedbal:jn-boolean-operand-branches

Conversation

@janedbal

Copy link
Copy Markdown
Contributor

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 &&.

return $x !== null
    && count($x) > 1      // does not run when $x is null
    && $x[0] === 'a';     // does not run when $x is null

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.

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.61%. Comparing base (c5033f9) to head (9d9851b).
✅ All tests successful. No failed tests found.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sebastianbergmann

Copy link
Copy Markdown
Owner

I tested it with Xdebug 3.5.3, PCOV 1.0.12 and turbocov.

And with PHP < 8.6, I assume? Would be interesting to see how the bytecode-based analysis we use on main when PHP >= 8.6 is used works for that case.

@janedbal

Copy link
Copy Markdown
Contributor Author

And with PHP < 8.6, I assume?

Yes, it ran on 8.5.12.


I just retested it with 8.6 and the results are:

  • Bytecode mode: the output with and without the PR is byte-identical for all three drivers
  • Syntax mode (default): same effect like in 8.5

sebastianbergmann added a commit that referenced this pull request Nov 21, 2026
@sebastianbergmann
sebastianbergmann merged commit 9d9851b into sebastianbergmann:main Nov 21, 2026
21 checks passed
@sebastianbergmann

Copy link
Copy Markdown
Owner

Thank you for this. I merged your two commits into main as they are and made a number of changes on top of them. The change will be part of 14.6.0.

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)

isConstant() only recognised a literal, a constant, or a class constant. The compiler also evaluates expressions such as \PHP_VERSION_ID >= 80100, -1, and \defined('NAME'), and the optimizer of OPcache evaluates calls such as \function_exists('name').

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 match expression, and the initializer of a static variable before it compiles them. A constant that is substituted by that evaluation is no longer attributed to its own line, and neither is the opcode of the operator it is an operand of. The check for a && that is the value of an array element did not cover a && that is nested deeper in the element, in a condition of a match arm, or in a static initializer.

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 && as values of the array element unnecessary, so I removed it: it also took the branches away from the arms of a ternary operator in an operand of such 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 match expression, the other lines of that range got their status from it.

$check = static fn (int $i): bool => $i > 0 && (
    $i < 10 || $i === 99
);                                                     // was reported as not executed

The 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 report

For the example from the description, nothing changes with Xdebug and PCOV: after calling the function only with null, all three lines are still reported as executed.

return $x !== null
    && count($x) > 1      // also holds the JMPZ_EX of the && that follows
    && $x[0] === 'a';     // also holds VERIFY_RETURN_TYPE and RETURN

These 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 RETURN to the line of the last operand is the defect in PHP that is discussed in #1257.

With Xdebug, the line of a right operand such as && $callable() is reported as not executed when every execution of that call throws. The arms of a ternary operator and of a match expression already behave like this, so I left it as it is.

@janedbal

Copy link
Copy Markdown
Contributor Author

Thank you for the merge and for your report - I used your snippets to improve our tests

janedbal added a commit to shipmonk-rnd/turbocov that referenced this pull request Nov 21, 2026
…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.
@sebastianbergmann

Copy link
Copy Markdown
Owner

A follow-up, because shipmonk-rnd/turbocov#18 made me look at the other operators that short-circuit.

|| and ?-> on main

The line of the right operand of a || that spans multiple lines now has a status of its own, like the one of a && (af29773). So have the lines of a method call or property fetch after ?-> that starts on a line of its own (814e339):

assert($a
    || $b);                // no longer has the status of the line before it

return $a
    ?->getIterator()       // no longer has the status of the line before it
    ?->count();            // no longer has the status of the lines before it

The second one also corrects a line for Xdebug and PCOV: both report ?->getIterator() as not executed when $a is null, and php-code-coverage showed it as executed.

The cases of 027-skipped-operands.phpt

I ran the 32 cases of your test through php-code-coverage with the line data that the test expects: the lines that it lists as not executed, for the executable lines that PCOV reports. I did not run turbocov itself. The cases contain 47 lines whose code is skipped. When the executable lines are determined from the syntax, php-code-coverage shows this many of them as not executed:

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 if or elseif (|| $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.

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