Conditions | 11 |
Paths | 7 |
Total Lines | 38 |
Code Lines | 22 |
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 |
||
94 | public function write($value, WriterContext $context): ?array |
||
95 | { |
||
96 | if ($value === null) { |
||
97 | return null; |
||
98 | } |
||
99 | |||
100 | $serializeNull = $context->serializeNull(); |
||
101 | $result = []; |
||
102 | $arrayIsObject = is_string(key($value)); |
||
103 | |||
104 | if ($arrayIsObject !== $this->stringKeys) { |
||
105 | throw new JsonSyntaxException( |
||
106 | sprintf( |
||
107 | 'Expected %s, but found %s for key', |
||
108 | $this->stringKeys ? 'string' : 'integer', |
||
109 | $arrayIsObject ? 'string' : 'integer' |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | foreach ($value as $key => $item) { |
||
115 | if ($item === null && !$serializeNull) { |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | $itemValue = $this->valueTypeAdapter->write($item, $context); |
||
120 | if ($itemValue === null && !$serializeNull) { |
||
121 | continue; |
||
122 | } |
||
123 | |||
124 | if ($this->stringKeys) { |
||
125 | $result[(string)$key] = $itemValue; |
||
126 | } else { |
||
127 | $result[] = $itemValue; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return $result; |
||
132 | } |
||
144 |