Completed
Pull Request — master (#18)
by Tomáš
05:03
created

MultiCodingStandardExtension::detectConfigPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\MultiCodingStandard\DI;
11
12
use Nette\DI\CompilerExtension;
13
use Nette\DI\Helpers;
14
15
final class MultiCodingStandardExtension extends CompilerExtension
16
{
17
    /**
18
     * @var string[]
19
     */
20
    private $defaults = [
21
        'configPathsToCheck' => [
22
            '%appDir%/../../../../multi-cs.json', // installed as dependency
23
            '%appDir%/../../multi-cs.json', // cloned package
24
        ],
25
    ];
26
27
    public function loadConfiguration()
28
    {
29
        $this->setConfigToContainerBuilder($this->defaults);
30
        $this->loadServicesFromConfigPath(__DIR__.'/../config/services.neon');
31
    }
32
33
    /**
34
     * @param string[] $defaults
35
     */
36
    private function setConfigToContainerBuilder(array $defaults)
37
    {
38
        $config = $this->validateConfig($defaults);
39
        $config['configPath'] = $this->detectConfigPath($config['configPathsToCheck']);
40
        $this->getContainerBuilder()->parameters += $config;
41
    }
42
43
    private function loadServicesFromConfigPath(string $configPath)
44
    {
45
        $containerBuilder = $this->getContainerBuilder();
46
        $config = $this->loadFromFile($configPath);
47
        $this->compiler->parseServices($containerBuilder, $config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->loadFromFile($configPath) on line 46 can also be of type string; however, Nette\DI\Compiler::parseServices() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Deprecated Code introduced by
The method Nette\DI\Compiler::parseServices() has been deprecated.

This method has been deprecated.

Loading history...
48
    }
49
50
    private function detectConfigPath(array $configPathsToCheck) : string
51
    {
52
        foreach ($configPathsToCheck as $configPathToCheck) {
53
            $configPath = Helpers::expand($configPathToCheck, $this->getContainerBuilder()->parameters);
54
            if (file_exists($configPath)) {
55
                return $configPath;
56
            }
57
        }
58
59
        return '';
60
    }
61
}
62