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 |
||
| 23 | abstract class AbstractFunctionCallParameterSniff extends Sniff |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Is the sniff looking for a function call or a method call ? |
||
| 27 | * |
||
| 28 | * Note: the child class may need to do additional checks to make sure that |
||
| 29 | * the method called is of the right class/object. |
||
| 30 | * Checking that is outside of the scope of this abstract sniff. |
||
| 31 | * |
||
| 32 | * @var bool False (default) if the sniff is looking for function calls. |
||
| 33 | * True if the sniff is looking for method calls. |
||
| 34 | */ |
||
| 35 | protected $isMethod = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Functions the sniff is looking for. Should be defined in the child class. |
||
| 39 | * |
||
| 40 | * @var array The only requirement for this array is that the top level |
||
| 41 | * array keys are the names of the functions you're looking for. |
||
| 42 | * Other than that, the array can have arbitrary content |
||
| 43 | * depending on your needs. |
||
| 44 | */ |
||
| 45 | protected $targetFunctions = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * List of tokens which when they preceed the $stackPtr indicate that this |
||
| 49 | * is not a function call. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $ignoreTokens = array( |
||
| 54 | T_DOUBLE_COLON => true, |
||
| 55 | T_OBJECT_OPERATOR => true, |
||
| 56 | T_FUNCTION => true, |
||
| 57 | T_NEW => true, |
||
| 58 | T_CONST => true, |
||
| 59 | T_USE => true, |
||
| 60 | ); |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns an array of tokens this test wants to listen for. |
||
| 65 | * |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | public function register() |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Processes this test, when one of its tokens is encountered. |
||
| 79 | * |
||
| 80 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 81 | * @param int $stackPtr The position of the current token in |
||
| 82 | * the stack passed in $tokens. |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Do a version check to determine if this sniff needs to run at all. |
||
| 135 | * |
||
| 136 | * If the check done in a child class is not specific to one PHP version, |
||
| 137 | * this function should return `false`. |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | abstract protected function bowOutEarly(); |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Process the parameters of a matched function. |
||
| 146 | * |
||
| 147 | * This method has to be made concrete in child classes. |
||
| 148 | * |
||
| 149 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 150 | * @param int $stackPtr The position of the current token in the stack. |
||
| 151 | * @param string $functionName The token content (function name) which was matched. |
||
| 152 | * @param array $parameters Array with information about the parameters. |
||
| 153 | * |
||
| 154 | * @return int|void Integer stack pointer to skip forward or void to continue |
||
| 155 | * normal file processing. |
||
| 156 | */ |
||
| 157 | abstract public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters); |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Process the function if no parameters were found. |
||
| 162 | * |
||
| 163 | * Defaults to doing nothing. Can be overloaded in child classes to handle functions |
||
| 164 | * were parameters are expected, but none found. |
||
| 165 | * |
||
| 166 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
| 167 | * @param int $stackPtr The position of the current token in the stack. |
||
| 168 | * @param string $functionName The token content (function name) which was matched. |
||
| 169 | * |
||
| 170 | * @return int|void Integer stack pointer to skip forward or void to continue |
||
| 171 | * normal file processing. |
||
| 172 | */ |
||
| 173 | public function processNoParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName) |
||
| 177 | }//end class |
||
| 178 |