| Conditions | 27 |
| Paths | 15 |
| Total Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 756 |
| Changes | 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 |
||
| 21 | public function calculate(array $map, array $object, array $endpoint_object): array |
||
| 22 | { |
||
| 23 | $diff = []; |
||
| 24 | foreach ($map as $value) { |
||
| 25 | $attr = $value['name']; |
||
| 26 | $exists = isset($endpoint_object[$attr]); |
||
| 27 | |||
| 28 | if ($value['ensure'] === AttributeMapInterface::ENSURE_EXISTS && ($exists === true || !isset($object[$attr]))) { |
||
| 29 | continue; |
||
| 30 | } |
||
| 31 | if (($value['ensure'] === AttributeMapInterface::ENSURE_LAST || $value['ensure'] === AttributeMapInterface::ENSURE_EXISTS) && isset($object[$attr])) { |
||
| 32 | if ($exists && is_array($object[$attr]) && is_array($endpoint_object[$attr]) && Helper::arrayEqual($endpoint_object[$attr], $object[$attr])) { |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | if ($exists && $object[$attr] === $endpoint_object[$attr]) { |
||
| 36 | continue; |
||
| 37 | } |
||
| 38 | |||
| 39 | $diff[$attr] = [ |
||
| 40 | 'action' => AttributeMapInterface::ACTION_REPLACE, |
||
| 41 | 'value' => $object[$attr], |
||
| 42 | ]; |
||
| 43 | } elseif ($value['ensure'] === AttributeMapInterface::ENSURE_ABSENT && isset($endpoint_object[$attr]) || isset($endpoint_object[$attr]) && !isset($object[$attr]) && $value['ensure'] !== AttributeMapInterface::ENSURE_MERGE) { |
||
| 44 | $diff[$attr] = [ |
||
| 45 | 'action' => AttributeMapInterface::ACTION_REMOVE, |
||
| 46 | ]; |
||
| 47 | } elseif ($value['ensure'] === AttributeMapInterface::ENSURE_MERGE && isset($object[$attr])) { |
||
| 48 | $new_values = []; |
||
| 49 | |||
| 50 | foreach ($object[$attr] as $val) { |
||
| 51 | if (!$exists) { |
||
| 52 | $new_values[] = $val; |
||
| 53 | } elseif (is_array($endpoint_object[$attr]) && in_array($val, $endpoint_object[$attr]) || $val === $endpoint_object[$attr]) { |
||
| 54 | continue; |
||
| 55 | } else { |
||
| 56 | $new_values[] = $val; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if (!empty($new_values)) { |
||
| 61 | $diff[$attr] = [ |
||
| 62 | 'action' => AttributeMapInterface::ACTION_ADD, |
||
| 63 | 'value' => $new_values, |
||
| 64 | ]; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | return $diff; |
||
| 70 | } |
||
| 71 | } |
||
| 72 |