DoesScience trait handles some of the boilerplate associated with performing experimental code refactoring using the PHP port of Scientist by Dayle Rees which is itself inspired by GitHub's Scientist.
<?phpclass myClass
{
publicfunctionmain()
{
$result = $this->oldCreakyMethod();
}
// ...
}Lets refactor oldCreakyMethod by starting from some boilerplate:
<?phpclass myClass
{
use \thinkspill\DoesScience;
publicfunctionmain()
{
/** boilerplate start */$control = function (...$vars) {
returncall_user_func_array([$this, 'oldCreakyMethod'], func_get_args());
};
$trial = function (...$vars) {
returncall_user_func_array([$this, 'newShinyMethod'], func_get_args());
};
$this->makeScienceExperiment('Experiment Name', $control);
$this->addExperimentTrial('trial 1', $trial);
print_r($this->experimentReport($arg1, $arg2));
/** boilerplate end */// $result = $this->oldCreakyFunction($arg1, $arg2);
}
privatefunctionoldCreakyMethod($arg1, $arg2)
{ // ...
}
privatefunctionnewShinyMethod($arg1, $arg2)
{ // ...
}
}The experiment boilerplate code gets copy/pasted into the code location that calls the method being refactored, and modified to call the correct control and trial methods.
The only call that needs to retain the actual arguments of the
method under refactor are those in the experimentReport call,
due to the use of variable arguments everywhere else.
The experimentReport method will return the results of each
experiment and if they matched.