Completed
Push — master ( b3674c...7a0e45 )
by Vitaly
02:46
created

XmlMetadataCollector::collect()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 2
crap 5
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 3
    public function collect($xmlConfig, array $classesMetadata = []) : array
22
    {
23
        // Iterate config and resolve single instance
24 3
        foreach ($this->xml2array(new \SimpleXMLElement($xmlConfig)) as $key => $classesArrayData) {
25 3
            if ($key === CollectionClassResolver::KEY) {
26
                // If we have only one instance we need to add array
27
                /** @var array $classesArrayData */
28 3
                foreach (!array_key_exists(0, $classesArrayData) ? [$classesArrayData] : $classesArrayData as $classArrayData) {
29 3
                    $classMetadata = $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...
30
                    // TODO: This is only work aroud right now
31 3
                    $classesMetadata[$classMetadata->className] = $this->resolver->resolve($classArrayData, $classesMetadata[$classMetadata->className] ?? null);
32
                }
33
            }
34
        }
35
36 3
        return $classesMetadata;
37
    }
38
39
    /**
40
     * function xml2array
41
     *
42
     * This function is part of the PHP manual.
43
     *
44
     * The PHP manual text and comments are covered by the Creative Commons
45
     * Attribution 3.0 License, copyright (c) the PHP Documentation Group
46
     *
47
     * @param \SimpleXMLElement|array $xmlObject
48
     * @param array                   $out
49
     *
50
     * @return array XML converted array
51
     *
52
     * @author  k dot antczak at livedata dot pl
53
     * @date    2011-04-22 06:08 UTC
54
     * @link    http://www.php.net/manual/en/ref.simplexml.php#103617
55
     * @license http://www.php.net/license/index.php#doc-lic
56
     * @license http://creativecommons.org/licenses/by/3.0/
57
     * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
58
     */
59 3
    protected function xml2array($xmlObject, array $out = []) : array
60
    {
61 3
        foreach ((array)$xmlObject as $index => $node) {
62 3
            $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...
63
        }
64
65 3
        return $out;
66
    }
67
}
68