| Total Complexity | 52 |
| Total Lines | 291 |
| 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 | |||
| 9 | private $options = 0; |
||
| 10 | private $original; |
||
| 11 | private $new; |
||
| 12 | |||
| 13 | private $added; |
||
| 14 | private $addedCnt = 0; |
||
| 15 | private $addedPaths = array(); |
||
| 16 | |||
| 17 | private $removed; |
||
| 18 | private $removedCnt = 0; |
||
| 19 | private $removedPaths = array(); |
||
| 20 | |||
| 21 | private $modifiedOriginal; |
||
| 22 | private $modifiedNew; |
||
| 23 | private $modifiedCnt = 0; |
||
| 24 | private $modifiedPaths = array(); |
||
| 25 | |||
| 26 | private $path = '#'; |
||
| 27 | |||
| 28 | private $rearranged; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Processor constructor. |
||
| 32 | * @param $original |
||
| 33 | * @param $new |
||
| 34 | * @param int $options |
||
| 35 | */ |
||
| 36 | public function __construct($original, $new, $options = 0) |
||
| 37 | { |
||
| 38 | $this->original = $original; |
||
| 39 | $this->new = $new; |
||
| 40 | $this->options = $options; |
||
| 41 | |||
| 42 | $this->rearranged = $this->rearrange(); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns removals as partial value of original. |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | public function getRemoved() |
||
| 50 | { |
||
| 51 | return $this->removed; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns list of `JSON` paths that were removed from original. |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | public function getRemovedPaths() |
||
| 59 | { |
||
| 60 | return $this->removedPaths; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns number of removals. |
||
| 65 | * @return int |
||
| 66 | */ |
||
| 67 | public function getRemovedCnt() |
||
| 68 | { |
||
| 69 | return $this->removedCnt; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Returns additions as partial value of new. |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | public function getAdded() |
||
| 77 | { |
||
| 78 | return $this->added; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns number of additions. |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | public function getAddedCnt() |
||
| 86 | { |
||
| 87 | return $this->addedCnt; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns list of `JSON` paths that were added to new. |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function getAddedPaths() |
||
| 95 | { |
||
| 96 | return $this->addedPaths; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns changes as partial value of original. |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | public function getModifiedOriginal() |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns changes as partial value of new. |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public function getModifiedNew() |
||
| 113 | { |
||
| 114 | return $this->modifiedNew; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns number of changes. |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | public function getModifiedCnt() |
||
| 122 | { |
||
| 123 | return $this->modifiedCnt; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns list of `JSON` paths that were changed from original to new. |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function getModifiedPaths() |
||
| 131 | { |
||
| 132 | return $this->modifiedPaths; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns new value, rearranged with original order. |
||
| 137 | * @return array|object |
||
| 138 | */ |
||
| 139 | public function getRearranged() |
||
| 140 | { |
||
| 141 | return $this->rearranged; |
||
| 142 | } |
||
| 143 | |||
| 144 | private function rearrange() |
||
| 147 | } |
||
| 148 | |||
| 149 | private function process($original, $new) |
||
| 150 | { |
||
| 151 | if ( |
||
| 152 | (!$original instanceof \stdClass && !is_array($original)) |
||
| 153 | || (!$new instanceof \stdClass && !is_array($new)) |
||
| 154 | ) { |
||
| 155 | if ($original !== $new) { |
||
| 156 | $this->modifiedCnt++; |
||
| 157 | $this->modifiedPaths [] = $this->path; |
||
| 158 | $this->pushByPath($this->modifiedOriginal, $this->path, $original); |
||
| 159 | $this->pushByPath($this->modifiedNew, $this->path, $new); |
||
| 160 | } |
||
| 161 | return $new; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ( |
||
| 165 | !($this->options & self::SKIP_REARRANGE_ARRAY) |
||
| 166 | && is_array($original) && is_array($new) |
||
| 167 | ) { |
||
| 168 | $new = $this->rearrangeArray($original, $new); |
||
| 169 | } |
||
| 170 | |||
| 171 | $newArray = $new instanceof \stdClass ? get_object_vars($new) : $new; |
||
| 172 | $newOrdered = array(); |
||
| 173 | |||
| 174 | $originalKeys = $original instanceof \stdClass ? get_object_vars($original) : $original; |
||
| 175 | |||
| 176 | foreach ($originalKeys as $key => $originalValue) { |
||
| 177 | $path = $this->path; |
||
| 178 | $this->path .= '/' . urlencode($key); |
||
| 179 | |||
| 180 | if (isset($newArray[$key])) { |
||
| 181 | $newOrdered[$key] = $this->process($originalValue, $newArray[$key]); |
||
| 182 | unset($newArray[$key]); |
||
| 183 | } else { |
||
| 184 | $this->removedCnt++; |
||
| 185 | $this->removedPaths [] = $this->path; |
||
| 186 | $this->pushByPath($this->removed, $this->path, $originalValue); |
||
| 187 | } |
||
| 188 | $this->path = $path; |
||
| 189 | } |
||
| 190 | |||
| 191 | // additions |
||
| 192 | foreach ($newArray as $key => $value) { |
||
| 193 | $newOrdered[$key] = $value; |
||
| 194 | $path = $this->path . '/' . urlencode($key); |
||
| 195 | $this->pushByPath($this->added, $path, $value); |
||
| 196 | $this->addedCnt++; |
||
| 197 | $this->addedPaths [] = $path; |
||
| 198 | } |
||
| 199 | |||
| 200 | return is_array($new) ? $newOrdered : (object)$newOrdered; |
||
| 201 | } |
||
| 202 | |||
| 203 | private function rearrangeArray(array $original, array $new) |
||
| 283 | } |
||
| 284 | |||
| 285 | private function pushByPath(&$holder, $path, $value) |
||
| 286 | { |
||
| 287 | $pathItems = explode('/', $path); |
||
| 288 | if ('#' === $pathItems[0]) { |
||
| 289 | array_shift($pathItems); |
||
| 290 | } |
||
| 291 | $ref = &$holder; |
||
| 296 | } |
||
| 297 | } |