Passed
Pull Request — master (#15)
by ANTHONIUS
03:07
created

CoveragePass::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 4
cp 0.75
crap 1.0156
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Bridge\CodeCoverage\Compiler;
15
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
class CoveragePass implements CompilerPassInterface
22
{
23 2
    public function process(ContainerBuilder $container)
24
    {
25 2
        $this->processDriver($container);
26 2
        $this->processFilter($container);
27
    }
28
29 2
    private function processDriver(ContainerBuilder $container)
30
    {
31 2
        $driverClass = $container->get('runtime')->getDriverClass();
32 2
        $container->getParameterBag()->set('coverage.driver.class', $driverClass);
33
34 2
        $r = new \ReflectionClass($driverClass);
35 2
        if ($r->hasMethod('setFilter')) {
36
            $definition = $container->getDefinition('coverage.filter');
37
            $definition->addMethodCall('setFilter', [new Reference('coverage.filter')]);
38
        }
39
    }
40
41 2
    private function processFilter(ContainerBuilder $container)
42
    {
43 2
        $config     = $container->getParameter('config.filter');
44 2
        $definition = $container->getDefinition('coverage.filter');
45
46 2
        foreach ($config as $options) {
47 1
            $options['basePath'] = '';
48 1
            $this->filterWhitelist($definition, $options, 'add');
49 1
            $exclude = $options['exclude'];
50 1
            foreach ($exclude as $item) {
51
                $item['basePath'] = '';
52
                $this->filterWhitelist($definition, $item, 'remove');
53
            }
54
        }
55
    }
56
57 1
    private function filterWhitelist(Definition $definition, $options, $method)
58
    {
59 1
        $suffix    = $options['suffix'] ?: '.php';
60 1
        $prefix    = $options['prefix'] ?: '';
61 1
        $directory = $options['directory'];
62 1
        $file      = $options['file'];
63 1
        $type      = !is_null($options['directory']) ? 'directory' : 'file';
64
65 1
        if(!is_null($directory) && "" === $directory){
66
            $directory = getcwd();
67
        }
68
        
69
        if (
70 1
            !is_null($directory)
71 1
            && preg_match('/\/\*(\..+)/', $directory, $matches)
72
        ) {
73
            $suffix    = $matches[1];
74
            $directory = str_replace($matches[0], '', $directory);
75
        }
76
77 1
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
78 1
        if ('directory' === $type) {
79 1
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
80
        }
81
82 1
        if ('file' === $type) {
83
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
84
        }
85
    }
86
}
87