Factory::all()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Knp\RadBundle\Routing\Conventional\Config;
4
5
use Knp\RadBundle\Routing\Conventional\Config;
6
7
class Factory
8
{
9
    private $parser;
10
    private $index = array();
11
12
    public function __construct(Parser $parser = null)
13
    {
14
        $this->parser = $parser ?: new Parser;
15
    }
16
17
    public function all($path)
18
    {
19
        $rawConfigs = $this->parser->parse($path);
20
        if (empty($rawConfigs)) {
21
            return array();
22
        }
23
24
        $all = array();
25
        foreach ($rawConfigs as $name => $rawConfig) {
0 ignored issues
show
Bug introduced by
The expression $rawConfigs of type string|array|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
26
            $all[] = $this->get($name, $rawConfigs);
27
        }
28
29
        return $all;
30
    }
31
32
    private function get($name, array $rawConfigs)
33
    {
34
        if (isset($this->index[$name])) {
35
            return $this->index[$name];
36
        }
37
        if (!array_key_exists($name, $rawConfigs)) {
38
            throw new \InvalidArgumentException(
39
                sprintf('no "%s" in [%s]', $name, implode(', ', array_keys($rawConfigs)))
40
            );
41
        }
42
        $rawConfig = $rawConfigs[$name];
43
        $parent = null;
44
        if (isset($rawConfig['parent'])) {
45
            $parentCollection = $this->get($rawConfig['parent'], $rawConfigs);
46
            $parent = $parentCollection->getRepresentant();
47
        }
48
49
        return $this->index[$name] = new Config($name, $rawConfig, $parent);
50
    }
51
}
52