| Conditions | 11 |
| Paths | 8 |
| Total Lines | 35 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 76 | public function write($value, WriterContext $context): ?array |
||
| 77 | { |
||
| 78 | if ($value === null) { |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | |||
| 82 | $arrayIsObject = is_string(key($value)); |
||
| 83 | $enableScalarAdapters = $context->enableScalarAdapters(); |
||
| 84 | $serializeNull = $context->serializeNull(); |
||
| 85 | $typeAdapterProvider = $context->getTypeAdapterProvider(); |
||
| 86 | $result = []; |
||
| 87 | |||
| 88 | foreach ($value as $key => $item) { |
||
| 89 | if ($item === null && !$serializeNull) { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!$enableScalarAdapters && is_scalar($item)) { |
||
| 94 | $result[$arrayIsObject ? (string)$key : (int)$key] = $item; |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | $itemValue = null; |
||
|
|
|||
| 99 | $type = TypeToken::createFromVariable($item); |
||
| 100 | $adapter = $this->adapters[$type->rawType] ?? $this->adapters[$type->rawType] = $typeAdapterProvider->getAdapter($type); |
||
| 101 | $itemValue = $adapter->write($item, $context); |
||
| 102 | |||
| 103 | if ($itemValue === null && !$serializeNull) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | $result[$arrayIsObject ? (string)$key : (int)$key] = $itemValue; |
||
| 108 | } |
||
| 109 | |||
| 110 | return $result; |
||
| 111 | } |
||
| 128 |