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 ArrayType 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 ArrayType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ArrayType extends AbstractType |
||
14 | { |
||
15 | |||
16 | private static $classTypes = array( |
||
17 | 'integer' => 'Integer', |
||
18 | 'int' => 'Integer', |
||
19 | 'int32' => 'Integer', |
||
20 | 'int64' => 'Integer', |
||
21 | 'long' => 'Integer', |
||
22 | 'float' => 'Number', |
||
23 | 'double' => 'Number', |
||
24 | 'string' => 'String', |
||
25 | 'byte' => 'String', |
||
26 | 'binary' => 'String', |
||
27 | 'password' => 'String', |
||
28 | 'enum' => 'String', |
||
29 | 'boolean' => 'Boolean', |
||
30 | 'bool' => 'Boolean', |
||
31 | 'array' => 'Array', |
||
32 | 'csv' => 'Array', |
||
33 | 'ssv' => 'Array', |
||
34 | 'tsv' => 'Array', |
||
35 | 'pipes' => 'Array', |
||
36 | 'date' => 'Date', |
||
37 | 'datetime' => 'Date', |
||
38 | 'date-time' => 'Date', |
||
39 | 'object' => 'Object', |
||
40 | ); |
||
41 | private static $collectionFormats = array( |
||
42 | 'array' => 'csv', |
||
43 | 'csv' => 'csv', |
||
44 | 'ssv' => 'ssv', |
||
45 | 'tsv' => 'tsv', |
||
46 | 'pipes' => 'pipes', |
||
47 | 'multi' => 'multi', |
||
48 | ); |
||
49 | |||
50 | /** |
||
51 | * @var AbstractType |
||
52 | */ |
||
53 | private $Items = null; |
||
54 | private $minItems = null; |
||
55 | private $maxItems = null; |
||
56 | private $collectionFormat = null; |
||
57 | |||
58 | View Code Duplication | protected function parseDefinition($definition) |
|
71 | |||
72 | private function parseFormat($definition, $match) |
||
88 | |||
89 | private function parseItems($definition, $match) |
||
95 | |||
96 | View Code Duplication | private function parseRange($definition, $match) |
|
115 | |||
116 | /** |
||
117 | * @param string $command The comment command |
||
118 | * @param string $data Any data added after the command |
||
119 | * @return \SwaggerGen\Swagger\Type\AbstractType|boolean |
||
120 | */ |
||
121 | public function handleCommand($command, $data = null) |
||
158 | |||
159 | public function toArray() |
||
169 | |||
170 | private function validateItems($items) |
||
189 | |||
190 | public function __toString() |
||
194 | |||
195 | } |
||
196 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.