Application of a finite state machine

I apply the state machine pattern generator I came across at Forum PHP to my latest project.

The refactoring issue

I’ve fiddled with an argument handling PHP 5.2 compatible library lately and, willing to expand its functions after refactoring the code, I’ve tried to see if Sebastian Bergmann’s finite state machine generator could fit.
It does like a charm.
The core of the library is the assert() method and it actually reads like

public function assert( $condition, $reason ) {
    if ( $this->check->is_failed() ) {
        $this->throw_exception( $reason );

    if ( ! $condition ) {
        $this->reason = $reason;
    } else {

    return $this;

which is as clean as I can make it; I can read it easily, grasp the sense of the code and have a glimpse of what’s happening under the hood.
While that’s not adding anything to the code it’s a solid base I can build on and the automatic code generator that comes with the state project really made the tedious work for me allowing me to focus on the, here small, design.
Since the XML specification I’ve used to generate the code used is not pushed here it is in its simple splendor:

<?xml version="1.0" encoding="UTF-8"?>
<class name="tad_Arg_Check"/>
<interface name="tad_Arg_Check_State"/>
<abstractClass name="tad_Arg_Check_AbstractState"/>
<state name="tad_Arg_Check_PassingState" query="is_passing"/>
<state name="tad_Arg_Check_FailingState" query="is_failing"/>
<state name="tad_Arg_Check_OrFailingState" query="is_or_failing"/>
<state name="tad_Arg_Check_FailedState" query="is_failed"/>
<transition from="tad_Arg_Check_PassingState" to="tad_Arg_Check_PassingState" operation="pass"/>
<transition from="tad_Arg_Check_PassingState" to="tad_Arg_Check_PassingState" operation="or_condition"/>
<transition from="tad_Arg_Check_PassingState" to="tad_Arg_Check_FailingState" operation="fail"/>
<transition from="tad_Arg_Check_FailingState" to="tad_Arg_Check_FailedState" operation="fail"/>
<transition from="tad_Arg_Check_FailingState" to="tad_Arg_Check_FailedState" operation="pass"/>
<transition from="tad_Arg_Check_FailingState" to="tad_Arg_Check_OrFailingState" operation="or_condition"/>
<transition from="tad_Arg_Check_OrFailingState" to="tad_Arg_Check_FailedState" operation="fail"/>
<transition from="tad_Arg_Check_OrFailingState" to="tad_Arg_Check_OrFailingState" operation="or_condition"/>
<transition from="tad_Arg_Check_OrFailingState" to="tad_Arg_Check_PassingState" operation="pass"/>
<transition from="tad_Arg_Check_FailedState" to="tad_Arg_Check_FailedState" operation="fail"/>
<transition from="tad_Arg_Check_FailedState" to="tad_Arg_Check_FailedState" operation="pass"/>
<transition from="tad_Arg_Check_FailedState" to="tad_Arg_Check_OrFailingState" operation="or_condition"/>
<operation name="pass" allowed="canPass" disallowed="cannotPass"/>
<operation name="fail" allowed="canFail" disallowed="cannotFail"/>
<operation name="or_condition" allowed="canApplyOrCondition" disallowed="cannotApplyOrCondition"/>

Have a look

Sebastian Bergmann is the developer that made PHPUnit and he made an inspiring presentation that moved something in the developer I am for its plain simplicity and strong logic and a read through his repos is a lesson in itself.

I appreciate your input