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 |
||
19 | class JsonSpecContext implements Context, JsonHolderAware, MemoryHelperAware, JsonMatcherAware |
||
20 | { |
||
21 | |||
22 | use JsonMatcherAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * @var MemoryHelper |
||
26 | */ |
||
27 | private $memoryHelper; |
||
28 | |||
29 | /** |
||
30 | * @var JsonHolder |
||
31 | */ |
||
32 | private $jsonHolder; |
||
33 | |||
34 | /** |
||
35 | * @var JsonLoader |
||
36 | */ |
||
37 | private $jsonLoader; |
||
38 | |||
39 | /** |
||
40 | * @var JsonHelper |
||
41 | */ |
||
42 | private $jsonHelper; |
||
43 | |||
44 | /** |
||
45 | * @param JsonLoader $jsonLoader |
||
46 | * @param JsonHelper $jsonHelper |
||
47 | */ |
||
48 | public function __construct( |
||
56 | |||
57 | /** |
||
58 | * @Then /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ |
||
59 | */ |
||
60 | public function keepJson($path, $key) |
||
65 | |||
66 | /** |
||
67 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be(:)$/ |
||
68 | */ |
||
69 | public function checkEquality($path, $isNegative, PyStringNode $json) |
||
73 | |||
74 | /** |
||
75 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be file "(.+)"/ |
||
76 | */ |
||
77 | public function checkEqualityWithFileContents($path = null, $isNegative = null, $jsonFile) |
||
81 | |||
82 | /** |
||
83 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ |
||
84 | */ |
||
85 | public function checkEqualityInline($path, $isNegative, $json) |
||
95 | |||
96 | /** |
||
97 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include(:)$/ |
||
98 | */ |
||
99 | public function checkInclusion($path, $isNegative, PyStringNode $json) |
||
103 | |||
104 | /** |
||
105 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include file "(.+)"$/ |
||
106 | */ |
||
107 | public function checkInclusionOfFile($path, $isNegative, $jsonFile) |
||
111 | |||
112 | /** |
||
113 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ |
||
114 | */ |
||
115 | View Code Duplication | public function checkInclusionInline($path, $isNegative, $json) |
|
125 | |||
126 | /** |
||
127 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should have the following(:)$/ |
||
128 | */ |
||
129 | public function hasKeys($base, TableNode $table) |
||
142 | |||
143 | /** |
||
144 | * @Then /^the (?:JSON|json)(?: response)? should( not)? have "(.*)"$/ |
||
145 | */ |
||
146 | public function hasKeysInline($isNegative, $path) |
||
155 | |||
156 | /** |
||
157 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be an? (.*)$/ |
||
158 | */ |
||
159 | View Code Duplication | public function haveType($path, $isNegative, $type) |
|
169 | |||
170 | /** |
||
171 | * @Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? have (\d+)/ |
||
172 | */ |
||
173 | public function haveSize($path, $isNegative, $size) |
||
183 | |||
184 | /** |
||
185 | * @Then print last JSON response |
||
186 | */ |
||
187 | public function printLastJsonResponse() |
||
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | public function setJsonHolder(JsonHolder $holder) |
||
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | public function setMemoryHelper(MemoryHelper $memoryHelper) |
||
207 | |||
208 | /** |
||
209 | * @return string |
||
210 | */ |
||
211 | private function rememberJson($json = null) |
||
219 | |||
220 | private function normalizePath($path) |
||
228 | |||
229 | } |
||
230 |
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.