Conditions | 22 |
Paths | 64 |
Total Lines | 43 |
Code Lines | 29 |
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 |
||
64 | protected function isChanged($prop, $val) |
||
65 | { |
||
66 | $getter = (method_exists($this, $prop)) ? $prop : 'get'.ucfirst($prop); |
||
67 | $current = $this->$getter(); |
||
68 | if ('category' == $prop) { |
||
69 | $currentId = ($current) ? $current->getId() : ''; |
||
70 | $newId = ($val) ? $val->getId() : null; |
||
71 | if ($currentId != $newId) { |
||
72 | $this->addChange($prop, [$currentId, $newId]); |
||
73 | } |
||
74 | } elseif ($current !== $val) { |
||
75 | if ($current instanceof Collection || $val instanceof Collection) { |
||
76 | if (!isset($this->changes[$prop])) { |
||
77 | $this->changes[$prop] = [ |
||
78 | 'added' => [], |
||
79 | 'removed' => [], |
||
80 | ]; |
||
81 | } |
||
82 | |||
83 | if (is_object($val)) { |
||
84 | // Entity is getting added to the collection |
||
85 | $this->changes['added'][] = method_exists($val, 'getId') ? $val->getId() : (string) $val; |
||
86 | } else { |
||
87 | // Entity is getting removed from the collection |
||
88 | $this->changes['removed'][] = $val; |
||
89 | } |
||
90 | } else { |
||
91 | if ($current instanceof \DateTime) { |
||
92 | $current = $current->format('c'); |
||
93 | } elseif (is_object($current)) { |
||
94 | $current = (method_exists($current, 'getId')) ? $current->getId() : (string) $current; |
||
95 | } elseif (('' === $current && null === $val) || (null === $current && '' === $val)) { |
||
96 | // Ingore empty conversion (but allow 0 to '' or null) |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | if ($val instanceof \DateTime) { |
||
101 | $val = $val->format('c'); |
||
102 | } elseif (is_object($val)) { |
||
103 | $val = (method_exists($val, 'getId')) ? $val->getId() : (string) $val; |
||
104 | } |
||
105 | |||
106 | $this->addChange($prop, [$current, $val]); |
||
107 | } |
||
150 |