Conditions | 11 |
Paths | 14 |
Total Lines | 35 |
Code Lines | 18 |
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 |
||
112 | public static function formatAttribute($value): ?array |
||
113 | { |
||
114 | static $realEmpty = [null, '', false]; |
||
115 | |||
116 | if (in_array($value, $realEmpty, true)) { |
||
117 | return null; |
||
118 | } |
||
119 | |||
120 | if (!is_scalar($value) && !is_array($value)) { |
||
121 | throw new \InvalidArgumentException(static::ERROR_INVALID_ATTRIBUTES); |
||
122 | } |
||
123 | |||
124 | if (is_scalar($value)) { |
||
125 | $value = [(string)$value]; |
||
126 | } |
||
127 | |||
128 | if (count($value) === 1 && in_array(current($value), $realEmpty, true)) { |
||
129 | return null; |
||
130 | } |
||
131 | |||
132 | $newValue = []; |
||
133 | foreach ($value as $v) { |
||
134 | if (!is_scalar($v)) { |
||
135 | throw new \InvalidArgumentException(static::ERROR_INVALID_ATTRIBUTES); |
||
136 | } |
||
137 | |||
138 | foreach (explode(' ', (string)$v) as $w) { |
||
139 | if ('' === $w) { |
||
140 | continue; |
||
141 | } |
||
142 | $newValue[$w] = $w; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return $newValue; |
||
147 | } |
||
168 |