Completed
Push — master ( 01582c...42d944 )
by Vitaly
02:30
created

XmlMetadataCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 51
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 16 4
A xml2array() 0 8 4
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 20.08.16 at 12:39
5
 */
6
namespace samsonframework\container;
7
8
use samsonframework\container\collection\CollectionClassResolver;
9
10
/**
11
 * XML class metadata collector.
12
 * Class resolves and collects class metadata from XML string.
13
 *
14
 * @author Vitaly Egorov <[email protected]>
15
 */
16
class XmlMetadataCollector extends AbstractMetadataCollector
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function collect($xmlConfig) : array
22
    {
23 2
        $classesMetadata = [];
24
25
        // Iterate config and resolve single instance
26 2
        foreach ($this->xml2array(new \SimpleXMLElement($xmlConfig)) as $key => $classesArrayData) {
27 2
            if ($key === CollectionClassResolver::KEY) {
28
                /** @var array $classesArrayData */
29 2
                foreach ($classesArrayData as $classArrayData) {
30 2
                    $classesMetadata[] = $this->resolver->resolve($classArrayData);
0 ignored issues
show
Bug introduced by
The property resolver does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
                }
32
            }
33
        }
34
35 2
        return $classesMetadata;
36
    }
37
38
    /**
39
     * function xml2array
40
     *
41
     * This function is part of the PHP manual.
42
     *
43
     * The PHP manual text and comments are covered by the Creative Commons
44
     * Attribution 3.0 License, copyright (c) the PHP Documentation Group
45
     *
46
     * @param \SimpleXMLElement|array $xmlObject
47
     * @param array                   $out
48
     *
49
     * @return array XML converted array
50
     *
51
     * @author  k dot antczak at livedata dot pl
52
     * @date    2011-04-22 06:08 UTC
53
     * @link    http://www.php.net/manual/en/ref.simplexml.php#103617
54
     * @license http://www.php.net/license/index.php#doc-lic
55
     * @license http://creativecommons.org/licenses/by/3.0/
56
     * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
57
     */
58 2
    protected function xml2array($xmlObject, array $out = []) : array
59
    {
60 2
        foreach ((array)$xmlObject as $index => $node) {
61 2
            $out[$index] = (is_object($node) || is_array($node)) ? $this->xml2array($node) : $node;
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type object; however, samsonframework\containe...aCollector::xml2array() does only seem to accept object<SimpleXMLElement>|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...
62
        }
63
64 2
        return $out;
65
    }
66
}
67