Completed
Push — master ( 19fab1...ec2cfc )
by ANTHONIUS
23s queued 11s
created

CoveragePass   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 76.74%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 72
ccs 33
cts 43
cp 0.7674
rs 10
c 0
b 0
f 0
wmc 20

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processDriver() 0 9 2
A process() 0 4 1
A processFilter() 0 11 3
C filterWhitelist() 0 37 14
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 35
    public function process(ContainerBuilder $container)
24
    {
25 35
        $this->processDriver($container);
26 35
        $this->processFilter($container);
27
    }
28
29 35
    private function processDriver(ContainerBuilder $container)
30
    {
31 35
        $driverClass = $container->get('runtime')->getDriverClass();
32 35
        $container->getParameterBag()->set('coverage.driver.class', $driverClass);
33
34 35
        $r = new \ReflectionClass($driverClass);
35 35
        if ($r->hasMethod('setFilter')) {
36
            $definition = $container->getDefinition('coverage.filter');
37
            $definition->addMethodCall('setFilter', [new Reference('coverage.filter')]);
38
        }
39
    }
40
41 35
    private function processFilter(ContainerBuilder $container)
42
    {
43 35
        $config     = $container->getParameter('config');
44 35
        $config = $config['filter'];
45 35
        $definition = $container->getDefinition('coverage.filter');
46
47 35
        foreach ($config as $options) {
48 14
            $this->filterWhitelist($definition, $options, 'add');
49 14
            $exclude = $options['exclude'];
50 14
            foreach ($exclude as $item) {
51
                $this->filterWhitelist($definition, $item, 'remove');
52
            }
53
        }
54
    }
55
56 14
    private function filterWhitelist(Definition $definition, $options, $method)
57
    {
58 14
        $suffix    = $options['suffix'] ?: '.php';
59 14
        $prefix    = $options['prefix'] ?: '';
60 14
        $directory = $options['directory'];
61 14
        $file      = $options['file'];
62 14
        $type      = null !== $options['directory'] ? 'directory' : 'file';
63
64 14
        if (null !== $directory && '' === $directory) {
65
            $directory = getcwd();
66
        }
67
68
        if (
69 14
            null !== $directory
70 14
            && preg_match('/\/\*(\..+)/', $directory, $matches)
71
        ) {
72
            $suffix    = $matches[1];
73
            $directory = str_replace($matches[0], '', $directory);
74
        }
75
76 14
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
77 14
        if ('directory' === $type) {
78 8
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
79
        }
80
81 14
        if ('file' === $type) {
82 6
            $files = [$file];
83
84 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...
85
                $files = [];
86
                foreach (glob($file) as $filename) {
87
                    $filename = realpath($filename);
88
                    $files[] = $filename;
89
                }
90
            }
91 6
            foreach($files as $file){
92 6
                $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
93
            }
94
        }
95
    }
96
}
97