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 |
||
| 25 | class ForbiddenNamesAsInvokedFunctionsSniff extends Sniff |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of tokens to register. |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $targetedTokens = array( |
||
| 34 | T_ABSTRACT => '5.0', |
||
| 35 | T_CALLABLE => '5.4', |
||
| 36 | T_CATCH => '5.0', |
||
| 37 | T_FINAL => '5.0', |
||
| 38 | T_GOTO => '5.3', |
||
| 39 | T_IMPLEMENTS => '5.0', |
||
| 40 | T_INTERFACE => '5.0', |
||
| 41 | T_INSTANCEOF => '5.0', |
||
| 42 | T_NAMESPACE => '5.3', |
||
| 43 | T_PRIVATE => '5.0', |
||
| 44 | T_PROTECTED => '5.0', |
||
| 45 | T_PUBLIC => '5.0', |
||
| 46 | T_TRY => '5.0', |
||
| 47 | ); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * T_STRING keywords to recognize as targetted tokens. |
||
| 51 | * |
||
| 52 | * Compatibility for PHP versions where the keyword is not yet recognized |
||
| 53 | * as its own token and for PHPCS versions which change the token to |
||
| 54 | * T_STRING when used in a method call. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $targetedStringTokens = array( |
||
| 59 | 'abstract' => '5.0', |
||
| 60 | 'callable' => '5.4', |
||
| 61 | 'catch' => '5.0', |
||
| 62 | 'final' => '5.0', |
||
| 63 | 'finally' => '5.5', |
||
| 64 | 'goto' => '5.3', |
||
| 65 | 'implements' => '5.0', |
||
| 66 | 'interface' => '5.0', |
||
| 67 | 'instanceof' => '5.0', |
||
| 68 | 'insteadof' => '5.4', |
||
| 69 | 'namespace' => '5.3', |
||
| 70 | 'private' => '5.0', |
||
| 71 | 'protected' => '5.0', |
||
| 72 | 'public' => '5.0', |
||
| 73 | 'trait' => '5.4', |
||
| 74 | 'try' => '5.0', |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns an array of tokens this test wants to listen for. |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function register() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Processes this test, when one of its tokens is encountered. |
||
| 105 | * |
||
| 106 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 107 | * @param int $stackPtr The position of the current token in the |
||
| 108 | * stack passed in $tokens. |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 188 | |||
| 189 | }//end class |
||
| 190 |