Passed
Pull Request — master (#3)
by ANTHONIUS
04:17
created

CoveragePass   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 57
ccs 28
cts 35
cp 0.8
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processDriver() 0 9 2
A process() 0 4 1
A processFilter() 0 12 3
B filterWhitelist() 0 21 8
1
<?php
2
3
4
namespace Doyo\Bridge\CodeCoverage\Compiler;
5
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class CoveragePass implements CompilerPassInterface
12
{
13 15
    public function process(ContainerBuilder $container)
14
    {
15 15
        $this->processDriver($container);
16 15
        $this->processFilter($container);
17
    }
18
19 15
    private function processDriver(ContainerBuilder $container)
20
    {
21 15
        $driverClass = $container->get('runtime')->getDriverClass();
22 15
        $container->getParameterBag()->set('coverage.driver.class', $driverClass);
23
24 15
        $r = new \ReflectionClass($driverClass);
25 15
        if($r->hasMethod('setFilter')){
26
            $definition = $container->getDefinition('coverage.filter');
27
            $definition->addMethodCall('setFilter',[new Reference('coverage.filter')]);
28
        }
29
    }
30
31 15
    private function processFilter(ContainerBuilder $container)
32
    {
33 15
        $config     = $container->getParameter('config.filter');
34 15
        $definition = $container->getDefinition('coverage.filter');
35
36 15
        foreach ($config as $options) {
37 1
            $options['basePath'] = "";
38 1
            $this->filterWhitelist($definition, $options, 'add');
39 1
            $exclude = $options['exclude'];
40 1
            foreach ($exclude as $item) {
41
                $item['basePath'] = "";
42
                $this->filterWhitelist($definition, $item, 'remove');
43
            }
44
        }
45
    }
46
47 1
    private function filterWhitelist(Definition $definition, $options, $method)
48
    {
49 1
        $basePath  = $options['basePath'];
0 ignored issues
show
Unused Code introduced by
The assignment to $basePath is dead and can be removed.
Loading history...
50 1
        $suffix    = $options['suffix'] ?: '.php';
51 1
        $prefix    = $options['prefix'] ?: '';
52 1
        $type      = $options['directory'] ? 'directory' : 'file';
53 1
        $directory = $options['directory'];
54 1
        $file      = $options['file'];
55
56 1
        if (preg_match('/\/\*(\..+)/', $directory, $matches)) {
57
            $suffix    = $matches[1];
58
            $directory = str_replace($matches[0], '', $directory);
59
        }
60
61 1
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
62 1
        if ('directory' === $type) {
63 1
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
64
        }
65
66 1
        if ('file' === $type) {
67
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
68
        }
69
    }
70
}
71