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_RemovedFunctionParametersSniff extends PHPCompatibility_AbstractRemovedFeatureSniff |
||
|
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * A list of removed function parameters, which were present in older versions. |
||
| 21 | * |
||
| 22 | * The array lists : version number with false (deprecated) and true (removed). |
||
| 23 | * The index is the location of the parameter in the parameter list, starting at 0 ! |
||
| 24 | * If's sufficient to list the first version where the function parameter was deprecated/removed. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $removedFunctionParameters = array( |
||
| 29 | 'gmmktime' => array( |
||
| 30 | 6 => array( |
||
| 31 | 'name' => 'is_dst', |
||
| 32 | '5.1' => false, // deprecated |
||
| 33 | '7.0' => true, |
||
| 34 | ), |
||
| 35 | ), |
||
| 36 | 'ldap_first_attribute' => array( |
||
| 37 | 2 => array( |
||
| 38 | 'name' => 'ber_identifier', |
||
| 39 | '5.2.4' => true, |
||
| 40 | ), |
||
| 41 | ), |
||
| 42 | 'ldap_next_attribute' => array( |
||
| 43 | 2 => array( |
||
| 44 | 'name' => 'ber_identifier', |
||
| 45 | '5.2.4' => true, |
||
| 46 | ), |
||
| 47 | ), |
||
| 48 | 'mktime' => array( |
||
| 49 | 6 => array( |
||
| 50 | 'name' => 'is_dst', |
||
| 51 | '5.1' => false, // deprecated |
||
| 52 | '7.0' => true, |
||
| 53 | ), |
||
| 54 | ), |
||
| 55 | ); |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns an array of tokens this test wants to listen for. |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function register() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Processes this test, when one of its tokens is encountered. |
||
| 73 | * |
||
| 74 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 75 | * @param int $stackPtr The position of the current token in |
||
| 76 | * the stack passed in $tokens. |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the relevant sub-array for a specific item from a multi-dimensional array. |
||
| 129 | * |
||
| 130 | * @param array $itemInfo Base information about the item. |
||
| 131 | * |
||
| 132 | * @return array Version and other information about the item. |
||
| 133 | */ |
||
| 134 | public function getItemArray(array $itemInfo) |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Get an array of the non-PHP-version array keys used in a sub-array. |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | protected function getNonVersionArrayKeys() |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Retrieve the relevant detail (version) information for use in an error message. |
||
| 153 | * |
||
| 154 | * @param array $itemArray Version and other information about the item. |
||
| 155 | * @param array $itemInfo Base information about the item. |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the item name to be used for the creation of the error code. |
||
| 170 | * |
||
| 171 | * @param array $itemInfo Base information about the item. |
||
| 172 | * @param array $errorInfo Detail information about an item. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | protected function getItemName(array $itemInfo, array $errorInfo) |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the error message template for this sniff. |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | protected function getErrorMsgTemplate() |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
||
| 195 | * |
||
| 196 | * @param array $data The error data array which was created. |
||
| 197 | * @param array $itemInfo Base information about the item this error message applied to. |
||
| 198 | * @param array $errorInfo Detail information about an item this error message applied to. |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
||
| 208 | |||
| 209 | |||
| 210 | }//end class |
||
| 211 |
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.