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_NewFunctionParametersSniff extends PHPCompatibility_Sniff |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * If true, forbidden functions will be considered regular expressions. |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $patternMatch = false; |
||
26 | |||
27 | /** |
||
28 | * A list of new functions, not present in older versions. |
||
29 | * |
||
30 | * The array lists : version number with false (not present) or true (present). |
||
31 | * The index is the location of the parameter in the parameter list, starting at 0 ! |
||
32 | * If's sufficient to list the first version where the function appears. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | public $newFunctionParameters = array( |
||
37 | 'dirname' => array( |
||
38 | 1 => array( |
||
39 | 'name' => 'depth', |
||
40 | '5.6' => false, |
||
41 | '7.0' => true |
||
42 | ), |
||
43 | ), |
||
44 | 'unserialize' => array( |
||
45 | 1 => array( |
||
46 | 'name' => 'options', |
||
47 | '5.6' => false, |
||
48 | '7.0' => true |
||
49 | ), |
||
50 | ), |
||
51 | 'session_start' => array( |
||
52 | 0 => array( |
||
53 | 'name' => 'options', |
||
54 | '5.6' => false, |
||
55 | '7.0' => true |
||
56 | ) |
||
57 | ) |
||
58 | ); |
||
59 | |||
60 | |||
61 | /** |
||
62 | * If true, an error will be thrown; otherwise a warning. |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | public $error = false; |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $newFunctionParametersNames; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Returns an array of tokens this test wants to listen for. |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public function register() |
||
94 | |||
95 | /** |
||
96 | * Processes this test, when one of its tokens is encountered. |
||
97 | * |
||
98 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
99 | * @param int $stackPtr The position of the current token in |
||
100 | * the stack passed in $tokens. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Generates the error or wanrning for this sniff. |
||
148 | * |
||
149 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
150 | * @param int $stackPtr The position of the function |
||
151 | * in the token array. |
||
152 | * @param string $function The name of the function. |
||
153 | * @param string $pattern The pattern used for the match. |
||
|
|||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | View Code Duplication | protected function addError($phpcsFile, $stackPtr, $function, $parameterLocation) |
|
182 | |||
183 | }//end class |
||
184 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.