XmlMetadataCollector   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B collect() 0 17 5
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\containerxml;
7
8
use samsonframework\container\AbstractMetadataCollector;
9
use samsonframework\containercollection\CollectionClassResolver;
10
11
/**
12
 * XML class metadata collector.
13
 * Class resolves and collects class metadata from XML string.
14
 *
15
 * @author Vitaly Egorov <[email protected]>
16
 */
17
class XmlMetadataCollector extends AbstractMetadataCollector
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 2
    public function collect($xmlConfig, array $classesMetadata = []) : array
23
    {
24
        // Iterate config and resolve single instance
25 2
        foreach ($this->xml2array(new \SimpleXMLElement($xmlConfig)) as $key => $classesArrayData) {
26 2
            if ($key === CollectionClassResolver::KEY) {
27
                // If we have only one instance we need to add array
28
                /** @var array $classesArrayData */
29 2
                foreach (!array_key_exists(0, $classesArrayData) ? [$classesArrayData] : $classesArrayData as $classArrayData) {
30 2
                    $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...
31
                    // TODO: This is only work aroud right now
32 2
                    $classesMetadata[$classMetadata->className] = $this->resolver->resolve($classArrayData, $classesMetadata[$classMetadata->className] ?? null);
33
                }
34
            }
35
        }
36
37 2
        return $classesMetadata;
38
    }
39
40
    /**
41
     * function xml2array
42
     *
43
     * This function is part of the PHP manual.
44
     *
45
     * The PHP manual text and comments are covered by the Creative Commons
46
     * Attribution 3.0 License, copyright (c) the PHP Documentation Group
47
     *
48
     * @param \SimpleXMLElement|array $xmlObject
49
     * @param array                   $out
50
     *
51
     * @return array XML converted array
52
     *
53
     * @author  k dot antczak at livedata dot pl
54
     * @date    2011-04-22 06:08 UTC
55
     * @link    http://www.php.net/manual/en/ref.simplexml.php#103617
56
     * @license http://www.php.net/license/index.php#doc-lic
57
     * @license http://creativecommons.org/licenses/by/3.0/
58
     * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
59
     */
60 2
    protected function xml2array($xmlObject, array $out = []) : array
61
    {
62 2
        foreach ((array)$xmlObject as $index => $node) {
63 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...
64
        }
65
66 2
        return $out;
67
    }
68
}
69