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 |
||
19 | class PHPCompatibility_Sniffs_PHP_NewInterfacesSniff extends PHPCompatibility_Sniff |
||
|
|||
20 | { |
||
21 | |||
22 | /** |
||
23 | * A list of new interfaces, not present in older versions. |
||
24 | * |
||
25 | * The array lists : version number with false (not present) or true (present). |
||
26 | * If's sufficient to list the first version where the interface appears. |
||
27 | * |
||
28 | * @var array(string => array(string => int|string|null)) |
||
29 | */ |
||
30 | protected $newInterfaces = array( |
||
31 | 'Countable' => array( |
||
32 | '5.0' => false, |
||
33 | '5.1' => true |
||
34 | ), |
||
35 | 'OuterIterator' => array( |
||
36 | '5.0' => false, |
||
37 | '5.1' => true |
||
38 | ), |
||
39 | 'RecursiveIterator' => array( |
||
40 | '5.0' => false, |
||
41 | '5.1' => true |
||
42 | ), |
||
43 | 'SeekableIterator' => array( |
||
44 | '5.0' => false, |
||
45 | '5.1' => true |
||
46 | ), |
||
47 | 'Serializable' => array( |
||
48 | '5.0' => false, |
||
49 | '5.1' => true, |
||
50 | ), |
||
51 | 'SplObserver' => array( |
||
52 | '5.0' => false, |
||
53 | '5.1' => true |
||
54 | ), |
||
55 | 'SplSubject' => array( |
||
56 | '5.0' => false, |
||
57 | '5.1' => true |
||
58 | ), |
||
59 | |||
60 | 'JsonSerializable' => array( |
||
61 | '5.3' => false, |
||
62 | '5.4' => true |
||
63 | ), |
||
64 | 'SessionHandlerInterface' => array( |
||
65 | '5.3' => false, |
||
66 | '5.4' => true |
||
67 | ), |
||
68 | |||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * A list of methods which cannot be used in combination with particular interfaces. |
||
73 | * |
||
74 | * @var array(string => array(string => string)) |
||
75 | */ |
||
76 | protected $unsupportedMethods = array( |
||
77 | 'Serializable' => array( |
||
78 | '__sleep' => 'http://php.net/serializable', |
||
79 | '__wakeup' => 'http://php.net/serializable', |
||
80 | ), |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * Returns an array of tokens this test wants to listen for. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function register() |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Processes this test, when one of its tokens is encountered. |
||
106 | * |
||
107 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
108 | * @param int $stackPtr The position of the current token in |
||
109 | * the stack passed in $tokens. |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Generates the error or warning for this sniff. |
||
161 | * |
||
162 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
163 | * @param int $stackPtr The position of the function |
||
164 | * in the token array. |
||
165 | * @param string $interface The name of the interface. |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | View Code Duplication | protected function addError($phpcsFile, $stackPtr, $interface) |
|
195 | |||
196 | }//end class |
||
197 |
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.