Conditions | 15 |
Paths | 25 |
Total Lines | 61 |
Code Lines | 40 |
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 |
||
7 | public function toArray(array $options = []): array |
||
8 | { |
||
9 | $reflectionProperty = new ReflectionProperty(get_called_class(), $key); |
||
10 | $annotationReader = new AnnotationReader(); |
||
11 | $propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty); |
||
12 | |||
13 | $array = []; |
||
14 | |||
15 | $classMetadata = $this->getRepository() |
||
16 | ->getClassMetadata(); |
||
17 | |||
18 | foreach ($this->getFillable() as $key) { |
||
19 | $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null; |
||
20 | |||
21 | if ($this->checkOnyExceptInArray($key, $options)) { |
||
22 | if (is_object($this->$key)) { |
||
23 | if ($this->$key instanceof DateTime) { |
||
24 | if ($this->$key) { |
||
25 | $dateFormat = 'Y-m-d'; |
||
26 | |||
27 | if ($metaDataKey) { |
||
28 | switch ($metaDataKey['type']) { |
||
29 | case 'datetime': |
||
30 | $dateFormat = 'Y-m-d H:i:s'; |
||
31 | break; |
||
32 | case 'time': |
||
33 | $dateFormat = 'H:i:s'; |
||
34 | break; |
||
35 | default: |
||
36 | break; |
||
37 | } |
||
38 | } |
||
39 | |||
40 | $array[$key] = $this->$key->format($dateFormat); |
||
41 | } |
||
42 | } elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) { |
||
43 | $ids = []; |
||
44 | |||
45 | foreach ($this->$key->getValues() as $item) { |
||
46 | $ids[] = $item->getId(); |
||
47 | } |
||
48 | |||
49 | $array[$key] = $ids; |
||
50 | } else { |
||
51 | if (method_exists($this->$key, 'getId')) { |
||
52 | $array[$key] = $this->$key->getId(); |
||
53 | } else { |
||
54 | $array[$key] = $this->$key; |
||
55 | } |
||
56 | } |
||
57 | } else { |
||
58 | if ($metaDataKey['type'] == 'decimal') { |
||
59 | $array[$key] = (float) $this->$key; |
||
60 | } else { |
||
61 | $array[$key] = $this->$key; |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $array; |
||
68 | } |
||
70 |