The Structure Tree (or structree) has no preconceptions on launch about what phases the game has or in which order they are. This is by design, and permits mods to add or remove phases as they wish, with no modification to the structree code necessary.
The way the structree currently determines phase order is that if makes use of the fact that the required phase of tech A usually comes before the required phase of tech B, where tech B requires tech A to be researched first and the two phases are different.
This works, so long as a civ has several non-phase techs that each require at least one other non-phase tech to be researched first.
However, what if a civ doesn't have any non-phase techs that require another non-phase tech? That's what appears to have happened with the Zora civilisation from The Undying Nephalim's Hyrule Conquest mod.
This revision rewrites the unravel_phases function to work out the phase order from the phase-techs themselves, as they should be expected to always require each other.
As to why the function was not written this way originally:
- The phase-techs' actualPhase attribute (used below) is derived from the replaces attribute in the respective phase-tech's json template.
- The unravel_phases function follows the same basic logic as the same function of the original php/web-version. (link)
- The php version was written before the replaces attribute was added to json templates. (In rP16678.)