Ad hoc adapter classes 07

Setting up to intercept file inclusion in PHP.

Reconnecting

In my personal crusade to bring easy testing tools to WordPress projects, while not moving from its minimum requirements of PHP 5.2, I’ve been tinkering and playing with the idea of a magic script (or whatever) that will allow wrapping calls to WordPress globally defined functions and variables into mockable and testable objects; the struggle for such a tool has this as its latest chapter.
Willing to do things the WordPress way, through a plugin if possible, I’ve begun building a developer oriented plugin, here the post and here the plugin, to have a place to make these ideas live.

The problem

A tool wishing to scan a theme or plugin file to find calls to WordPress defined functions will use a list of known used functions (maybe generated using the dump function of the plugin above) to parse the code and change any call to those from something like

add_filter('filter_tag', array( $this , 'a_function') );

to

$this->adapter->add_filter('filter_tag', array( $this , 'a_function') );

which is not an extraordinary feat if done using regexes as an example.
But PHP is a dynamic language and calls to dynamic functions like this

$action = $post_meta ? 'update' : 'add';
$action . '_post_meta'(...);

are common place. While code like

if ( $post_meta ){
    update_post_meta(...);
} else {
    add_post_meta(...);
}

would be better I’m not going to be that opinionated about my code and I’m not trying to come up with a regex capable of matching the first call.
That call will be but defined at run time and one or both methods will be called with their full name.

A confused idea

While not still sure about the whole process I’m thinking about a stream wrapper like Patchwork to log calls to WordPress functions made by the target code.
That would allow me to create ad-hoc adapters taking dynamic function calls into account and that would only require the target code to run.
The other side of the ad-hoc adapter generation, though, remains obscure and will require more inquiry on my side.

Next

I will be setting up a small test environment to test the stream wrapper idea and make some experiments.

I appreciate your input