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_RemovedFunctionParametersSniff 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 Removed function parameters, 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 $removedFunctionParameters = array( |
||
37 | 'mktime' => array( |
||
38 | 6 => array( |
||
39 | 'name' => 'is_dst', |
||
40 | '5.1' => true, |
||
41 | '7.0' => false |
||
42 | ), |
||
43 | ), |
||
44 | 'gmmktime' => array( |
||
45 | 6 => array( |
||
46 | 'name' => 'is_dst', |
||
47 | '7.0' => false |
||
48 | ), |
||
49 | ), |
||
50 | ); |
||
51 | |||
52 | |||
53 | /** |
||
54 | * If true, an error will be thrown; otherwise a warning. |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | public $error = false; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | private $removedFunctionParametersNames; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Returns an array of tokens this test wants to listen for. |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | public function register() |
||
86 | |||
87 | /** |
||
88 | * Processes this test, when one of its tokens is encountered. |
||
89 | * |
||
90 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
91 | * @param int $stackPtr The position of the current token in |
||
92 | * the stack passed in $tokens. |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | View Code Duplication | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
133 | |||
134 | |||
135 | /** |
||
136 | * Generates the error or wanrning for this sniff. |
||
137 | * |
||
138 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
139 | * @param int $stackPtr The position of the function |
||
140 | * in the token array. |
||
141 | * @param string $function The name of the function. |
||
142 | * @param string $pattern The pattern used for the match. |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | View Code Duplication | protected function addError($phpcsFile, $stackPtr, $function, $parameterLocation) |
|
171 | |||
172 | }//end class |
||
173 |
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.