Loader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A load() 0 15 4
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Config;
11
use Hal\Application\Config\Configuration;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * Load a config file
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class Loader
20
{
21
    /**
22
     * Validator of configuration
23
     *
24
     * @var Hydrator
25
     */
26
    private $hydrator;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param Hydrator $hydrator
32
     */
33
    public function __construct(Hydrator $hydrator) {
34
        $this->hydrator = $hydrator;
35
    }
36
37
    /**
38
     * Load config file
39
     *
40
     * @param $filename
41
     * @return Configuration
42
     * @throws \RuntimeException
43
     */
44
    public function load($filename) {
45
46
        if(!\file_exists($filename) ||!\is_readable($filename)) {
47
            throw new \RuntimeException('configuration file is not accessible');
48
        }
49
        $content = file_get_contents($filename);
50
        $parser = new Yaml();
51
        $array = $parser->parse($content);
52
        if (null === $array) {
53
            throw new \RuntimeException('configuration file is empty');
54
        }
55
56
        return $this->hydrator->hydrates(new Configuration, $array);
0 ignored issues
show
Bug introduced by
It seems like $array defined by $parser->parse($content) on line 51 can also be of type string; however, Hal\Component\Config\Hydrator::hydrates() 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...
57
58
    }
59
}