Completed
Push — develop ( ddf000...96b89c )
by Kevin
10:14 queued 06:44
created

ContextRepository::getContextHierarchy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Magium\Configuration\View\Controllers\Helpers;
4
5
use Magium\Configuration\Config\Repository\ConfigInterface;
6
use Magium\Configuration\File\Context\AbstractContextConfigurationFile;
7
8
class ContextRepository
9
{
10
11
    protected $contextFile;
12
13 10
    public function __construct(
14
        AbstractContextConfigurationFile $contextFile
15
    )
16
    {
17 10
        $this->contextFile = $contextFile;
18 10
    }
19
20 9
    public function getContextHierarchy()
21
    {
22 9
        $contextsFile = $this->getContextFile();
23 9
        $xml = $contextsFile->toXml();
24 9
        $children = [];
25 9
        foreach ($xml->children() as $child) {
26 1
            $children[] = $this->buildContextArray($child);
27
        }
28 9
        $contexts[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$contexts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $contexts = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
29 9
            'id' => 'default',
30 9
            'label' => 'Default',
31 9
            'children' => $children
32
        ];
33
34 9
        return $contexts;
35
    }
36
37 1
    public function getContextList()
38
    {
39
        $contexts = [
40 1
            ConfigInterface::CONTEXT_DEFAULT   => 'Default'
41
        ];
42 1
        $xml = $this->contextFile->toXml();
43 1
        $xml->registerXPathNamespace('s', 'http://www.magiumlib.com/ConfigurationContext');
44 1
        $nodes = $this->contextFile->toXml()->xpath('//s:context');
45 1
        foreach ($nodes as $node) {
46 1
            $id = $label = (string)$node['id'];
47 1
            if (isset($node['label'])) {
48 1
                $label = (string)$node['label'];
49
            }
50 1
            $contexts[$id] = $label;
51
        }
52
53 1
        return $contexts;
54
    }
55
56 9
    public function getContextFile()
57
    {
58 9
        return $this->contextFile;
59
    }
60
61 1
    private function buildContextArray(\SimpleXMLElement $element)
62
    {
63 1
        $children = [];
64 1
        foreach ($element->children() as $child) {
65 1
            $children[] = $this->buildContextArray($child);
66
        }
67 1
        $label = (string)$element['id'];
68 1
        if (isset($element['label'])) {
69 1
            $label = (string)$element['label'];
70
        }
71
        $contexts = [
72 1
            'id' => (string)$element['id'],
73 1
            'label' => $label,
74 1
            'children' => $children
75
        ];
76 1
        return $contexts;
77
    }
78
79
}
80