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 FluentSetterSniff extends AbstractScopeSniff |
||
18 | { |
||
19 | /** |
||
20 | * Code when multiple return statements are found. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | const CODE_MULTIPLE_RETURN_FOUND = 'MultipleReturnFound'; |
||
25 | |||
26 | /** |
||
27 | * Code when the method does not return $this. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | const CODE_MUST_RETURN_THIS = 'MustReturnThis'; |
||
32 | |||
33 | /** |
||
34 | * Code when no return statement is found. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | const CODE_NO_RETURN_FOUND = 'NoReturnFound'; |
||
39 | |||
40 | /** |
||
41 | * Error message when no return statement is found. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | const ERROR_NO_RETURN_FOUND = 'Method "%s" has no return statement'; |
||
46 | |||
47 | /** |
||
48 | * Error message when multiple return statements are found. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | const ERROR_MULTIPLE_RETURN_FOUND = 'Method "%s" has multiple return statements'; |
||
53 | |||
54 | /** |
||
55 | * Error message when the method does not return $this. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | const ERROR_MUST_RETURN_THIS = 'The method "%s" must return $this'; |
||
60 | |||
61 | /** |
||
62 | * Specifies how an identation looks like. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | public $identation = ' '; |
||
67 | |||
68 | /** |
||
69 | * FluentSetterSniff constructor. |
||
70 | */ |
||
71 | 4 | public function __construct() |
|
75 | |||
76 | /** |
||
77 | * Processes the tokens that this test is listening for. |
||
78 | * |
||
79 | 4 | * @param File $phpcsFile The file where this token was found. |
|
80 | * @param int $stackPtr The position in the stack where this token was found. |
||
81 | * @param int $currScope The position in the tokens array that opened the scope that this test is listening for. |
||
82 | * |
||
83 | * @return void |
||
84 | 4 | */ |
|
85 | 4 | protected function processTokenWithinScope( |
|
154 | |||
155 | /** |
||
156 | * Checks if the given method name relates to a setter function. |
||
157 | * |
||
158 | * Its not a simple strpos because a method name like "setupDatabase" would be catched. |
||
159 | * We check that the letters until the first upper case character equals "set". |
||
160 | * This way we expect that after "set" follows an upper case letter. |
||
161 | * |
||
162 | * @param string $methodName Current method name |
||
163 | * |
||
164 | * @return bool Indicator if the given method is a setter function |
||
165 | 4 | */ |
|
166 | private function checkIfSetterFunction(string $methodName): bool |
||
172 | |||
173 | /** |
||
174 | * Fixes if no return statement is found. |
||
175 | * |
||
176 | * @param File $phpcsFile The php cs file |
||
177 | * @param int $closingBracePtr Pointer to the closing curly brace of the function |
||
178 | * |
||
179 | * @return void |
||
180 | 1 | */ |
|
181 | private function fixNoReturnFound(File $phpcsFile, int $closingBracePtr) |
||
194 | |||
195 | /** |
||
196 | * Fixes the return value of a function to $this. |
||
197 | * |
||
198 | * @param File $phpcsFile The php cs file |
||
199 | * @param int $returnPtr Pointer to the return token |
||
200 | * |
||
201 | * @return void |
||
202 | 1 | */ |
|
203 | private function fixMustReturnThis(File $phpcsFile, $returnPtr) |
||
218 | |||
219 | /** |
||
220 | * Processes a token that is found outside the scope that this test is listening to. |
||
221 | * |
||
222 | * @param File $phpcsFile The php cs file |
||
223 | * @param int $stackPtr Pointer to the token |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) |
||
231 | } |
||
232 |
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.