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 |
||
21 | class OptionalRequiredFunctionParametersSniff extends RequiredOptionalFunctionParametersSniff |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * A list of function parameters, which were optional in older versions and became required later on. |
||
26 | * |
||
27 | * The array lists : version number with true (required) and false (optional use deprecated). |
||
28 | * |
||
29 | * The index is the location of the parameter in the parameter list, starting at 0 ! |
||
30 | * If's sufficient to list the last version in which the parameter was not yet required. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $functionParameters = array( |
||
35 | 'parse_str' => array( |
||
36 | 1 => array( |
||
37 | 'name' => 'result', |
||
38 | '7.2' => false, |
||
39 | ), |
||
40 | ), |
||
41 | ); |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Determine whether an error/warning should be thrown for an item based on collected information. |
||
46 | * |
||
47 | * @param array $errorInfo Detail information about an item. |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | protected function shouldThrowError(array $errorInfo) |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Retrieve the relevant detail (version) information for use in an error message. |
||
59 | * |
||
60 | * @param array $itemArray Version and other information about the item. |
||
61 | * @param array $itemInfo Base information about the item. |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | View Code Duplication | public function getErrorInfo(array $itemArray, array $itemInfo) |
|
94 | |||
95 | |||
96 | /** |
||
97 | * Get the error message template for this sniff. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | protected function getErrorMsgTemplate() |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Generates the error or warning for this item. |
||
109 | * |
||
110 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
111 | * @param int $stackPtr The position of the relevant token in |
||
112 | * the stack. |
||
113 | * @param array $itemInfo Base information about the item. |
||
114 | * @param array $errorInfo Array with detail (version) information |
||
115 | * relevant to the item. |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | public function addError(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
||
147 | |||
148 | |||
149 | }//end class |
||
150 |
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.