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_RequiredOptionalFunctionParametersSniff extends PHPCompatibility_AbstractComplexVersionSniff |
||
|
|
|||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * A list of function parameters, which were required in older versions and became optional later on. |
||
| 22 | * |
||
| 23 | * The array lists : version number with true (required) and false (optional). |
||
| 24 | * |
||
| 25 | * The index is the location of the parameter in the parameter list, starting at 0 ! |
||
| 26 | * If's sufficient to list the last version in which the parameter was still required. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $functionParameters = array( |
||
| 31 | 'preg_match_all' => array( |
||
| 32 | 2 => array( |
||
| 33 | 'name' => 'matches', |
||
| 34 | '5.3' => true, |
||
| 35 | '5.4' => false, |
||
| 36 | ), |
||
| 37 | ), |
||
| 38 | 'stream_socket_enable_crypto' => array( |
||
| 39 | 2 => array( |
||
| 40 | 'name' => 'crypto_type', |
||
| 41 | '5.5' => true, |
||
| 42 | '5.6' => false, |
||
| 43 | ), |
||
| 44 | ), |
||
| 45 | ); |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns an array of tokens this test wants to listen for. |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public function register() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Processes this test, when one of its tokens is encountered. |
||
| 63 | * |
||
| 64 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 65 | * @param int $stackPtr The position of the current token in |
||
| 66 | * the stack passed in $tokens. |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * Determine whether an error/warning should be thrown for an item based on collected information. |
||
| 119 | * |
||
| 120 | * @param array $errorInfo Detail information about an item. |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | protected function shouldThrowError(array $errorInfo) |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 132 | * |
||
| 133 | * @param array $itemInfo Base information about the item. |
||
| 134 | * |
||
| 135 | * @return array Version and other information about the item. |
||
| 136 | */ |
||
| 137 | public function getItemArray(array $itemInfo) |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | protected function getNonVersionArrayKeys() |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Retrieve the relevant detail (version) information for use in an error message. |
||
| 156 | * |
||
| 157 | * @param array $itemArray Version and other information about the item. |
||
| 158 | * @param array $itemInfo Base information about the item. |
||
| 159 | * |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the error message template for this sniff. |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | protected function getErrorMsgTemplate() |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Generates the error or warning for this item. |
||
| 197 | * |
||
| 198 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 199 | * @param int $stackPtr The position of the relevant token in |
||
| 200 | * the stack. |
||
| 201 | * @param array $itemInfo Base information about the item. |
||
| 202 | * @param array $errorInfo Array with detail (version) information |
||
| 203 | * relevant to the item. |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
||
| 220 | |||
| 221 | |||
| 222 | }//end class |
||
| 223 |
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.