| Conditions | 14 |
| Paths | 23 |
| Total Lines | 51 |
| Code Lines | 34 |
| Lines | 7 |
| Ratio | 13.73 % |
| 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 |
||
| 188 | public function toArray(array $options = null) |
||
| 189 | { |
||
| 190 | $classMetadata = $this->getRepository()->getClassMetadata(); |
||
| 191 | $array = []; |
||
| 192 | |||
| 193 | foreach ($this->getFillable() as $key) { |
||
| 194 | $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null; |
||
| 195 | |||
| 196 | if ($this->checkOnyExceptInArray($key, $options)) { |
||
| 197 | if (is_object($this->$key)) { |
||
| 198 | if ($this->$key instanceof DateTime) { |
||
| 199 | if ($this->$key) { |
||
| 200 | $dateFormat = 'Y-m-d'; |
||
| 201 | |||
| 202 | if ($metaDataKey) { |
||
| 203 | switch ($metaDataKey['type']) { |
||
| 204 | case 'datetime': |
||
| 205 | $dateFormat = 'Y-m-d H:i:s'; |
||
| 206 | break; |
||
| 207 | |||
| 208 | case 'time': |
||
| 209 | $dateFormat = 'H:i:s'; |
||
| 210 | break; |
||
| 211 | |||
| 212 | default: |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | $array[$key] = $this->$key->format($dateFormat); |
||
| 217 | } |
||
| 218 | View Code Duplication | } elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) { |
|
| 219 | $ids = []; |
||
| 220 | foreach ($this->$key->getValues() as $item) { |
||
| 221 | $ids[] = $item->getId(); |
||
| 222 | } |
||
| 223 | $array[$key] = $ids; |
||
| 224 | } else { |
||
| 225 | $array[$key] = $this->$key->getId(); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | if ($metaDataKey['type'] == 'decimal') { |
||
| 229 | $array[$key] = (float) $this->$key; |
||
| 230 | } else { |
||
| 231 | $array[$key] = $this->$key; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | return $array; |
||
| 238 | } |
||
| 239 | } |
||
| 240 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.