Completed
Push — master ( 78171c...11e063 )
by Alexander
02:28
created

XmlDifferenceService::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: horat1us
5
 * Date: 5/11/17
6
 * Time: 6:41 PM
7
 */
8
9
namespace Horat1us\Services;
10
11
use Horat1us\Arrays\Collection;
12
use Horat1us\Services\Traits\PropertiesDifferenceTrait;
13
use Horat1us\XmlConvertibleInterface;
14
use Horat1us\XmlConvertibleObject;
15
16
17
/**
18
 * Class XmlDifferenceService
19
 * @package Horat1us\Services
20
 */
21
class XmlDifferenceService
22
{
23
    use PropertiesDifferenceTrait;
24
25
    /**
26
     * @var XmlConvertibleInterface
27
     */
28
    protected $source;
29
30
    /**
31
     * @var XmlConvertibleInterface
32
     */
33
    protected $target;
34
35
36
    /**
37
     * XmlDifferenceService constructor.
38
     * @param XmlConvertibleInterface $source
39
     * @param XmlConvertibleInterface $target
40
     */
41 4
    public function __construct(
42
        XmlConvertibleInterface $source,
43
        XmlConvertibleInterface $target
44
    )
45
    {
46
        $this
47 4
            ->setSource($source)
48 4
            ->setTarget($target);
49 4
    }
50
51
    /**
52
     * @return XmlConvertibleInterface|null
53
     */
54 4
    public function difference()
55
    {
56 4
        if ($this->getIsCommonDifferent()) {
57 4
            return clone $this->getSource();
58
        }
59
60 2
        $newChildren = $this->getDifferentChildren();
61 2
        if (empty($newChildren)) {
62 2
            return null;
63
        }
64
65 2
        $target = clone $this->getSource();
66 2
        $target->setXmlChildren($newChildren);
67
68 2
        return clone $target;
69
    }
70
71
    /**
72
     * Difference by element name, children count and properties
73
     */
74 4
    public function getIsCommonDifferent()
75
    {
76 4
        return $this->getSource()->getXmlElementName() !== $this->getTarget()->getXmlElementName()
77 4
            || empty($this->getSource()->getXmlChildren()) && !empty($this->getTarget()->getXmlChildren())
78 4
            || $this->getIsDifferentProperties();
79
    }
80
81
    /**
82
     * @return array
83
     */
84 2
    public function getDifferentChildren()
85
    {
86 2
        return Collection::from($this->getSource()->getXmlChildren() ?? [])
87
            ->map(function ($child) {
88 2
                return $this->transform($child);
89 2
            })
90
            ->map(function (XmlConvertibleInterface $child) {
91 2
                return $this->findDifference($child);
92 2
            })
93 2
            ->filter(function ($child) {
94 2
                return $child !== null;
95 2
            })
96 2
            ->array;
97
    }
98
99
    /**
100
     * @param XmlConvertibleInterface|\DOMNode|\DOMDocument $object
101
     * @return XmlConvertibleInterface
102
     */
103 2
    protected function transform($object)
104
    {
105 2
        return $object instanceof XmlConvertibleInterface
106 2
            ? $object
107 2
            : XmlConvertibleObject::fromXml($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<DOMNode>, but the function expects a object<DOMDocument>|object<DOMElement>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
    }
109
110
    /**
111
     * @param XmlConvertibleInterface $child
112
     * @return XmlConvertibleInterface|null
113
     */
114 2
    protected function findDifference(
115
        XmlConvertibleInterface $child
116
    )
117
    {
118 2
        foreach ($this->getTarget()->getXmlChildren() ?? [] as $comparedChild) {
119 2
            $target = $this->transform($comparedChild);
120
121 2
            if ($difference = $child->xmlDiff($target)) {
122 2
                return $difference;
123
            }
124
        }
125
126 2
        return null;
127
    }
128
129
    /**
130
     * @return XmlConvertibleInterface
131
     */
132 4
    public function getSource(): XmlConvertibleInterface
133
    {
134 4
        return $this->source;
135
    }
136
137
    /**
138
     * @param XmlConvertibleInterface $source
139
     * @return $this
140
     */
141 4
    public function setSource(XmlConvertibleInterface $source)
142
    {
143 4
        $this->source = $source;
144
145 4
        return $this;
146
    }
147
148
    /**
149
     * @return XmlConvertibleInterface
150
     */
151 4
    public function getTarget(): XmlConvertibleInterface
152
    {
153 4
        return $this->target;
154
    }
155
156
    /**
157
     * @param XmlConvertibleInterface $target
158
     * @return $this
159
     */
160 4
    public function setTarget(XmlConvertibleInterface $target)
161
    {
162 4
        $this->target = $target;
163
164 4
        return $this;
165
    }
166
}