Conditions | 14 |
Paths | 22 |
Total Lines | 56 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
47 | public function toArray(array $options = []): array |
||
48 | { |
||
49 | $classMetadata = $this->getRepository() |
||
50 | ->getClassMetadata(); |
||
51 | |||
52 | $propertiesFillable = $this->getPropertiesFillable(); |
||
53 | |||
54 | $array = []; |
||
55 | |||
56 | foreach ($propertiesFillable as $property) { |
||
57 | if ($this->isOnly($property, $options)) { |
||
58 | $key = $property->getName(); |
||
59 | $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null; |
||
60 | |||
61 | if (is_object($this->$key)) { |
||
62 | if ($this->$key instanceof DateTime) { |
||
63 | if ($this->$key) { |
||
64 | $dateFormat = 'Y-m-d'; |
||
65 | |||
66 | if ($metaDataKey) { |
||
67 | switch ($metaDataKey['type']) { |
||
68 | case 'datetime': |
||
69 | $dateFormat = 'Y-m-d H:i:s'; |
||
70 | break; |
||
71 | case 'time': |
||
72 | $dateFormat = 'H:i:s'; |
||
73 | break; |
||
74 | default: |
||
75 | break; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | $array[$key] = $this->$key->format($dateFormat); |
||
80 | } |
||
81 | } elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) { |
||
82 | $array[$key] = array_map(function ($item) { |
||
83 | return $item->getId(); |
||
84 | }, $this->$key->getValues()); |
||
85 | } else { |
||
86 | if (method_exists($this->$key, 'getId')) { |
||
87 | $array[$key] = $this->$key->getId(); |
||
88 | } else { |
||
89 | $array[$key] = $this->$key; |
||
90 | } |
||
91 | } |
||
92 | } else { |
||
93 | if (in_array($metaDataKey['type'], ['decimal', 'float'])) { |
||
94 | $array[$key] = (float) $this->$key; |
||
95 | } else { |
||
96 | $array[$key] = $this->$key; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | return $array; |
||
103 | } |
||
105 |