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 |
||
13 | final class FeatureContext implements Context |
||
14 | { |
||
15 | /** @var Merger */ |
||
16 | private $merger; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $generatedPdf = ''; |
||
20 | |||
21 | /** @var ?\Exception */ |
||
22 | private $mergeException; |
||
23 | |||
24 | public function __construct(string $driverName) |
||
33 | |||
34 | /** |
||
35 | * @Given the :driver driver |
||
36 | */ |
||
37 | public function theDriver(string $driver) |
||
42 | |||
43 | /** |
||
44 | * @Given a pdf |
||
45 | */ |
||
46 | public function aPdf() |
||
50 | |||
51 | /** |
||
52 | * @Given a pdf with pages :pages |
||
53 | */ |
||
54 | public function aPdfWithPages($pages) |
||
58 | |||
59 | /** |
||
60 | * @Given a pdf of version :version |
||
61 | */ |
||
62 | public function aPdfOfVersion(string $version) |
||
66 | |||
67 | /** |
||
68 | * @Given a pdf of version :version with pages :pages |
||
69 | */ |
||
70 | public function aPdfOfVersionWithPages(string $version, string $pages) |
||
74 | |||
75 | /** |
||
76 | * @Given a pdf with a header including text HEADER |
||
77 | */ |
||
78 | public function aPdfWithAHeaderIncludingTextHeader() |
||
82 | |||
83 | /** |
||
84 | * @Given a blank pdf |
||
85 | */ |
||
86 | public function aBlankPdf() |
||
90 | |||
91 | /** |
||
92 | * @When I merge |
||
93 | */ |
||
94 | public function iMerge() |
||
103 | |||
104 | /** |
||
105 | * @Then there is no error |
||
106 | */ |
||
107 | public function thereIsNoError() |
||
113 | |||
114 | /** |
||
115 | * @Then there is an error |
||
116 | */ |
||
117 | public function thereIsAnError() |
||
123 | |||
124 | /** |
||
125 | * @Then a pdf is generated |
||
126 | */ |
||
127 | public function aPdfIsGenerated() |
||
132 | |||
133 | /** |
||
134 | * @Then a pdf with :expectedCount pages is generated |
||
135 | */ |
||
136 | public function aPdfWithPagesIsGenerated(string $expectedCount) |
||
146 | |||
147 | /** |
||
148 | * @Then a pdf including text :expectedText is generated |
||
149 | */ |
||
150 | View Code Duplication | public function aPdfIncludingTextIsGenerated(string $expectedText) |
|
162 | |||
163 | /** |
||
164 | * @Then a pdf not including text :unexpectedText is generated |
||
165 | */ |
||
166 | View Code Duplication | public function aPdfNotIncludingTextIsGenerated(string $unexpectedText) |
|
178 | } |
||
179 |
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.