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 |
||
9 | class FeatureContext implements Context |
||
10 | { |
||
11 | |||
12 | use \JuliusHaertl\NextcloudBehat\FilesSharingContextTrait; |
||
13 | use \JuliusHaertl\NextcloudBehat\FilesDavContextTrait; |
||
14 | use \JuliusHaertl\NextcloudBehat\UserContextTrait; |
||
15 | use \JuliusHaertl\NextcloudBehat\UserWebContextTrait; |
||
16 | |||
17 | /** |
||
18 | * Initializes context. |
||
19 | * |
||
20 | * Every scenario gets its own context instance. |
||
21 | * You can also pass arbitrary arguments to the |
||
22 | * context constructor through behat.yml. |
||
23 | */ |
||
24 | public function __construct($baseUrl) |
||
28 | |||
29 | /** |
||
30 | * @When User :user opens :file |
||
31 | */ |
||
32 | public function userOpens($user, $file) |
||
55 | |||
56 | |||
57 | /** |
||
58 | * @Then a guest opens the share link |
||
59 | */ |
||
60 | public function aGuestOpensTheShareLink() |
||
61 | { |
||
62 | View Code Duplication | if (count($this->lastShareData->data->element) > 0){ |
|
63 | $token = $this->lastShareData->data[0]->token; |
||
64 | } else { |
||
65 | $token = $this->lastShareData->data->token; |
||
66 | } |
||
67 | |||
68 | |||
69 | // public function publicPage($shareToken, $fileName, $fileId) { |
||
70 | $client = new Client(); |
||
71 | $result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/public?shareToken=' . $token, $this->getWebOptions()); |
||
72 | $contents =$result->getBody()->getContents(); |
||
73 | $re = '/var richdocuments_([A-z]+) = (.*);/m'; |
||
74 | preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0); |
||
75 | $result = []; |
||
76 | View Code Duplication | foreach ($matches as $match) { |
|
77 | $result[$match[1]] = str_replace("'", "", $match[2]); |
||
78 | } |
||
79 | |||
80 | $this->fileId = $result['fileId']; |
||
81 | $this->wopiToken = $result['token']; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @Then a guest opens the share link as :user |
||
86 | */ |
||
87 | public function aGuestOpensTheShareLinkAs($user) |
||
88 | { |
||
89 | View Code Duplication | if (count($this->lastShareData->data->element) > 0){ |
|
90 | $token = $this->lastShareData->data[0]->token; |
||
91 | } else { |
||
92 | $token = $this->lastShareData->data->token; |
||
93 | } |
||
94 | |||
95 | $cookieJar = \GuzzleHttp\Cookie\CookieJar::fromArray([ |
||
96 | 'guestUser' => $user |
||
97 | ], 'localhost'); |
||
98 | |||
99 | |||
100 | // public function publicPage($shareToken, $fileName, $fileId) { |
||
101 | $client = new Client(); |
||
102 | $result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/public?shareToken=' . $token, array_merge($this->getWebOptions(), [ |
||
103 | 'cookies' => $cookieJar |
||
104 | ])); |
||
105 | $contents = $result->getBody()->getContents(); |
||
106 | $re = '/var richdocuments_([A-z]+) = (.*);/m'; |
||
107 | preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0); |
||
108 | $result = []; |
||
109 | View Code Duplication | foreach ($matches as $match) { |
|
110 | $result[$match[1]] = str_replace("'", "", $match[2]); |
||
111 | } |
||
112 | |||
113 | $this->fileId = $result['fileId']; |
||
114 | $this->wopiToken = $result['token']; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @Then Collabora fetches checkFileInfo |
||
119 | */ |
||
120 | public function collaboraFetchesCheckfileinfo() { |
||
126 | |||
127 | /** |
||
128 | * @Then Collabora puts :source |
||
129 | */ |
||
130 | public function collaboraPuts($source) |
||
147 | |||
148 | /** |
||
149 | * @Then /^the HTTP status code should be "([^"]*)"$/ |
||
150 | * @param int $statusCode |
||
151 | */ |
||
152 | public function theHTTPStatusCodeShouldBe($statusCode) { |
||
155 | |||
156 | |||
157 | /** |
||
158 | * @Then checkFileInfo :arg1 is ":arg2" |
||
159 | */ |
||
160 | public function checkfileinfoIs($arg1, $arg2) |
||
164 | |||
165 | |||
166 | /** |
||
167 | * @Then checkFileInfo :arg1 matches ":arg2" |
||
168 | */ |
||
169 | public function checkfileinfoMatches($arg1, $arg2) |
||
173 | |||
174 | /** |
||
175 | * @Then checkFileInfo :arg1 is true |
||
176 | */ |
||
177 | public function checkfileinfoIsTrue($arg1) |
||
181 | |||
182 | /** |
||
183 | * @Then checkFileInfo :arg1 is false |
||
184 | */ |
||
185 | public function checkfileinfoIsFalse($arg1) |
||
189 | |||
190 | } |
||
191 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.