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