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 PHP_CodeSniffer_Standards_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 | * @inheritdoc |
||
78 | */ |
||
79 | 4 | protected function processTokenWithinScope( |
|
148 | |||
149 | /** |
||
150 | * Checks if the given method name relates to a setter function. |
||
151 | * |
||
152 | * Its not a simple strpos because a method name like "setupDatabase" would be catched. |
||
153 | * We check that the letters until the first upper case character equals "set". |
||
154 | * This way we expect that after "set" follows an upper case letter. |
||
155 | * |
||
156 | * @param string $methodName |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | 4 | private function checkIfSetterFunction($methodName) |
|
166 | |||
167 | /** |
||
168 | * Fixes if no return statement is found. |
||
169 | * |
||
170 | * @param PHP_CodeSniffer_File $phpcsFile |
||
171 | * @param int $closingBracePtr |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | 1 | private function fixNoReturnFound(PHP_CodeSniffer_File $phpcsFile, $closingBracePtr) |
|
188 | |||
189 | /** |
||
190 | * Fixes the return value of a function to $this. |
||
191 | * |
||
192 | * @param PHP_CodeSniffer_File $phpcsFile |
||
193 | * @param int $returnPtr |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | 1 | private function fixMustReturnThis(PHP_CodeSniffer_File $phpcsFile, $returnPtr) |
|
212 | } |
||
213 |
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.