Completed
Push — PSR-11-2 ( a5ad88...7f5041 )
by Nikolaos
03:59
created

Grouped::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.7023

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
ccs 19
cts 26
cp 0.7308
rs 8.7857
cc 6
nc 6
nop 2
crap 6.7023
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Config\Adapter;
15
16
use Phalcon\Config;
17
use Phalcon\Config\ConfigFactory;
18
use Phalcon\Config\Exception;
19
use Phalcon\Factory\Exception as ExceptionAlias;
20
21
use function is_string;
22
23
class Grouped extends Config
24
{
25
    /**
26
     * Grouped constructor.
27
     *
28
     * @param array  $arrayConfig
29
     * @param string $defaultAdapter
30
     *
31
     * @throws Exception
32
     * @throws ExceptionAlias
33
     */
34 15
    public function __construct(array $arrayConfig, string $defaultAdapter = "php")
35
    {
36 15
        parent::__construct([]);
37
38 15
        foreach ($arrayConfig as $configName) {
39 15
            $configInstance = $configName;
40
41
            // Set to default adapter if passed as string
42 15
            if ($configName instanceof Config) {
0 ignored issues
show
Bug introduced by
The class Phalcon\Config does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
43 1
                $this->merge($configInstance);
44 1
                continue;
45 15
            } elseif (is_string($configName)) {
46 13
                if ("" === $defaultAdapter) {
47 1
                    $this->merge(
48 1
                        (new ConfigFactory())->load($configName)
49
                    );
50
51 1
                    continue;
52
                }
53
54
                $configInstance = [
55 13
                    "filePath" => $configName,
56 13
                    "adapter"  => $defaultAdapter,
57
                ];
58 15
            } elseif (!isset($configInstance["adapter"])) {
59 1
                $configInstance["adapter"] = $defaultAdapter;
60
            }
61
62 15
            $configInstance = $this->getConfigObject($configInstance);
63 14
            $this->merge($configInstance);
64
        }
65 14
    }
66
67
    /**
68
     * @param array $configInstance
69
     *
70
     * @return Config
71
     * @throws Exception
72
     */
73 15
    private function checkOptionsArray(array $configInstance): Config
74
    {
75 15
        if (!isset($configInstance["config"])) {
76 1
            throw new Exception(
77
                "To use 'array' adapter you have to specify " .
78 1
                "the 'config' as an array."
79
            );
80
        }
81
82 14
        $configArray = $configInstance["config"];
83
84 14
        return new Config($configArray);
85
    }
86
87
    /**
88
     * @param array $configInstance
89
     *
90
     * @return Config
91
     * @throws Exception
92
     * @throws ExceptionAlias
93
     */
94 15
    private function getConfigObject(array $configInstance): Config
95
    {
96 15
        if ("array" === $configInstance["adapter"]) {
97 15
            $configInstance = $this->checkOptionsArray($configInstance);
98
        } else {
99 14
            $configInstance = (new ConfigFactory())->load($configInstance);
100
        }
101
102 14
        return $configInstance;
103
    }
104
}
105