Merge Clover XML Files

We have a single git repository with a large project that is split into two separate PHP application (api and client). We use travis for running tests and deploying to production and CodeClimate to keep an eye on our code quality.

The team behind is relatively new to the project (after an acquisition and the most of the old team resigning just before that happened).

The team has started to do unit testing (and tdd) and as a consequence wanted to have code coverage metrics available as well.

The problem is that the code metrics are generated by PHPUnit per subproject - and CodeClimate needs a single clover.xml file.

How to Merge Clover XML Files

Basically we stole the idea from: - however this solution did not give us the correct nodes - and not all children.

Instead we combined that idea with this idea: - which allows us to copy a complete node with all it’s children from one document to another.

The complete scripts looks like this:

$lobjArgs = $_SERVER['argv'];
$lobjFiles = preg_grep("/^((?!.*(combine\.php)).*)/", $lobjArgs);
$lobjFiles = preg_grep('/^((?!(--output)).*)/',$lobjFiles);
$lobjFiles = array_values($lobjFiles);
$lobjOptions = getopt('',array('output:'));
if(count($lobjFiles) >= 2)
    $lobjXmlAggregate = simplexml_load_file($lobjFiles[0]);
    $domAggregate = dom_import_simplexml($lobjXmlAggregate->project[0]);
    $i = 1;
    while($i < count($lobjFiles))
        $lobjXmlNext = simplexml_load_file($lobjFiles[$i]);
        foreach($lobjXmlNext->project[0]->package as $package)
            $domPackage  = dom_import_simplexml($package);
            $domPackage = $domAggregate->ownerDocument->importNode($domPackage, TRUE);
    $lobjFileHandle = fopen($lobjOptions['output'],'w') or die("can't open file phpcs-merged.xml");
    fwrite($lobjFileHandle, $lobjXmlAggregate->asXML());
echo "Merge complete";