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:
Complex classes like ForbiddenNamesSniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ForbiddenNamesSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ForbiddenNamesSniff extends Sniff |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * A list of keywords that can not be used as function, class and namespace name or constant name. |
||
25 | * Mentions since which version it's not allowed. |
||
26 | * |
||
27 | * @var array(string => string) |
||
28 | */ |
||
29 | protected $invalidNames = array( |
||
30 | 'abstract' => '5.0', |
||
31 | 'and' => 'all', |
||
32 | 'array' => 'all', |
||
33 | 'as' => 'all', |
||
34 | 'break' => 'all', |
||
35 | 'callable' => '5.4', |
||
36 | 'case' => 'all', |
||
37 | 'catch' => '5.0', |
||
38 | 'class' => 'all', |
||
39 | 'clone' => '5.0', |
||
40 | 'const' => 'all', |
||
41 | 'continue' => 'all', |
||
42 | 'declare' => 'all', |
||
43 | 'default' => 'all', |
||
44 | 'die' => 'all', |
||
45 | 'do' => 'all', |
||
46 | 'echo' => 'all', |
||
47 | 'else' => 'all', |
||
48 | 'elseif' => 'all', |
||
49 | 'empty' => 'all', |
||
50 | 'enddeclare' => 'all', |
||
51 | 'endfor' => 'all', |
||
52 | 'endforeach' => 'all', |
||
53 | 'endif' => 'all', |
||
54 | 'endswitch' => 'all', |
||
55 | 'endwhile' => 'all', |
||
56 | 'eval' => 'all', |
||
57 | 'exit' => 'all', |
||
58 | 'extends' => 'all', |
||
59 | 'final' => '5.0', |
||
60 | 'finally' => '5.5', |
||
61 | 'for' => 'all', |
||
62 | 'foreach' => 'all', |
||
63 | 'function' => 'all', |
||
64 | 'global' => 'all', |
||
65 | 'goto' => '5.3', |
||
66 | 'if' => 'all', |
||
67 | 'implements' => '5.0', |
||
68 | 'include' => 'all', |
||
69 | 'include_once' => 'all', |
||
70 | 'instanceof' => '5.0', |
||
71 | 'insteadof' => '5.4', |
||
72 | 'interface' => '5.0', |
||
73 | 'isset' => 'all', |
||
74 | 'list' => 'all', |
||
75 | 'namespace' => '5.3', |
||
76 | 'new' => 'all', |
||
77 | 'or' => 'all', |
||
78 | 'print' => 'all', |
||
79 | 'private' => '5.0', |
||
80 | 'protected' => '5.0', |
||
81 | 'public' => '5.0', |
||
82 | 'require' => 'all', |
||
83 | 'require_once' => 'all', |
||
84 | 'return' => 'all', |
||
85 | 'static' => 'all', |
||
86 | 'switch' => 'all', |
||
87 | 'throw' => '5.0', |
||
88 | 'trait' => '5.4', |
||
89 | 'try' => '5.0', |
||
90 | 'unset' => 'all', |
||
91 | 'use' => 'all', |
||
92 | 'var' => 'all', |
||
93 | 'while' => 'all', |
||
94 | 'xor' => 'all', |
||
95 | 'yield' => '5.5', |
||
96 | '__class__' => 'all', |
||
97 | '__dir__' => '5.3', |
||
98 | '__file__' => 'all', |
||
99 | '__function__' => 'all', |
||
100 | '__method__' => 'all', |
||
101 | '__namespace__' => '5.3', |
||
102 | ); |
||
103 | |||
104 | /** |
||
105 | * A list of keywords that can follow use statements. |
||
106 | * |
||
107 | * @var array(string => string) |
||
108 | */ |
||
109 | protected $validUseNames = array( |
||
110 | 'const' => true, |
||
111 | 'function' => true, |
||
112 | ); |
||
113 | |||
114 | /** |
||
115 | * Scope modifiers and other keywords allowed in trait use statements. |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | private $allowedModifiers = array(); |
||
120 | |||
121 | /** |
||
122 | * Targeted tokens. |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | protected $targetedTokens = array( |
||
127 | \T_CLASS, |
||
128 | \T_FUNCTION, |
||
129 | \T_NAMESPACE, |
||
130 | \T_STRING, |
||
131 | \T_CONST, |
||
132 | \T_USE, |
||
133 | \T_AS, |
||
134 | \T_EXTENDS, |
||
135 | \T_INTERFACE, |
||
136 | \T_TRAIT, |
||
137 | ); |
||
138 | |||
139 | /** |
||
140 | * Returns an array of tokens this test wants to listen for. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function register() |
||
157 | |||
158 | /** |
||
159 | * Processes this test, when one of its tokens is encountered. |
||
160 | * |
||
161 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
162 | * @param int $stackPtr The position of the current token in the |
||
163 | * stack passed in $tokens. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function process(File $phpcsFile, $stackPtr) |
||
180 | |||
181 | /** |
||
182 | * Processes this test, when one of its tokens is encountered. |
||
183 | * |
||
184 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
185 | * @param int $stackPtr The position of the current token in the |
||
186 | * stack passed in $tokens. |
||
187 | * @param array $tokens The stack of tokens that make up |
||
188 | * the file. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | public function processNonString(File $phpcsFile, $stackPtr, $tokens) |
||
335 | |||
336 | /** |
||
337 | * Processes this test, when one of its tokens is encountered. |
||
338 | * |
||
339 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
340 | * @param int $stackPtr The position of the current token in the |
||
341 | * stack passed in $tokens. |
||
342 | * @param array $tokens The stack of tokens that make up |
||
343 | * the file. |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | public function processString(File $phpcsFile, $stackPtr, $tokens) |
||
383 | |||
384 | |||
385 | /** |
||
386 | * Add the error message. |
||
387 | * |
||
388 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
389 | * @param int $stackPtr The position of the current token in the |
||
390 | * stack passed in $tokens. |
||
391 | * @param string $content The token content found. |
||
392 | * @param array $data The data to pass into the error message. |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | protected function addError(File $phpcsFile, $stackPtr, $content, $data) |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Check if the current token code is for a token which can be considered |
||
406 | * the end of a (partial) use statement. |
||
407 | * |
||
408 | * @param int $token The current token information. |
||
409 | * |
||
410 | * @return bool |
||
411 | */ |
||
412 | protected function isEndOfUseStatement($token) |
||
416 | } |
||
417 |
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.