Passed
Pull Request — develop (#5)
by ANTHONIUS
10:30
created

CoveragePass::addCoverageOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-coverage-extension 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\Behat\Coverage\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 1
    public function process(ContainerBuilder $container)
24
    {
25 1
        $this->compileFilterOptions($container);
26 1
        $this->compileDrivers($container);
27
        $this->compileCoverageOptions($container);
28
29 1
        $definition = $container->getDefinition('doyo.coverage.dispatcher');
30
        $tagged     = $container->findTaggedServiceIds('doyo.dispatcher.subscriber');
31 1
32
        foreach ($tagged as $id=>$arguments) {
33 1
            $definition->addMethodCall('addSubscriber', [new Reference($id)]);
34
        }
35
    }
36 1
37 1
    private function compileDrivers(ContainerBuilder $container)
38 1
    {
39
        $drivers             = $container->getParameterBag()->get('doyo.coverage.drivers');
40
        $codeCoverageOptions = $container->getParameterBag()->get('doyo.coverage.options');
41
42 1
        $map = [
43
            'cached' => $container->getParameterBag()->get('doyo.coverage.cached.class'),
44 1
        ];
45 1
46 1
        foreach ($drivers as $config) {
47
            $namespace = $config['namespace'];
48
            $driver    = $config['driver'];
49
            $class     = $map[$driver];
50 1
            $id        = 'doyo.coverage.cached.'.$namespace;
51
52 1
            $definition = new Definition($class);
53 1
            $definition->addTag('doyo.dispatcher.subscriber');
54 1
            $definition->addArgument($namespace);
55
            $definition->addArgument($codeCoverageOptions);
56 1
            $definition->addArgument(new Reference('doyo.coverage.filter'));
57 1
            $definition->setPublic(true);
58
59 1
            $container->setDefinition($id, $definition);
60 1
        }
61 1
    }
62 1
63
    private function compileCoverageOptions(ContainerBuilder $container)
64 1
    {
65
        $options = $container->getParameterBag()->get('doyo.coverage.options');
66
67 1
        $definitions = $container->findTaggedServiceIds('doyo.code_coverage');
68
        /* @var \Symfony\Component\DependencyInjection\Definition $definition */
69 1
        foreach ($definitions as $id => $test) {
70 1
            $definition = $container->getDefinition($id);
71 1
            $this->addCoverageOption($definition, $options);
72 1
        }
73 1
    }
74 1
75
    private function addCoverageOption(Definition $definition, array $options)
76
    {
77 1
        foreach ($options as $name => $value) {
78 1
            $method = 'set'.ucfirst($name);
79 1
            $definition->addMethodCall($method, [$value]);
80 1
        }
81 1
    }
82
83
    private function compileFilterOptions(ContainerBuilder $container)
84 1
    {
85 1
        $config     = $container->getParameterBag()->get('doyo.coverage.config');
86
        $filter     = $config['filter'];
87
        $basePath   = $container->getParameterBag()->get('paths.base');
88 1
        $definition = $container->getDefinition('doyo.coverage.filter');
89 1
90 1
        foreach ($filter as $options) {
91
            $options['basePath'] = $basePath;
92
            $this->filterWhitelist($definition, $options, 'add');
93 1
            $exclude = $options['exclude'];
94
            foreach ($exclude as $item) {
95
                $item['basePath'] = $basePath;
96
                $this->filterWhitelist($definition, $item, 'remove');
97
            }
98
        }
99
    }
100
101
    private function filterWhitelist(Definition $definition, $options, $method)
102
    {
103
        $basePath  = $options['basePath'];
104
        $suffix    = $options['suffix'] ?: '.php';
105
        $prefix    = $options['prefix'] ?: '';
106
        $type      = $options['directory'] ? 'directory' : 'file';
107
        $directory = $basePath.\DIRECTORY_SEPARATOR.$options['directory'];
108
        $file      = $basePath.\DIRECTORY_SEPARATOR.$options['file'];
109
110
        if (preg_match('/\/\*(\..+)/', $directory, $matches)) {
111
            $suffix    = $matches[1];
112
            $directory = str_replace($matches[0], '', $directory);
113
        }
114
115
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
116
        if ('directory' === $type) {
117
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
118
        }
119
120
        if ('file' === $type) {
121
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
122
        }
123
    }
124
}
125