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 PHPCompatibility_Sniffs_PHP_NewInterfacesSniff extends PHPCompatibility_AbstractNewFeatureSniff |
||
|
|||
18 | { |
||
19 | |||
20 | /** |
||
21 | * A list of new interfaces, not present in older versions. |
||
22 | * |
||
23 | * The array lists : version number with false (not present) or true (present). |
||
24 | * If's sufficient to list the first version where the interface appears. |
||
25 | * |
||
26 | * @var array(string => array(string => int|string|null)) |
||
27 | */ |
||
28 | protected $newInterfaces = array( |
||
29 | 'Traversable' => array( |
||
30 | '4.4' => false, |
||
31 | '5.0' => true, |
||
32 | ), |
||
33 | |||
34 | 'Countable' => array( |
||
35 | '5.0' => false, |
||
36 | '5.1' => true, |
||
37 | ), |
||
38 | 'OuterIterator' => array( |
||
39 | '5.0' => false, |
||
40 | '5.1' => true, |
||
41 | ), |
||
42 | 'RecursiveIterator' => array( |
||
43 | '5.0' => false, |
||
44 | '5.1' => true, |
||
45 | ), |
||
46 | 'SeekableIterator' => array( |
||
47 | '5.0' => false, |
||
48 | '5.1' => true, |
||
49 | ), |
||
50 | 'Serializable' => array( |
||
51 | '5.0' => false, |
||
52 | '5.1' => true, |
||
53 | ), |
||
54 | 'SplObserver' => array( |
||
55 | '5.0' => false, |
||
56 | '5.1' => true, |
||
57 | ), |
||
58 | 'SplSubject' => array( |
||
59 | '5.0' => false, |
||
60 | '5.1' => true, |
||
61 | ), |
||
62 | |||
63 | 'JsonSerializable' => array( |
||
64 | '5.3' => false, |
||
65 | '5.4' => true, |
||
66 | ), |
||
67 | 'SessionHandlerInterface' => array( |
||
68 | '5.3' => false, |
||
69 | '5.4' => true, |
||
70 | ), |
||
71 | |||
72 | 'DateTimeInterface' => array( |
||
73 | '5.4' => false, |
||
74 | '5.5' => true, |
||
75 | ), |
||
76 | |||
77 | 'Throwable' => array( |
||
78 | '5.6' => false, |
||
79 | '7.0' => true, |
||
80 | ), |
||
81 | |||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * A list of methods which cannot be used in combination with particular interfaces. |
||
86 | * |
||
87 | * @var array(string => array(string => string)) |
||
88 | */ |
||
89 | protected $unsupportedMethods = array( |
||
90 | 'Serializable' => array( |
||
91 | '__sleep' => 'http://php.net/serializable', |
||
92 | '__wakeup' => 'http://php.net/serializable', |
||
93 | ), |
||
94 | ); |
||
95 | |||
96 | /** |
||
97 | * Returns an array of tokens this test wants to listen for. |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function register() |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Processes this test, when one of its tokens is encountered. |
||
124 | * |
||
125 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
126 | * @param int $stackPtr The position of the current token in |
||
127 | * the stack passed in $tokens. |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
152 | |||
153 | |||
154 | /** |
||
155 | * Processes this test for when a class token is encountered. |
||
156 | * |
||
157 | * - Detect classes implementing the new interfaces. |
||
158 | * - Detect classes implementing the new interfaces with unsupported functions. |
||
159 | * |
||
160 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
161 | * @param int $stackPtr The position of the current token in |
||
162 | * the stack passed in $tokens. |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | private function processClassToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Processes this test for when a function token is encountered. |
||
221 | * |
||
222 | * - Detect new interfaces when used as a type hint. |
||
223 | * |
||
224 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
225 | * @param int $stackPtr The position of the current token in |
||
226 | * the stack passed in $tokens. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | View Code Duplication | private function processFunctionToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
250 | |||
251 | |||
252 | /** |
||
253 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
254 | * |
||
255 | * @param array $itemInfo Base information about the item. |
||
256 | * |
||
257 | * @return array Version and other information about the item. |
||
258 | */ |
||
259 | public function getItemArray(array $itemInfo) |
||
263 | |||
264 | |||
265 | /** |
||
266 | * Get the error message template for this sniff. |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function getErrorMsgTemplate() |
||
274 | |||
275 | |||
276 | }//end class |
||
277 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.