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 |
||
17 | class SubmitControllerCreate extends AbstractController |
||
18 | { |
||
19 | use ValidateVersion; |
||
20 | |||
21 | /** |
||
22 | * Statistics model object. |
||
23 | * |
||
24 | * @var StatsModel |
||
25 | * @since 1.0 |
||
26 | */ |
||
27 | private $model; |
||
28 | |||
29 | /** |
||
30 | * Allowed Database Types. |
||
31 | * |
||
32 | * @var array |
||
33 | * @since 1.0 |
||
34 | */ |
||
35 | private $databaseTypes = [ |
||
36 | 'mysql', |
||
37 | 'mysqli', |
||
38 | 'pdomysql', |
||
39 | 'postgresql', |
||
40 | 'sqlazure', |
||
41 | 'sqlsrv', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Constructor. |
||
46 | * |
||
47 | * @param StatsModel $model Statistics model object. |
||
48 | * |
||
49 | * @since 1.0 |
||
50 | */ |
||
51 | 1 | public function __construct(StatsModel $model) |
|
55 | |||
56 | /** |
||
57 | * Execute the controller. |
||
58 | * |
||
59 | * @return boolean |
||
60 | * |
||
61 | * @since 1.0 |
||
62 | */ |
||
63 | 3 | public function execute() |
|
129 | |||
130 | /** |
||
131 | * Check the CMS version. |
||
132 | * |
||
133 | * @param string $version The version number to check. |
||
134 | * |
||
135 | * @return string|boolean The version number on success or boolean false on failure. |
||
136 | * |
||
137 | * @since 1.0 |
||
138 | */ |
||
139 | 3 | View Code Duplication | private function checkCMSVersion($version) |
|
|||
140 | { |
||
141 | 3 | $version = $this->validateVersionNumber($version); |
|
142 | |||
143 | // If the version number is invalid, don't go any further |
||
144 | 3 | if ($version === false) |
|
145 | 3 | { |
|
146 | 1 | return false; |
|
147 | } |
||
148 | |||
149 | // Joomla only uses major.minor.patch so everything else is invalid |
||
150 | 2 | $explodedVersion = explode('.', $version); |
|
151 | |||
152 | 2 | if (count($explodedVersion) > 3) |
|
153 | 2 | { |
|
154 | 1 | return false; |
|
155 | } |
||
156 | |||
157 | // Import the valid release listing |
||
158 | 1 | $path = APPROOT . '/versions/joomla.json'; |
|
159 | |||
160 | 1 | if (!file_exists($path)) |
|
161 | 1 | { |
|
162 | throw new \RuntimeException('Missing Joomla! release listing', 500); |
||
163 | } |
||
164 | |||
165 | 1 | $validVersions = json_decode(file_get_contents($path), true); |
|
166 | |||
167 | // Check that the version is in our valid release list |
||
168 | 1 | if (!in_array($version, $validVersions)) |
|
169 | 1 | { |
|
170 | return false; |
||
171 | } |
||
172 | |||
173 | 1 | return $version; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Check the database type |
||
178 | * |
||
179 | * @param string $database The database type to check. |
||
180 | * |
||
181 | * @return string|boolean The database type on success or boolean false on failure. |
||
182 | * |
||
183 | * @since 1.0 |
||
184 | */ |
||
185 | 3 | private function checkDatabaseType($database) |
|
194 | |||
195 | /** |
||
196 | * Check the PHP version |
||
197 | * |
||
198 | * @param string $version The version number to check. |
||
199 | * |
||
200 | * @return string|boolean The version number on success or boolean false on failure. |
||
201 | * |
||
202 | * @since 1.0 |
||
203 | */ |
||
204 | 3 | View Code Duplication | private function checkPHPVersion($version) |
240 | } |
||
241 |
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.