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
I've published a new blog post: "Human Creations", on the difference in content generation by LLMs, and the creation of text, art and code by humans.
You can find it at https://derickrethans.nl/human-creations.html or at @blog
RE: https://phpc.social/@afilina/116274024588235234
It's good to see that more and more people are realising that the Web can be for-good, without all the enshittification.
That's why I'm happy to see endeavours like phpc.tv springing up, and helping out where I can.
Taking back the control of how the Web is for people, by people, without big tech making it all shit.
Created a waste_basket; Updated 5 crossings and a bicycle_parking
I walked 10.7km in 2h35m10s
If you're in the UK (south, preferably), the International Space Station is going to be visible straight above you in about half an hour!
If you're further north than London, it moves further to the south.
Updated 2 crossings
Created 3 crossings, 2 waste_baskets, and 2 trees; Updated a crossing
Created 2 post_boxes and a crossing; Updated a newsagent shop
Updated a crossing
Updated a post_box
Created a bench; Updated a bench
Created an information; Updated 2 benches
Updated a crossing and a supermarket shop
Confirmed a restaurant
I hiked 18.8km in 3h50m35s
Updated a pub
Updated a restaurant, a pub, and a toilet
Created a butcher shop and a restaurant; Updated a restaurant; Confirmed a bakery shop and a restaurant
I walked 10.8km in 1h44m13s
Merged pull request #1069
Bump versions for CI and release scripts
Merged pull request #1067
Update GitHub actions
Merge branch 'xdebug_3_5'
Merged pull request #1068


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