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 |
||
| 27 | class LongParameterListTest extends AbstractTest |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * testApplyIgnoresMethodsWithLessParametersThanMinimum |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | View Code Duplication | public function testApplyIgnoresMethodsWithLessParametersThanMinimum() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * testApplyReportsMethodsWithIdenticalParametersAndMinimum |
||
| 44 | * |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function testApplyReportsMethodsWithIdenticalParametersAndMinimum() |
|
| 54 | |||
| 55 | /** |
||
| 56 | * testApplyReportsMethodsWithMoreParametersThanMinimum |
||
| 57 | * |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function testApplyReportsMethodsWithMoreParametersThanMinimum() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * testApplyIgnoresFunctionsWithLessParametersThanMinimum |
||
| 70 | * |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function testApplyIgnoresFunctionsWithLessParametersThanMinimum() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * testApplyReportsFunctionsWithIdenticalParametersAndMinimum |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function testApplyReportsFunctionsWithIdenticalParametersAndMinimum() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * testApplyReportsFunctionsWithMoreParametersThanMinimum |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function testApplyReportsFunctionsWithMoreParametersThanMinimum() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Returns a mocked method instance. |
||
| 109 | * |
||
| 110 | * @param integer $parameterCount |
||
| 111 | * @return \PHPMD\Node\MethodNode |
||
| 112 | */ |
||
| 113 | private function createMethod($parameterCount) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Creates a mocked function node instance. |
||
| 120 | * |
||
| 121 | * @param integer $parameterCount Number of function parameters. |
||
| 122 | * |
||
| 123 | * @return \PHPMD\Node\FunctionNode |
||
| 124 | */ |
||
| 125 | private function createFunction($parameterCount) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Initializes the getParameterCount() method of the given callable. |
||
| 132 | * |
||
| 133 | * @param \PHPMD\Node\FunctionNode|\PHPMD\Node\MethodNode $mock |
||
| 134 | * @param integer $parameterCount |
||
| 135 | * @return \PHPMD\Node\FunctionNode|\PHPMD\Node\MethodNode |
||
| 136 | */ |
||
| 137 | private function initFunctionOrMethodMock($mock, $parameterCount) |
||
| 145 | } |
||
| 146 |
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.