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_ValidIntegersSniff extends PHPCompatibility_Sniff |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Whether PHPCS is run on a PHP < 5.4. |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $isLowPHPVersion = false; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns an array of tokens this test wants to listen for. |
||
| 29 | * |
||
| 30 | * @return array |
||
| 31 | */ |
||
| 32 | public function register() |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Processes this test, when one of its tokens is encountered. |
||
| 46 | * |
||
| 47 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 48 | * @param int $stackPtr The position of the current token in |
||
| 49 | * the stack. |
||
| 50 | * |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Could the current token an potentially be a binary integer ? |
||
| 109 | * |
||
| 110 | * @param array $tokens Token stack. |
||
| 111 | * @param int $stackPtr The current position in the token stack. |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | private function couldBeBinaryInteger($tokens, $stackPtr) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Is the current token an invalid binary integer ? |
||
| 135 | * |
||
| 136 | * @param array $tokens Token stack. |
||
| 137 | * @param int $stackPtr The current position in the token stack. |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | private function isInvalidBinaryInteger($tokens, $stackPtr) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Retrieve the content of the tokens which together look like a binary integer. |
||
| 157 | * |
||
| 158 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 159 | * @param array $tokens Token stack. |
||
| 160 | * @param int $stackPtr The position of the current token in |
||
| 161 | * the stack. |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | private function getBinaryInteger(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Is the current token an invalid octal integer ? |
||
| 182 | * |
||
| 183 | * @param array $tokens Token stack. |
||
| 184 | * @param int $stackPtr The current position in the token stack. |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | View Code Duplication | private function isInvalidOctalInteger($tokens, $stackPtr) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Is the current token a hexidecimal numeric string ? |
||
| 201 | * |
||
| 202 | * @param array $tokens Token stack. |
||
| 203 | * @param int $stackPtr The current position in the token stack. |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | View Code Duplication | private function isHexidecimalNumericString($tokens, $stackPtr) |
|
| 217 | |||
| 218 | }//end class |
||
| 219 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.