Completed
Push — master ( b2cd26...6b1baa )
by ANTHONIUS
13s queued 11s
created

CoveragePass   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 36.1%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 58
ccs 13
cts 36
cp 0.361
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processDriver() 0 9 2
A processFilter() 0 13 3
A process() 0 4 1
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 1
    public function process(ContainerBuilder $container)
14
    {
15 1
        $this->processDriver($container);
16 1
        $this->processFilter($container);
17
    }
18
19 1
    private function processDriver(ContainerBuilder $container)
20
    {
21 1
        $driverClass = $container->get('runtime')->getDriverClass();
22 1
        $container->getParameterBag()->set('coverage.driver.class', $driverClass);
23
24 1
        $r = new \ReflectionClass($driverClass);
25 1
        if($r->hasMethod('setFilter')){
26
            $definition = $container->getDefinition('coverage.filter');
27
            $definition->addMethodCall('setFilter',[new Reference('coverage.filter')]);
28
        }
29
    }
30
31 1
    private function processFilter(ContainerBuilder $container)
32
    {
33 1
        $config     = $container->getParameter('config.filter');
34 1
        $definition = $container->getDefinition('coverage.filter');
35 1
        $basePath = getcwd();
36
37 1
        foreach ($config as $options) {
38
            $options['basePath'] = $basePath;
39
            $this->filterWhitelist($definition, $options, 'add');
40
            $exclude = $options['exclude'];
41
            foreach ($exclude as $item) {
42
                $item['basePath'] = $basePath;
43
                $this->filterWhitelist($definition, $item, 'remove');
44
            }
45
        }
46
    }
47
48
    private function filterWhitelist(Definition $definition, $options, $method)
49
    {
50
        $basePath  = $options['basePath'];
51
        $suffix    = $options['suffix'] ?: '.php';
52
        $prefix    = $options['prefix'] ?: '';
53
        $type      = $options['directory'] ? 'directory' : 'file';
54
        $directory = $basePath.\DIRECTORY_SEPARATOR.$options['directory'];
55
        $file      = $basePath.\DIRECTORY_SEPARATOR.$options['file'];
56
57
        if (preg_match('/\/\*(\..+)/', $directory, $matches)) {
58
            $suffix    = $matches[1];
59
            $directory = str_replace($matches[0], '', $directory);
60
        }
61
62
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
63
        if ('directory' === $type) {
64
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
65
        }
66
67
        if ('file' === $type) {
68
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
69
        }
70
    }
71
}
72