Figuring Out Foreach
For a while I have been aware of some odd behaviour when you use branch coverage with PHP's foreach language construct. Usually with PHPUnit.
Take for example this bit of code:
<?php
function showLanguages(array $languages): void
{
foreach ($languages as $language) {
echo $language, "\n";
}
}
It seems that in order to cover all paths by test cases to pass in an array without any elements, and one with at least one element.
But no, when you run the analysis, you will see that there only two out of the three paths are covered:
Let's first actually analyse which opcodes, PHP's internal execution units, are generated by the engine:
line #* E I O op return operands
------------------------------------------------
2 0 E > RECV !0
4 1 EXT_STMT
2 > FE_RESET_R $2 !0, ->9
3 > > FE_FETCH_R $2, !1, ->9
5 4 > EXT_STMT
5 ECHO !1
6 EXT_STMT
7 ECHO '%0A'
4 8 > JMP ->3
9 > FE_FREE $2
7 10 EXT_STMT
11 > RETURN null
This output from VLD shows on line 4, two relevant opcodes: FE_RESET_R and FE_FETCH_R. Both of them can either followed by the next opcode on the list, or they can jump (->9) to opcode 9, which marks the end of the loop.
However from the diagram above, we don't see that path (the green one) being taken. Only the two paths that both continue with the next opcode (the red and blue lines). It seems that the emptiness of a normal array is checked by the FE_FETCH_R opcode.
Now the question is how to trigger the other path, so that 100% path (and branch) coverage can be reached.
This turns out harder than it is. I originally thought that this would be possible by trying to create a broken iterator ā for example one where you inherit from an internal class with a custom one, and not call the original constructor. But this creates an exception which pre-empts the engine from even running the rest of the function.
The only situation where I managed to do this was by creating an iterator that after a correct initialisation, had no items to return. An example of such as case is a DatePeriod iterator where the start date of the iterator is behind the end date:
$i1 = new DatePeriod(
new DateTimeImmutable("2025-01-14"),
DateInterval::createFromDateString("+1 day"),
new DateTimeImmutable("2025-01-01")
);
I had to change the definition of showLanguages too so that it accepts DatePeriod besides just array. But with that done, this specific iterator now lights up the green path:
There is currently an open bug in Xdebug's issue tracker to merge the branch analysis information for the two subsequent opcodes (FE_RESET_R and FE_FETCH_R).
I think I will now rather hide the jump away from the FE_RESET_R (and FE_RESET_RW) opcode.
But at last I now have an explanation as to where this phantom path came from.
Life Line
In more info innocent times, the hat riots caused some spankings...
100 Years Ago Men and Boys Fought on the Streets of New York Over Wearing Straw Hats Past Summer | The New York Public Library
https://www.nypl.org/blog/2022/09/23/straw-hat-riots-nycš· Mushroom Village
š© Weg van den Prins Willemsberg, Ellecom, Nederland
I walked 2.4km in 59m34s
š· National Cycle Network 6
š© Watford, United Kingdom
I walked 7.6km in 1h54m50s
š· Tufted Duck Pair
š© Outer Circle, City of Westminster, United Kingdom
RE: https://phpc.social/@Xdebug/115662135830755552
I have just released Xdebug 3.5.0!
In the next few weeks I will create some content (text, and perhaps video) highlighting some new features in more detail.
Please share it with the world!
The master branch is now for Xdebug 3.6, targetting PHP 8.6
Back to -dev
Tweak release instructions a little
Go with 3.5.0
Tweak message IDs and severities for control socket log entries
I walked 8.1km in 1h26m40s
I walked 1.1km in 9m28s
I walked 8.5km in 1h30m56s
My whisky of the month for December 2025, is a 15yo Aultmore bottled by Cadenhead's.
Fixed off-by-one error in address length name for control socket on Lā¦
Merged pull request #1050
Fixed control socket name by removing silly trailing things (by not pā¦
RE: https://en.osm.town/@harry_wood/115650834037247679
Fancy a friendly #OpenStreetMap chat in the pub, with some Christmas celebrations?
The London OSM gang is meeting on December 16th near Paddington.




Shortlink
This article has a short URL available: https://drck.me/fidgety-forach-jbc