Completed
Push — master ( 434e76...71c234 )
by Demonchaux
02:02
created

ConfigDAO::findConfig()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 8.9197
cc 4
eloc 11
nc 4
nop 2
crap 20
1
<?php
2
3
/**
4
 * This file is part of the Clover to Html package.
5
 *
6
 * (c) Stéphane Demonchaux <[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
namespace CloverToHtml;
12
13
class ConfigDAO
14
{
15
    /**
16
     * @param string $templatePath
17
     * @param string $key
18
     * @throws \InvalidArgumentException
19
     * @return array
20
     */
21
    public function findConfig($templatePath, $key = null)
22
    {
23
        if (is_file($this->getConfigPath($templatePath)) === false) {
24
            throw new \InvalidArgumentException(
25
                sprintf('Template "%s" with key "%s" not found', $templatePath, $key)
26
            );
27
        }
28
29
        if ($key === null) {
30
            return $config;
0 ignored issues
show
Bug introduced by
The variable $config seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
31
        }
32
33
        $config = $this->loadConfig($templatePath);
34
35
        if (isset($config[$key])) {
36
            return $config[$key];
37
        }
38
39
        throw new \InvalidArgumentException(
40
            sprintf('Template "%s" with key "%s" not found', $templatePath, $key)
41
        );
42
    }
43
44
    /**
45
     * @param string $templatePath
46
     * @return string
47
     */
48
    private function getConfigPath($templatePath)
49
    {
50
        return $templatePath.'/config.json';
51
    }
52
53
    /**
54
     * @param string $templatePath
55
     * @return array
56
     */
57
    private function loadConfig($templatePath)
58
    {
59
        return json_decode(file_get_contents($this->getConfigPath($templatePath)), true);
60
    }
61
}
62