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 | 26 | public function fieldDefinitions() |
|
| 57 | |||
| 58 | 26 | public function fieldDefinition($field) |
|
| 59 | { |
||
| 60 | 26 | if ($field == static::PROP_VALUE) { |
|
| 61 | 25 | if (isset(static::$types[$this->getName()])) { |
|
| 62 | 18 | $fieldDefinition = static::$types[$this->getName()]; |
|
| 63 | 18 | if (!$fieldDefinition instanceof AttributeDefinition) { |
|
| 64 | return null; |
||
| 65 | } |
||
| 66 | 18 | $fieldType = $fieldDefinition->getType(); |
|
| 67 | 18 | if (!$fieldType instanceof AttributeType) { |
|
| 68 | return null; |
||
| 69 | } |
||
| 70 | 18 | return $fieldType->fieldTypeDefinition(); |
|
| 71 | } |
||
| 72 | 7 | return null; |
|
| 73 | } |
||
| 74 | 26 | return parent::fieldDefinition($field); |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $attributeName |
||
| 79 | * @param $value |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | 18 | protected function getApiType($attributeName, $value) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param AttributeDefinition $definition |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | 18 | public function setAttributeDefinition(AttributeDefinition $definition) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $field |
||
| 114 | */ |
||
| 115 | 19 | protected function initialize($field) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param $value |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 14 | protected function guessApiType($value) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param $value |
||
| 156 | * @param string[] $keys |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | 8 | protected function hasKeys($value, $keys) |
|
| 167 | |||
| 168 | 14 | protected function guessUnknown($value) |
|
| 172 | |||
| 173 | 12 | protected function guessTextLike($value) |
|
| 177 | |||
| 178 | 13 | protected function guessBool($value) |
|
| 182 | |||
| 183 | 10 | protected function guessNumber($value) |
|
| 187 | |||
| 188 | 8 | View Code Duplication | protected function guessEnum($value) |
| 196 | |||
| 197 | 7 | View Code Duplication | protected function guessLocalizedEnum($value) |
| 205 | |||
| 206 | 6 | View Code Duplication | protected function guessMoney($value) |
| 214 | |||
| 215 | 5 | View Code Duplication | protected function guessReference($value) |
| 223 | |||
| 224 | 4 | protected function guessLocalizedText($value) |
|
| 232 | |||
| 233 | 2 | protected function guessSet($value) |
|
| 241 | |||
| 242 | 3 | protected function guessNested($value) |
|
| 253 | } |
||
| 254 |
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: