Passed
Push — master ( a874de...6549f7 )
by AJ
01:41
created

DOMArray::loadArray()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 26
nc 18
nop 2
dl 0
loc 34
rs 6.9666
c 0
b 0
f 0
ccs 23
cts 23
cp 1
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *       __  ___      ____  _     ___                           _                    __
4
 *      /  |/  /_  __/ / /_(_)___/ (_)___ ___  ___  ____  _____(_)___  ____   ____ _/ /
5
 *     / /|_/ / / / / / __/ / __  / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ /
6
 *    / /  / / /_/ / / /_/ / /_/ / / / / / / /  __/ / / (__  ) / /_/ / / / // /_/ / /
7
 *   /_/  /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/
8
 *
9
 *  Array to  DOM Document Library
10
 *  Copyright (c) Multidimension.al (http://multidimension.al)
11
 *  Github : https://github.com/multidimension-al/dom-array
12
 *
13
 *  Licensed under The MIT License
14
 *  For full copyright and license information, please see the LICENSE file
15
 *  Redistributions of files must retain the above copyright notice.
16
 *
17
 *  @copyright  Copyright © 2017-2019 Multidimension.al (http://multidimension.al)
18
 *  @link       https://github.com/multidimension-al/dom-array Github
19
 *  @license    http://www.opensource.org/licenses/mit-license.php MIT License
20
 */
21
22
namespace Multidimensional\DomArray;
23
24
class DOMArray extends \DOMDocument
25
{
26
27
    /**
28
     * @param array|string $data
29
     * @param \DOMElement $domElement
30
     */
31 26
    public function loadArray($data, \DOMElement $domElement = null)
32
    {
33 26
        $domElement = is_null($domElement) ? $this : $domElement;
34
35 26
        if (is_array($data)) {
36 18
            foreach ($data as $key => $value) {
37 18
                if (is_null($value)) {
38 4
                    $domNode = $this->createElement($key);
39 4
                    $domNode->setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:nil", "true");
40 4
                    $domElement->appendChild($domNode);
41
                } else {
42 18
                    if (is_int($key)) {
43 6
                        if ($key === 0) {
44 6
                            $domNode = $domElement;
45
                        } else {
46 6
                            $domNode = $this->createElement($domElement->tagName);
47 6
                            $domElement->parentNode->appendChild($domNode);
48
                        }
49
                    } else {
50 18
                        if (preg_match('/^\@(.*)$/', $key, $attribute)) {
51 6
                            $domElement->setAttribute($attribute[1], $value);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on Multidimensional\DomArray\DOMArray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
                            $domElement->/** @scrutinizer ignore-call */ 
52
                                         setAttribute($attribute[1], $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52 6
                            continue;
53
                        } else {
54 18
                            $domNode = $this->createElement($key);
55 18
                            $domElement->appendChild($domNode);
56
                        }
57
                    }
58
                }
59 18
                $this->loadArray($value, $domNode);
0 ignored issues
show
Bug introduced by
It seems like $domNode can also be of type Multidimensional\DomArray\DOMArray; however, parameter $domElement of Multidimensional\DomArray\DOMArray::loadArray() does only seem to accept DOMElement|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
                $this->loadArray($value, /** @scrutinizer ignore-type */ $domNode);
Loading history...
60
            }
61 24
        } elseif (is_bool($data) === true) {
0 ignored issues
show
introduced by
The condition is_bool($data) === true is always false.
Loading history...
62 2
            $domElement->appendChild($this->createTextNode((boolval($data) ? 'true' : 'false')));
63 22
        } elseif (!empty($data) || is_numeric($data)) {
64 14
            $domElement->appendChild($this->createTextNode($data));
65
        }
66 26
    }
67
}
68