Passed
Push — master ( 146be5...1e12ef )
by ANTHONIUS
06:12
created

CoveragePass::filterWhitelist()   C

Complexity

Conditions 13
Paths 96

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 24.5711

Importance

Changes 0
Metric Value
cc 13
eloc 22
nc 96
nop 3
dl 0
loc 37
ccs 13
cts 22
cp 0.5909
crap 24.5711
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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