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:
1 | <?php |
||
14 | abstract class AbstractObject |
||
15 | { |
||
16 | |||
17 | private static $mime_types = array( |
||
18 | 'fileform' => 'multipart/form-data', |
||
19 | 'form' => 'application/x-www-form-urlencoded', |
||
20 | 'json' => 'application/json', |
||
21 | 'text' => 'text/plain', |
||
22 | 'utf8' => 'text/plain; charset=utf-8', |
||
23 | 'yml' => 'application/x-yaml', |
||
24 | 'yaml' => 'application/x-yaml', |
||
25 | 'php' => 'text/x-php', |
||
26 | 'xml' => 'text/xml', |
||
27 | ); |
||
28 | |||
29 | /** |
||
30 | * @var AbstractObject |
||
31 | */ |
||
32 | private $parent; |
||
33 | |||
34 | /** |
||
35 | * Map of extensions and their (trimmed) values |
||
36 | * @var string[] |
||
37 | */ |
||
38 | private $extensions = array(); |
||
39 | |||
40 | public function __construct(AbstractObject $parent = null) |
||
44 | |||
45 | /** |
||
46 | * @return AbstractObject |
||
47 | */ |
||
48 | protected function getParent() |
||
52 | |||
53 | protected function getParentClass($classname) |
||
60 | |||
61 | /** |
||
62 | * @return \SwaggerGen\Swagger\Swagger |
||
63 | */ |
||
64 | protected function getSwagger() |
||
65 | { |
||
66 | return $this->parent->getSwagger(); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $command |
||
71 | * @param string $data |
||
72 | * @return \SwaggerGen\Swagger\AbstractObject|boolean |
||
73 | */ |
||
74 | public function handleCommand($command, $data = null) |
||
83 | |||
84 | /** |
||
85 | * @return array |
||
86 | */ |
||
87 | public function toArray() |
||
91 | |||
92 | /** |
||
93 | * Translate consumes from shortcuts |
||
94 | * @param String[] $mimeTypes |
||
95 | * @return String[] |
||
96 | */ |
||
97 | protected static function translateMimeTypes($mimeTypes) |
||
107 | |||
108 | /** |
||
109 | * Trim whitespace from a multibyte string |
||
110 | * @param string $string |
||
111 | * @return string |
||
112 | */ |
||
113 | public static function trim($string) |
||
117 | |||
118 | /** |
||
119 | * Filter all items from an array where the value is either null or an |
||
120 | * empty array. |
||
121 | * @param Array $array |
||
122 | * @return Array |
||
123 | */ |
||
124 | public static function arrayFilterNull($array) |
||
130 | |||
131 | /** |
||
132 | * Recursively call toArray() on all objects to return an array |
||
133 | * @param Array $array |
||
134 | * @return Array |
||
135 | */ |
||
136 | public static function objectsToArray(&$array) |
||
142 | |||
143 | /** |
||
144 | * Shifts the first word off a text line and returns it |
||
145 | * @param string $data |
||
146 | * @return string|bool Either the first word or false if no more words available |
||
147 | */ |
||
148 | View Code Duplication | public static function wordShift(&$data) |
|
156 | |||
157 | /** |
||
158 | * Splits a text line in all it's words |
||
159 | * @param string $data |
||
160 | * @return string |
||
161 | */ |
||
162 | public static function wordSplit($data) |
||
166 | |||
167 | } |
||
168 |
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.