| Conditions | 14 |
| Paths | 8 |
| Total Lines | 27 |
| Code Lines | 11 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 declare(strict_types=1); |
||
| 19 | protected function normalizePayload($value, $excludeExistingMedia = true): array |
||
| 20 | { |
||
| 21 | $payload = $this->emptyPayload(); |
||
| 22 | |||
| 23 | if(!$value || !is_array($value)) return $payload; |
||
| 24 | |||
| 25 | foreach([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) { |
||
| 26 | if(isset($value[$action])){ |
||
| 27 | |||
| 28 | // Front sometimes gives us a 0 => null array when an image is added and detached at the same time. |
||
| 29 | // Here we check for these fake entries and exclude them. |
||
| 30 | if(!is_array($value[$action]) || (count($value[$action]) === 1 && key($value[$action]) === 0 && is_null(reset($value[$action])))) { |
||
| 31 | continue; |
||
| 32 | } |
||
| 33 | |||
| 34 | $payload[$action] = $value[$action]; |
||
| 35 | |||
| 36 | // A replace NULL value passed from frontend is expected as a default so w'll need to remove it here to avoid unwanted validation. |
||
| 37 | if($excludeExistingMedia && $action == MediaRequest::REPLACE && is_array($payload[$action])) { |
||
| 38 | foreach($payload[$action] as $k => $v) { |
||
| 39 | if(is_null($v)) unset($payload[$action][$k]); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return $payload; |
||
| 46 | } |
||
| 76 |