| Conditions | 16 |
| Paths | 29 |
| Total Lines | 56 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function xorHash($data, $path = '') |
||
| 20 | { |
||
| 21 | $xorHash = ''; |
||
| 22 | |||
| 23 | if (!$data instanceof \stdClass && !is_array($data)) { |
||
| 24 | $s = $path . (string)$data; |
||
| 25 | if (strlen($xorHash) < strlen($s)) { |
||
| 26 | $xorHash = str_pad($xorHash, strlen($s)); |
||
| 27 | } |
||
| 28 | $xorHash ^= $s; |
||
| 29 | |||
| 30 | return $xorHash; |
||
| 31 | } |
||
| 32 | |||
| 33 | if ($this->options & JsonDiff::TOLERATE_ASSOCIATIVE_ARRAYS) { |
||
| 34 | if (is_array($data) && !empty($data) && !array_key_exists(0, $data)) { |
||
| 35 | $data = (object)$data; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | if (is_array($data)) { |
||
| 40 | if ($this->options & JsonDiff::REARRANGE_ARRAYS) { |
||
| 41 | foreach ($data as $key => $item) { |
||
| 42 | $itemPath = $path . '/' . $key; |
||
| 43 | $itemHash = $path . $this->xorHash($item, $itemPath); |
||
| 44 | if (strlen($xorHash) < strlen($itemHash)) { |
||
| 45 | $xorHash = str_pad($xorHash, strlen($itemHash)); |
||
| 46 | } |
||
| 47 | $xorHash ^= $itemHash; |
||
| 48 | } |
||
| 49 | } else { |
||
| 50 | foreach ($data as $key => $item) { |
||
| 51 | $itemPath = $path . '/' . $key; |
||
| 52 | $itemHash = md5($itemPath . $this->xorHash($item, $itemPath), true); |
||
| 53 | if (strlen($xorHash) < strlen($itemHash)) { |
||
| 54 | $xorHash = str_pad($xorHash, strlen($itemHash)); |
||
| 55 | } |
||
| 56 | $xorHash ^= $itemHash; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | return $xorHash; |
||
| 61 | } |
||
| 62 | |||
| 63 | $dataKeys = get_object_vars($data); |
||
| 64 | foreach ($dataKeys as $key => $value) { |
||
| 65 | $propertyPath = $path . '/' . |
||
| 66 | JsonPointer::escapeSegment($key, (bool)($this->options & JsonDiff::JSON_URI_FRAGMENT_ID)); |
||
| 67 | $propertyHash = $propertyPath . $this->xorHash($value, $propertyPath); |
||
| 68 | if (strlen($xorHash) < strlen($propertyHash)) { |
||
| 69 | $xorHash = str_pad($xorHash, strlen($propertyHash)); |
||
| 70 | } |
||
| 71 | $xorHash ^= $propertyHash; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $xorHash; |
||
| 75 | } |
||
| 76 | } |