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 | abstract class PHPCompatibility_AbstractNewFeatureSniff extends PHPCompatibility_AbstractComplexVersionSniff |
||
18 | { |
||
19 | |||
20 | |||
21 | /** |
||
22 | * Determine whether an error/warning should be thrown for an item based on collected information. |
||
23 | * |
||
24 | * @param array $errorInfo Detail information about an item. |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | protected function shouldThrowError(array $errorInfo) |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Retrieve the relevant detail (version) information for use in an error message. |
||
36 | * |
||
37 | * @param array $itemArray Version and other information about the item. |
||
38 | * @param array $itemInfo Base information about the item. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function getErrorInfo(array $itemArray, array $itemInfo) |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Get the error message template for this sniff. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | protected function getErrorMsgTemplate() |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Generates the error or warning for this item. |
||
78 | * |
||
79 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
80 | * @param int $stackPtr The position of the relevant token in |
||
81 | * the stack. |
||
82 | * @param array $itemInfo Base information about the item. |
||
83 | * @param array $errorInfo Array with detail (version) information |
||
84 | * relevant to the item. |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo) |
||
104 | |||
105 | |||
106 | }//end class |
||
107 |
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.