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 |
||
18 | class MethodDocSniff implements PHP_CodeSniffer_Sniff |
||
19 | { |
||
20 | #region constants |
||
21 | /** |
||
22 | * Code for an empty doc block |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | const CODE_EMPTY = 'Empty'; |
||
27 | |||
28 | /** |
||
29 | * Message for an empty doc block |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | const MESSAGE_DOC_EMPTY = 'Doc comment is empty'; |
||
34 | |||
35 | /** |
||
36 | * Code for a Spacing Error after the Summary |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const CODE_SPACING_AFTER = 'SpacingAfter'; |
||
41 | |||
42 | /** |
||
43 | * Message for a Spacing Error after the Summary |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | const MESSAGE_SPACING_AFTER = 'Additional blank lines found at end of doc comment'; |
||
48 | |||
49 | /** |
||
50 | * Code for missing spacing between descriptions |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | const CODE_SPACING_BETWEEN = 'SpacingBetween'; |
||
55 | |||
56 | /** |
||
57 | * Message for missing spacing between descriptions |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | const MESSAGE_SPACING_BETWEEN = 'There must be exactly one blank line between descriptions in a doc comment'; |
||
62 | |||
63 | /** |
||
64 | * Code for missing spacing before the tags |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | const CODE_SPACING_BEFORE_TAGS = 'SpacingBeforeTags'; |
||
69 | |||
70 | /** |
||
71 | * Message for missing spacing before the tags |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | const MESSAGE_SPACING_BEFORE_TAGS = 'There must be exactly one blank line before the tags in a doc comment'; |
||
76 | |||
77 | /** |
||
78 | * Code for missing short description (Summary) |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | const CODE_MISSING_SHORT = 'MissingShort'; |
||
83 | |||
84 | /** |
||
85 | * Message for missing short description (Summary) |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | const MESSAGE_MISSING_SHORT = 'Missing short description in doc comment'; |
||
90 | |||
91 | /** |
||
92 | * Code for not capitalized short description |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | const CODE_SHORT_NOT_CAPITAL = 'ShortNotCapital'; |
||
97 | |||
98 | /** |
||
99 | * Message for not capitalized short description |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | const MESSAGE_SHORT_NOT_CAPITAL = 'Doc comment short description must start with a capital letter'; |
||
104 | |||
105 | /** |
||
106 | * Code for not Capitalized long description |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | const CODE_LONG_NOT_CAPITAL = 'LongNotCapital'; |
||
111 | |||
112 | /** |
||
113 | * Message for not Capitalized long description |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | const MESSAGE_LONG_NOT_CAPITAL = 'Doc comment long description must start with a capital letter'; |
||
118 | |||
119 | /** |
||
120 | * Code for empty lines before the short description |
||
121 | * |
||
122 | * @var string |
||
123 | */ |
||
124 | const CODE_SPACING_BEFORE_SHORT = 'SpacingBeforeShort'; |
||
125 | |||
126 | /** |
||
127 | * Message for empty lines before the short description |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | const MESSAGE_CODE_SPACING_BEFORE = 'Doc comment short description must be on the first line'; |
||
132 | #endregion |
||
133 | |||
134 | /** |
||
135 | * A list of tokenizers this sniff supports. |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | public $supportedTokenizers = ['PHP', 'JS']; |
||
140 | |||
141 | /** |
||
142 | * The token array is the base of this evaluation |
||
143 | * |
||
144 | * @var array |
||
145 | */ |
||
146 | public $tokens; |
||
147 | |||
148 | /** |
||
149 | * Definitions for an empty doc block |
||
150 | * |
||
151 | * @var array |
||
152 | */ |
||
153 | public $empty = [T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR]; |
||
154 | |||
155 | /** |
||
156 | * The phpcs file |
||
157 | * |
||
158 | * @var PHP_CodeSniffer_File |
||
159 | */ |
||
160 | public $phpcsFile; |
||
161 | |||
162 | /** |
||
163 | * Pointer to the start of the comment |
||
164 | * |
||
165 | * @var int |
||
166 | */ |
||
167 | public $commentStart; |
||
168 | |||
169 | /** |
||
170 | * Pointer to the start of the short description |
||
171 | * |
||
172 | * @var int |
||
173 | */ |
||
174 | public $short; |
||
175 | |||
176 | /** |
||
177 | * Returns an array of tokens this test wants to listen for. |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | 8 | public function register() |
|
185 | |||
186 | /** |
||
187 | * Checks if there is a blank line at the end of the doc |
||
188 | * |
||
189 | * @param $commentEnd |
||
190 | * @param $prev |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 7 | private function checkBlankLines($commentEnd, $prev) |
|
201 | |||
202 | /** |
||
203 | * Checks if short or long description starts with a capital letter |
||
204 | * |
||
205 | * @param $shortContent |
||
206 | * @param $long |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | 6 | private function checkCapitalLetter($shortContent, $long) |
|
223 | |||
224 | /** |
||
225 | * Checks capital letter and spacing of long description |
||
226 | * |
||
227 | * @param $shortEnd |
||
228 | * @param $long |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | 6 | private function checkLongDescription( |
|
246 | |||
247 | /** |
||
248 | * Checks for a blank line before the doc tags |
||
249 | * |
||
250 | * @param $stackPtr |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | 6 | private function checkCommentTags($stackPtr) |
|
270 | |||
271 | /** |
||
272 | * Checks long and short description |
||
273 | * |
||
274 | * @param $stackPtr |
||
275 | * @param $commentEnd |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | 6 | private function checkSummaryAndLongDescription($stackPtr, $commentEnd) |
|
309 | |||
310 | /** |
||
311 | * Checks the method doc block |
||
312 | * |
||
313 | * @param $stackPtr |
||
314 | * @param $commentEnd |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | 7 | private function checkMethodPhpDoc($stackPtr, $commentEnd) |
|
336 | |||
337 | /** |
||
338 | * Processes this test, when one of its tokens is encountered. |
||
339 | * |
||
340 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
341 | * @param int $stackPtr The position of the current token in the stack passed in $tokens. |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | 8 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
365 | } |
||
366 |
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.