Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Attribute often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Attribute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Attribute extends JsonObject |
||
21 | { |
||
22 | // identifiers for the Api Product Attribute Types: |
||
23 | const T_UNKNOWN = 'unknown'; // zero, should evaluate to false |
||
24 | |||
25 | const PROP_VALUE = "value"; |
||
26 | const PROP_KEY = "key"; |
||
27 | const PROP_NAME = "name"; |
||
28 | const PROP_CURRENCY_CODE = "currencyCode"; |
||
29 | const PROP_CENT_AMOUNT = "centAmount"; |
||
30 | const PROP_TYPE_ID = "typeId"; |
||
31 | const PROP_ID = "id"; |
||
32 | const PROP_LABEL = "label"; |
||
33 | |||
34 | const API_BOOL = 'boolean'; |
||
35 | const API_NUMBER = 'number'; |
||
36 | const API_TEXT = 'text'; |
||
37 | const API_LTEXT = 'ltext'; |
||
38 | const API_LENUM = 'lenum'; |
||
39 | const API_ENUM = 'enum'; |
||
40 | const API_MONEY = 'money'; |
||
41 | const API_DATE = 'date'; |
||
42 | const API_TIME = 'time'; |
||
43 | const API_DATETIME = 'datetime'; |
||
44 | const API_SET = 'set'; |
||
45 | const API_NESTED = 'nested'; |
||
46 | const API_REFERENCE = 'reference'; |
||
47 | |||
48 | protected static $types = []; |
||
49 | |||
50 | 27 | public function fieldDefinitions() |
|
57 | |||
58 | 27 | public function fieldDefinition($field) |
|
59 | { |
||
60 | 27 | if ($field == static::PROP_VALUE) { |
|
61 | 26 | if (isset(static::$types[$this->getName()])) { |
|
62 | 19 | $fieldDefinition = static::$types[$this->getName()]; |
|
63 | 19 | if (!$fieldDefinition instanceof AttributeDefinition) { |
|
64 | return null; |
||
65 | } |
||
66 | 19 | $fieldType = $fieldDefinition->getType(); |
|
67 | 19 | if (!$fieldType instanceof AttributeType) { |
|
68 | return null; |
||
69 | } |
||
70 | 19 | return $fieldType->fieldTypeDefinition(); |
|
71 | } |
||
72 | 8 | return null; |
|
73 | } |
||
74 | 27 | return parent::fieldDefinition($field); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $attributeName |
||
79 | * @param $value |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 19 | protected function getApiType($attributeName, $value) |
|
100 | |||
101 | /** |
||
102 | * @param AttributeDefinition $definition |
||
103 | * @return $this |
||
104 | */ |
||
105 | 19 | public function setAttributeDefinition(AttributeDefinition $definition) |
|
111 | |||
112 | /** |
||
113 | * @param string $field |
||
114 | */ |
||
115 | 20 | protected function initialize($field) |
|
124 | |||
125 | /** |
||
126 | * @param $value |
||
127 | * @return string |
||
128 | */ |
||
129 | 15 | protected function guessApiType($value) |
|
130 | { |
||
131 | $map = [ |
||
132 | 15 | 'guessUnknown', |
|
133 | 'guessBool', |
||
134 | 'guessTextLike', |
||
135 | 'guessNumber', |
||
136 | 'guessEnum', |
||
137 | 'guessLocalizedEnum', |
||
138 | 'guessMoney', |
||
139 | 'guessReference', |
||
140 | 'guessLocalizedText', |
||
141 | 'guessNested', |
||
142 | 'guessSet', |
||
143 | ]; |
||
144 | |||
145 | 15 | foreach ($map as $function) { |
|
146 | 15 | if ($type = $this->$function($value)) { |
|
147 | 15 | return $type; |
|
148 | } |
||
149 | } |
||
150 | |||
151 | return static::T_UNKNOWN; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param $value |
||
156 | * @param string[] $keys |
||
157 | * @return bool |
||
158 | */ |
||
159 | 9 | protected function hasKeys($value, $keys) |
|
167 | |||
168 | 15 | protected function guessUnknown($value) |
|
172 | |||
173 | 13 | protected function guessTextLike($value) |
|
177 | |||
178 | 14 | protected function guessBool($value) |
|
182 | |||
183 | 11 | protected function guessNumber($value) |
|
187 | |||
188 | 9 | View Code Duplication | protected function guessEnum($value) |
196 | |||
197 | 8 | View Code Duplication | protected function guessLocalizedEnum($value) |
205 | |||
206 | 7 | View Code Duplication | protected function guessMoney($value) |
214 | |||
215 | 6 | View Code Duplication | protected function guessReference($value) |
223 | |||
224 | 4 | protected function guessLocalizedText($value) |
|
232 | |||
233 | 2 | protected function guessSet($value) |
|
234 | { |
||
235 | 2 | if (is_array($value)) { |
|
236 | 2 | return static::API_SET; |
|
237 | } |
||
238 | |||
239 | return null; |
||
240 | } |
||
241 | |||
242 | 3 | protected function guessNested($value) |
|
253 | } |
||
254 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: