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 | 8 | 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 | 4 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 54 | { |
||
| 55 | 4 | $tokens = $phpcsFile->getTokens(); |
|
| 56 | 4 | $token = $tokens[$stackPtr]; |
|
| 57 | |||
| 58 | 4 | if ($this->couldBeBinaryInteger($tokens, $stackPtr) === true) { |
|
| 59 | 4 | if ($this->supportsBelow('5.3')) { |
|
| 60 | 2 | $error = 'Binary integer literals were not present in PHP version 5.3 or earlier. Found: %s'; |
|
| 61 | 2 | if ($this->isLowPHPVersion === false) { |
|
| 62 | $data = array($token['content']); |
||
| 63 | } else { |
||
| 64 | 2 | $data = array($this->getBinaryInteger($phpcsFile, $tokens, $stackPtr)); |
|
| 65 | } |
||
| 66 | 2 | $phpcsFile->addError($error, $stackPtr, 'BinaryIntegerFound', $data); |
|
| 67 | 2 | } |
|
| 68 | |||
| 69 | 4 | if ($this->isInvalidBinaryInteger($tokens, $stackPtr) === true) { |
|
| 70 | 4 | $error = 'Invalid binary integer detected. Found: %s'; |
|
| 71 | 4 | $data = array($this->getBinaryInteger($phpcsFile, $tokens, $stackPtr)); |
|
| 72 | 4 | $phpcsFile->addWarning($error, $stackPtr, 'InvalidBinaryIntegerFound', $data); |
|
| 73 | 4 | } |
|
| 74 | 4 | return; |
|
| 75 | } |
||
| 76 | |||
| 77 | 4 | $isError = $this->supportsAbove('7.0'); |
|
| 78 | 4 | $data = array( $token['content'] ); |
|
| 79 | |||
| 80 | 4 | if ($this->isInvalidOctalInteger($tokens, $stackPtr) === true) { |
|
| 81 | 4 | $this->addMessage( |
|
| 82 | 4 | $phpcsFile, |
|
| 83 | 4 | 'Invalid octal integer detected. Prior to PHP 7 this would lead to a truncated number. From PHP 7 onwards this causes a parse error. Found: %s', |
|
| 84 | 4 | $stackPtr, |
|
| 85 | 4 | $isError, |
|
| 86 | 4 | 'InvalidOctalIntegerFound', |
|
| 87 | $data |
||
| 88 | 4 | ); |
|
| 89 | 4 | return; |
|
| 90 | } |
||
| 91 | |||
| 92 | 4 | if ($this->isHexidecimalNumericString($tokens, $stackPtr) === true) { |
|
| 93 | 4 | $this->addMessage( |
|
| 94 | 4 | $phpcsFile, |
|
| 95 | 4 | 'The behaviour of hexadecimal numeric strings was inconsistent prior to PHP 7 and support has been removed in PHP 7. Found: %s', |
|
| 96 | 4 | $stackPtr, |
|
| 97 | 4 | $isError, |
|
| 98 | 4 | 'HexNumericStringFound', |
|
| 99 | $data |
||
| 100 | 4 | ); |
|
| 101 | 4 | return; |
|
| 102 | } |
||
| 103 | |||
| 104 | 4 | }//end process() |
|
| 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 | 4 | 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 | 4 | 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 | 4 | private function getBinaryInteger(PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr) |
|
| 166 | { |
||
| 167 | 4 | $length = 2; // PHP < 5.4 T_LNUMBER + T_STRING. |
|
| 168 | |||
| 169 | 4 | if ($this->isLowPHPVersion === false) { |
|
| 170 | $i = $stackPtr; |
||
| 171 | while ($tokens[$i]['code'] === T_LNUMBER) { |
||
| 172 | $i++; |
||
| 173 | } |
||
| 174 | $length = ($i - $stackPtr); |
||
| 175 | } |
||
| 176 | |||
| 177 | 4 | return $phpcsFile->getTokensAsString($stackPtr, $length); |
|
| 178 | } |
||
| 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 | 4 | 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 | 4 | View Code Duplication | private function isHexidecimalNumericString($tokens, $stackPtr) |
| 217 | |||
| 218 | }//end class |
||
| 219 |
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.