Passed
Pull Request — master (#18)
by ANTHONIUS
03:39
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

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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 <https://itstoni.com>
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 37
    public function process(ContainerBuilder $container)
24
    {
25 37
        $this->processDriver($container);
26 37
        $this->processFilter($container);
27
    }
28
29 37
    private function processDriver(ContainerBuilder $container)
30
    {
31 37
        $driverClass = $container->get('runtime')->getDriverClass();
32 37
        $container->getParameterBag()->set('coverage.driver.class', $driverClass);
33
34 37
        $r = new \ReflectionClass($driverClass);
35 37
        if ($r->hasMethod('setFilter')) {
36
            $definition = $container->getDefinition('coverage.filter');
37
            $definition->addMethodCall('setFilter', [new Reference('coverage.filter')]);
38
        }
39
    }
40
41 37
    private function processFilter(ContainerBuilder $container)
42
    {
43 37
        $config     = $container->getParameter('config.filter');
44 37
        $definition = $container->getDefinition('coverage.filter');
45
46 37
        foreach ($config as $options) {
47 14
            $this->filterWhitelist($definition, $options, 'add');
48 14
            $exclude = $options['exclude'];
49 14
            foreach ($exclude as $item) {
50
                $this->filterWhitelist($definition, $item, 'remove');
51
            }
52
        }
53
    }
54
55 14
    private function filterWhitelist(Definition $definition, $options, $method)
56
    {
57 14
        $suffix    = $options['suffix'] ?: '.php';
58 14
        $prefix    = $options['prefix'] ?: '';
59 14
        $directory = $options['directory'];
60 14
        $file      = $options['file'];
61 14
        $type      = null !== $options['directory'] ? 'directory' : 'file';
62
63 14
        if (null !== $directory && '' === $directory) {
64
            $directory = getcwd();
65
        }
66
67
        if (
68 14
            null !== $directory
69 14
            && preg_match('/\/\*(\..+)/', $directory, $matches)
70
        ) {
71
            $suffix    = $matches[1];
72
            $directory = str_replace($matches[0], '', $directory);
73
        }
74
75 14
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
76 14
        if ('directory' === $type) {
77 8
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
78
        }
79
80 14
        if ('file' === $type) {
81 6
            $files = [$file];
82
83 6
            if (false !== ($pos=strpos($file, '*'))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $pos is dead and can be removed.
Loading history...
84
                $files = [];
85
                foreach (glob($file) as $filename) {
86
                    $filename = realpath($filename);
87
                    $files[] = $filename;
88
                }
89
            }
90 6
            foreach($files as $file){
91 6
                $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
92
            }
93
        }
94
    }
95
}
96