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

ContainerFactory::doCreateContainer()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.0368

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 12
nop 0
dl 0
loc 54
ccs 30
cts 33
cp 0.9091
crap 7.0368
rs 8.4746
c 0
b 0
f 0

How to fix   Long Method   

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;
15
16
use Doyo\Bridge\CodeCoverage\Compiler\CoveragePass;
17
use Doyo\Bridge\CodeCoverage\Compiler\ReportPass;
18
use Doyo\Bridge\CodeCoverage\Console\Application;
19
use Doyo\Bridge\CodeCoverage\DependencyInjection\CodeCoverageExtension;
20
use Doyo\Bridge\CodeCoverage\Driver\Dummy;
21
use Doyo\Bridge\CodeCoverage\Exception\SessionException;
22
use Doyo\Bridge\CodeCoverage\Session\LocalSession;
23
use Doyo\Bridge\CodeCoverage\Session\RemoteSession;
24
use Doyo\Bridge\CodeCoverage\Session\SessionInterface;
25
use Symfony\Component\Config\ConfigCache;
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
29
use Symfony\Component\Yaml\Yaml;
30
31
class ContainerFactory
32
{
33
    /**
34
     * @var array
35
     */
36
    private $config;
37
38
    /**
39
     * @var ContainerInterface
40
     */
41
    private $container;
42
43 22
    public function __construct(array $config = [])
44
    {
45 22
        $this->config = $config;
46
    }
47
48
    /**
49
     * @return ContainerInterface
50
     */
51 21
    public function getContainer(): ContainerInterface
52
    {
53 21
        if(is_null($this->container)){
54 21
            $this->doCreateContainer();
55
        }
56
57 21
        return $this->container;
58
    }
59
60
    /**
61
     * @param bool $useDummyDriver
62
     * @return ProcessorInterface
63
     */
64 7
    public function createProcessor(bool $useDummyDriver = false): ProcessorInterface
65
    {
66 7
        $coverage = $this->createCodeCoverage($useDummyDriver);
67 7
        $processor = new Processor($coverage);
68
69 7
        return $processor;
70
    }
71
72 7
    public function createCodeCoverage(bool $useDummyDriver = false)
73
    {
74 7
        $container = $this->container;
75 7
        $driverClass = $container->getParameter('coverage.driver.class');
76
77 7
        if($useDummyDriver){
78 4
            $driverClass = $container->getParameter('coverage.driver.dummy.class');
79
        }
80
81 7
        $driver = new $driverClass;
82 7
        $filter = $container->get('coverage.filter');
83 7
        $coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
84
85 7
        return $coverage;
86
    }
87
88
    public function createApplication($version = 'dev')
89
    {
90
        return new Application('code-coverage', $version);
91
    }
92
93 21
    private function doCreateContainer()
94
    {
95 21
        $config = $this->config;
96 21
        $configs = [];
97
98 21
        if(isset($config['imports'])){
99
            $configs = $this->normalizeConfig($config);
100
            unset($config['imports']);
101
        }
102
103 21
        $configs[] = $config;
104
105 21
        $debug = false;
106 21
        foreach($configs as $config){
107 21
            if(isset($config['debug'])){
108
                $debug = $config['debug'];
109
            }
110
        }
111
112 21
        $id    = md5(serialize($configs));
113 21
        $file  = sys_get_temp_dir().'/doyo/coverage/container'.$id.'.php';
114 21
        $class = 'CodeCoverageContainer'.$id;
115 21
        $cachedContainer = new ConfigCache($file, $debug);
116 21
        $debug = true;
117 21
        if (!$cachedContainer->isFresh() || $debug) {
0 ignored issues
show
introduced by
The condition $debug is always true.
Loading history...
118
            //$this->dumpConfig();
119 21
            $builder = new ContainerBuilder();
120
121 21
            $builder->registerExtension(new CodeCoverageExtension());
122 21
            foreach($configs as $config){
123 21
                $builder->loadFromExtension('coverage', $config);
124
            }
125
126 21
            $builder->addCompilerPass(new CoveragePass());
127 21
            $builder->addCompilerPass(new ReportPass());
128 21
            $builder->compile(true);
129
130
131 21
            $dumper = new PhpDumper($builder);
132 21
            $cachedContainer->write(
133 21
                $dumper->dump([
0 ignored issues
show
Bug introduced by
It seems like $dumper->dump(array('class' => $class)) can also be of type array; however, parameter $content of Symfony\Component\Config...kerConfigCache::write() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
                /** @scrutinizer ignore-type */ $dumper->dump([
Loading history...
134 21
                    'class' => $class,
135
                ]),
136 21
                $builder->getResources()
137
            );
138
        }
139
140 21
        require_once $file;
141
142
        /* @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
143 21
        $container =  new $class();
144 21
        $container->set('factory', $this);
145
146 21
        $this->container = $container;
147
    }
148
149
    private function normalizeConfig($configuration)
150
    {
151
        $configs = [];
152
        foreach($configuration['imports'] as $file){
153
            $configs[] = $this->importFile($file);
154
        }
155
156
        return $configs;
157
    }
158
159
    private function importFile($file)
160
    {
161
        $config = Yaml::parseFile($file);
162
        return $config;
163
    }
164
}
165