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

CoveragePass   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 34.29%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 57
ccs 12
cts 35
cp 0.3429
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 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
36 1
        foreach ($config as $options) {
37
            $options['basePath'] = "";
38
            $this->filterWhitelist($definition, $options, 'add');
39
            $exclude = $options['exclude'];
40
            foreach ($exclude as $item) {
41
                $item['basePath'] = "";
42
                $this->filterWhitelist($definition, $item, 'remove');
43
            }
44
        }
45
    }
46
47
    private function filterWhitelist(Definition $definition, $options, $method)
48
    {
49
        $basePath  = $options['basePath'];
0 ignored issues
show
Unused Code introduced by
The assignment to $basePath is dead and can be removed.
Loading history...
50
        $suffix    = $options['suffix'] ?: '.php';
51
        $prefix    = $options['prefix'] ?: '';
52
        $type      = $options['directory'] ? 'directory' : 'file';
53
        $directory = $options['directory'];
54
        $file      = $options['file'];
55
56
        if (preg_match('/\/\*(\..+)/', $directory, $matches)) {
57
            $suffix    = $matches[1];
58
            $directory = str_replace($matches[0], '', $directory);
59
        }
60
61
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
62
        if ('directory' === $type) {
63
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
64
        }
65
66
        if ('file' === $type) {
67
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
68
        }
69
    }
70
}
71