Total Complexity | 54 |
Total Lines | 301 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like JsonDiff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonDiff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class JsonDiff |
||
6 | { |
||
7 | const SKIP_REARRANGE_ARRAY = 1; |
||
8 | const STOP_ON_DIFF = 2; |
||
9 | |||
10 | private $options = 0; |
||
11 | private $original; |
||
12 | private $new; |
||
13 | |||
14 | private $added; |
||
15 | private $addedCnt = 0; |
||
16 | private $addedPaths = array(); |
||
17 | |||
18 | private $removed; |
||
19 | private $removedCnt = 0; |
||
20 | private $removedPaths = array(); |
||
21 | |||
22 | private $modifiedOriginal; |
||
23 | private $modifiedNew; |
||
24 | private $modifiedCnt = 0; |
||
25 | private $modifiedPaths = array(); |
||
26 | |||
27 | private $path = '#'; |
||
28 | |||
29 | private $rearranged; |
||
30 | |||
31 | /** |
||
32 | * Processor constructor. |
||
33 | * @param $original |
||
34 | * @param $new |
||
35 | * @param int $options |
||
36 | */ |
||
37 | public function __construct($original, $new, $options = 0) |
||
38 | { |
||
39 | $this->original = $original; |
||
40 | $this->new = $new; |
||
41 | $this->options = $options; |
||
42 | |||
43 | $this->rearranged = $this->rearrange(); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Returns total number of differences |
||
48 | * @return int |
||
49 | */ |
||
50 | public function getDiffCnt() |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Returns removals as partial value of original. |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function getRemoved() |
||
60 | { |
||
61 | return $this->removed; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns list of `JSON` paths that were removed from original. |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getRemovedPaths() |
||
69 | { |
||
70 | return $this->removedPaths; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Returns number of removals. |
||
75 | * @return int |
||
76 | */ |
||
77 | public function getRemovedCnt() |
||
78 | { |
||
79 | return $this->removedCnt; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns additions as partial value of new. |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function getAdded() |
||
87 | { |
||
88 | return $this->added; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Returns number of additions. |
||
93 | * @return int |
||
94 | */ |
||
95 | public function getAddedCnt() |
||
96 | { |
||
97 | return $this->addedCnt; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Returns list of `JSON` paths that were added to new. |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getAddedPaths() |
||
105 | { |
||
106 | return $this->addedPaths; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Returns changes as partial value of original. |
||
111 | * @return mixed |
||
112 | */ |
||
113 | public function getModifiedOriginal() |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Returns changes as partial value of new. |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function getModifiedNew() |
||
123 | { |
||
124 | return $this->modifiedNew; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Returns number of changes. |
||
129 | * @return int |
||
130 | */ |
||
131 | public function getModifiedCnt() |
||
132 | { |
||
133 | return $this->modifiedCnt; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Returns list of `JSON` paths that were changed from original to new. |
||
138 | * @return array |
||
139 | */ |
||
140 | public function getModifiedPaths() |
||
141 | { |
||
142 | return $this->modifiedPaths; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns new value, rearranged with original order. |
||
147 | * @return array|object |
||
148 | */ |
||
149 | public function getRearranged() |
||
150 | { |
||
151 | return $this->rearranged; |
||
152 | } |
||
153 | |||
154 | private function rearrange() |
||
157 | } |
||
158 | |||
159 | private function process($original, $new) |
||
160 | { |
||
161 | if ( |
||
162 | (!$original instanceof \stdClass && !is_array($original)) |
||
163 | || (!$new instanceof \stdClass && !is_array($new)) |
||
164 | ) { |
||
165 | if ($original !== $new) { |
||
166 | $this->modifiedCnt++; |
||
167 | $this->modifiedPaths [] = $this->path; |
||
168 | JsonProcessor::pushByPath($this->modifiedOriginal, $this->path, $original); |
||
169 | JsonProcessor::pushByPath($this->modifiedNew, $this->path, $new); |
||
170 | if ($this->options & self::STOP_ON_DIFF) { |
||
171 | return; |
||
172 | } |
||
173 | } |
||
174 | return $new; |
||
175 | } |
||
176 | |||
177 | if ( |
||
178 | !($this->options & self::SKIP_REARRANGE_ARRAY) |
||
179 | && is_array($original) && is_array($new) |
||
180 | ) { |
||
181 | $new = $this->rearrangeArray($original, $new); |
||
182 | } |
||
183 | |||
184 | $newArray = $new instanceof \stdClass ? get_object_vars($new) : $new; |
||
185 | $newOrdered = array(); |
||
186 | |||
187 | $originalKeys = $original instanceof \stdClass ? get_object_vars($original) : $original; |
||
188 | |||
189 | foreach ($originalKeys as $key => $originalValue) { |
||
190 | $path = $this->path; |
||
191 | $this->path .= '/' . urlencode($key); |
||
192 | |||
193 | if (array_key_exists($key, $newArray)) { |
||
194 | $newOrdered[$key] = $this->process($originalValue, $newArray[$key]); |
||
195 | unset($newArray[$key]); |
||
196 | } else { |
||
197 | $this->removedCnt++; |
||
198 | $this->removedPaths [] = $this->path; |
||
199 | JsonProcessor::pushByPath($this->removed, $this->path, $originalValue); |
||
200 | if ($this->options & self::STOP_ON_DIFF) { |
||
201 | return; |
||
202 | } |
||
203 | } |
||
204 | $this->path = $path; |
||
205 | } |
||
206 | |||
207 | // additions |
||
208 | foreach ($newArray as $key => $value) { |
||
209 | $newOrdered[$key] = $value; |
||
210 | $path = $this->path . '/' . urlencode($key); |
||
211 | JsonProcessor::pushByPath($this->added, $path, $value); |
||
212 | $this->addedCnt++; |
||
213 | $this->addedPaths [] = $path; |
||
214 | if ($this->options & self::STOP_ON_DIFF) { |
||
215 | return; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | return is_array($new) ? $newOrdered : (object)$newOrdered; |
||
220 | } |
||
221 | |||
222 | private function rearrangeArray(array $original, array $new) |
||
306 | } |
||
307 | } |