Completed
Pull Request — master (#6)
by Tomáš
04:54
created

YamlFileConfigurationBuilder::internalBuild()   C

Complexity

Conditions 10
Paths 28

Size

Total Lines 49
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 17.2354

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 14
cts 24
cp 0.5833
rs 5.5471
c 0
b 0
f 0
cc 10
eloc 26
nc 28
nop 1
crap 17.2354

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 a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
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
namespace Symplify\PHP7_Sculpin\Core\Configuration;
13
14
use Dflydev\DotAccessConfiguration\AbstractConfigurationBuilder;
15
use Dflydev\DotAccessConfiguration\ConfigurationInterface;
16
use Dflydev\DotAccessData\Util as DotAccessDataUtil;
17
use Symfony\Component\Yaml\Yaml;
18
19
final class YamlFileConfigurationBuilder extends AbstractConfigurationBuilder
20
{
21
    /**
22
     * @var string[]
23
     */
24
    private $yamlConfigurationFilenames;
25
26 1
    public function __construct(array $yamlConfigurationFilenames)
27
    {
28 1
        $this->yamlConfigurationFilenames = $yamlConfigurationFilenames;
29 1
    }
30
31
    /**
32
     * {@inheritdoc}.
33
     */
34 1
    public function internalBuild(ConfigurationInterface $configuration)
35
    {
36 1
        $config = [];
37 1
        $imports = [];
38 1
        foreach ($this->yamlConfigurationFilenames as $yamlConfigurationFilename) {
39 1
            if (file_exists($yamlConfigurationFilename)) {
40 1
                $config = DotAccessDataUtil::mergeAssocArray(
41 1
                    $config, Yaml::parse(file_get_contents($yamlConfigurationFilename))
42
                );
43
44 1
                if (isset($config['imports'])) {
45
                    foreach ((array) $config['imports'] as $file) {
46
                        if (0 === strpos($file, '/')) {
47
                            // Absolute path
48
                            $imports[] = $file;
49
                        } else {
50
                            if ($realpath = realpath(dirname($yamlConfigurationFilename).'/'.$file)) {
51 1
                                $imports[] = $realpath;
52
                            }
53
                        }
54
                    }
55
                }
56
            }
57
        }
58
59 1
        if ($imports) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $imports of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
60
            $importsBuilder = new static($imports);
61
62
            // We want to reconfigure the imports builder to have the
63
            // same basic configuration as this instance.
64
            $this->reconfigure($importsBuilder);
65
66
            $configuration->import($importsBuilder->build());
67
68
            $internalImports = $configuration->get('imports');
69
        } else {
70 1
            $internalImports = null;
71
        }
72
73 1
        $configuration->importRaw($config);
74
75 1
        if ($internalImports) {
76
            foreach ((array) $internalImports as $import) {
77
                $configuration->append('imports', $import);
78
            }
79
        }
80
81 1
        return $configuration;
82
    }
83
}
84