Passed
Pull Request — develop (#5)
by ANTHONIUS
04:03
created

CoveragePass::findFiles()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 16
nop 3
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 5
rs 9.3888
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
20
class CoveragePass implements CompilerPassInterface
21
{
22 1
    public function process(ContainerBuilder $container)
23
    {
24 1
        $this->compileCoverageOptions($container);
25 1
        $this->compileFilterOptions($container);
26
    }
27
28 1
    private function compileCoverageOptions(ContainerBuilder $container)
29
    {
30 1
        $options = $container->getParameterBag()->get('doyo.coverage.options');
31
32 1
        $definitions = $container->findTaggedServiceIds('doyo.code_coverage');
33
34
        /* @var \Symfony\Component\DependencyInjection\Definition $definition */
35 1
        foreach ($definitions as $id => $test) {
36 1
            $definition = $container->getDefinition($id);
37 1
            $this->addCoverageOption($definition, $options);
38
        }
39
    }
40
41 1
    private function addCoverageOption(Definition $definition, array $options)
42
    {
43 1
        foreach ($options as $name => $value) {
44 1
            $method = 'set'.ucfirst($name);
45 1
            $definition->addMethodCall($method, [$value]);
46
        }
47
    }
48
49 1
    private function compileFilterOptions(ContainerBuilder $container)
50
    {
51 1
        $config     = $container->getParameterBag()->get('doyo.coverage.config');
52 1
        $filter     = $config['filter'];
53 1
        $basePath   = $container->getParameterBag()->get('paths.base');
54 1
        $definition = $container->getDefinition('doyo.coverage.filter');
55
56 1
        foreach ($filter as $options) {
57 1
            $options['basePath'] = $basePath;
58 1
            $this->filterWhitelist($definition, $options, 'add');
59 1
            $exclude = $options['exclude'];
60 1
            foreach ($exclude as $item) {
61 1
                $item['basePath'] = $basePath;
62 1
                $this->filterWhitelist($definition, $item, 'remove');
63
            }
64
        }
65
    }
66
67 1
    private function filterWhitelist(Definition $definition, $options, $method)
68
    {
69 1
        $basePath  = $options['basePath'];
70 1
        $suffix    = $options['suffix'] ?: '.php';
71 1
        $prefix    = $options['prefix'] ?: '';
72 1
        $type      = $options['directory'] ? 'directory' : 'file';
73 1
        $directory = $basePath.DIRECTORY_SEPARATOR.$options['directory'];
74 1
        $file      = $basePath.DIRECTORY_SEPARATOR.$options['file'];
75
76 1
        if (preg_match('/\/\*(\..+)/', $directory, $matches)) {
77 1
            $suffix    = $matches[1];
78 1
            $directory = str_replace($matches[0], '', $directory);
79
        }
80
81 1
        $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist';
82 1
        if ('directory' === $type) {
83 1
            $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]);
84
        }
85
86 1
        if ('file' === $type) {
87 1
            $definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
88
        }
89
    }
90
}
91