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 |
||
| 25 | final class TemplateTest extends TestCase |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @covers ::createFromString |
||
| 29 | * @covers ::__construct |
||
| 30 | * @covers ::toString |
||
| 31 | * |
||
| 32 | * @dataProvider providesValidNotation |
||
| 33 | */ |
||
| 34 | public function testItCanBeInstantiatedWithAValidNotation(string $notation): void |
||
| 39 | |||
| 40 | public function providesValidNotation(): iterable |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @covers ::createFromString |
||
| 51 | * |
||
| 52 | * @dataProvider providesInvalidNotation |
||
| 53 | */ |
||
| 54 | public function testItFailsToInstantiatedWithAnInvalidNotation(string $notation): void |
||
| 60 | |||
| 61 | public function providesInvalidNotation(): iterable |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @covers ::__set_state |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function testSetState(): void |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @covers ::variableNames |
||
| 86 | * |
||
| 87 | * @dataProvider expectedVariableNames |
||
| 88 | */ |
||
| 89 | public function testGetVariableNames(string $template, array $expected): void |
||
| 93 | |||
| 94 | View Code Duplication | public function expectedVariableNames(): iterable |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @covers ::createFromString |
||
| 118 | */ |
||
| 119 | public function testExpandAcceptsOnlyStringAndStringableObject(): void |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @dataProvider providesExpansion |
||
| 128 | */ |
||
| 129 | public function testItCanExpandVariables(string $notation, array $variables, string $expected): void |
||
| 133 | |||
| 134 | public function providesExpansion(): iterable |
||
| 149 | } |
||
| 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.