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 |
||
57 | class Test extends ExtendedBase { |
||
58 | |||
59 | const DELAY_STABILIZE_PLATFORM = 3; |
||
60 | |||
61 | /** @var RunningService */ |
||
62 | private $runningService; |
||
63 | |||
64 | /** @var PlatformService */ |
||
65 | private $platformService; |
||
66 | |||
67 | /** @var ProviderService */ |
||
68 | private $providerService; |
||
69 | |||
70 | /** @var IndexService */ |
||
71 | private $indexService; |
||
72 | |||
73 | /** @var TestService */ |
||
74 | private $testService; |
||
75 | |||
76 | /** @var MiscService */ |
||
77 | private $miscService; |
||
78 | |||
79 | |||
80 | /** @var Runner */ |
||
81 | private $runner; |
||
82 | |||
83 | |||
84 | /** @var boolean */ |
||
85 | private $isJson = false; |
||
86 | |||
87 | /** |
||
88 | * Index constructor. |
||
89 | * |
||
90 | * @param RunningService $runningService |
||
91 | * @param ProviderService $providerService |
||
92 | * @param IndexService $indexService |
||
93 | * @param PlatformService $platformService |
||
94 | * @param TestService $testService |
||
95 | * @param MiscService $miscService |
||
96 | */ |
||
97 | View Code Duplication | public function __construct( |
|
111 | |||
112 | |||
113 | /** |
||
114 | * |
||
115 | */ |
||
116 | protected function configure() { |
||
126 | |||
127 | |||
128 | /** |
||
129 | * @param InputInterface $input |
||
130 | * @param OutputInterface $output |
||
131 | * |
||
132 | * @return int|null|void |
||
133 | * @throws Exception |
||
134 | */ |
||
135 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
136 | $this->isJson = ($input->getOption('json') === true); |
||
137 | $platformDelay = ($input->getOption('platform_delay') > 0) ? $input->getOption( |
||
138 | 'platform_delay' |
||
139 | ) : self::DELAY_STABILIZE_PLATFORM; |
||
140 | |||
141 | $this->output($output, '.Testing your current setup:'); |
||
142 | |||
143 | try { |
||
144 | $testProvider = $this->testCreatingProvider($output); |
||
145 | $this->testMockedProvider($output, $testProvider); |
||
146 | $testPlatform = $this->testLoadingPlatform($output); |
||
147 | $this->testLockingProcess($output, $testPlatform, $testProvider); |
||
148 | } catch (Exception $e) { |
||
149 | $this->output($output, false); |
||
150 | throw $e; |
||
151 | } |
||
152 | |||
153 | try { |
||
154 | $this->testResetTest($output, $testProvider); |
||
155 | $this->pause($output, $platformDelay); |
||
156 | $this->testInitIndexing($output, $testPlatform); |
||
157 | $this->testIndexingDocuments($output, $testPlatform, $testProvider); |
||
158 | $this->pause($output, $platformDelay); |
||
159 | $this->testContentLicense($output, $testPlatform); |
||
160 | $this->testSearchSimple($output, $testPlatform, $testProvider); |
||
161 | |||
162 | $this->testUpdatingDocumentsAccess($output, $testPlatform, $testProvider); |
||
163 | $this->pause($output, $platformDelay); |
||
164 | $this->testSearchAccess($output, $testPlatform, $testProvider); |
||
165 | $this->testSearchShare($output, $testPlatform, $testProvider); |
||
166 | |||
167 | $this->testResetTest($output, $testProvider); |
||
168 | $this->testUnlockingProcess($output); |
||
169 | } catch (Exception $e) { |
||
170 | $this->output($output, false); |
||
171 | $this->output($output, 'Error detected, unlocking process'); |
||
172 | $this->runner->stop(); |
||
173 | $this->output($output, true); |
||
174 | |||
175 | throw $e; |
||
176 | } |
||
177 | |||
178 | $this->output($output, '', true); |
||
179 | } |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @return IFullTextSearchProvider |
||
184 | * @throws ProviderIsNotCompatibleException |
||
185 | * @throws QueryException |
||
186 | * @throws ProviderDoesNotExistException |
||
187 | * @throws ProviderIsNotUniqueException |
||
188 | */ |
||
189 | private function generateMockProvider() { |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @param OutputInterface $output |
||
198 | * @param string|bool $line |
||
199 | * @param bool $isNewLine |
||
200 | */ |
||
201 | private function output(OutputInterface $output, $line, $isNewLine = true) { |
||
209 | |||
210 | |||
211 | /** |
||
212 | * @param string|bool $line |
||
213 | * @param $isNewLine |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | private function convertBoolToLine($line, &$isNewLine) { |
||
229 | |||
230 | |||
231 | /** |
||
232 | * @param $output |
||
233 | * |
||
234 | * @return IFullTextSearchProvider |
||
235 | * @throws ProviderDoesNotExistException |
||
236 | * @throws ProviderIsNotCompatibleException |
||
237 | * @throws ProviderIsNotUniqueException |
||
238 | * @throws QueryException |
||
239 | */ |
||
240 | private function testCreatingProvider($output) { |
||
247 | |||
248 | |||
249 | /** |
||
250 | * @param $output |
||
251 | * @param IFullTextSearchProvider $testProvider |
||
252 | */ |
||
253 | private function testMockedProvider($output, IFullTextSearchProvider $testProvider) { |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param $output |
||
264 | * |
||
265 | * @return IFullTextSearchPlatform |
||
266 | * @throws Exception |
||
267 | */ |
||
268 | private function testLoadingPlatform($output) { |
||
269 | $this->output($output, 'Loading search platform.'); |
||
270 | $testPlatform = $this->platformService->getPlatform(); |
||
271 | $this->output($output, '(' . $testPlatform->getName() . ')', false); |
||
272 | $this->output($output, true); |
||
273 | |||
274 | $this->output($output, 'Testing search platform.'); |
||
275 | if (!$testPlatform->testPlatform()) { |
||
276 | throw new Exception ('Search platform (' . $testPlatform->getName() . ') down ?'); |
||
277 | } |
||
278 | $this->output($output, true); |
||
279 | |||
280 | return $testPlatform; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param OutputInterface $output |
||
285 | * @param IFullTextSearchPlatform $testPlatform |
||
286 | * @param IFullTextSearchProvider $testProvider |
||
287 | * |
||
288 | * @throws RunnerAlreadyUpException |
||
289 | */ |
||
290 | private function testLockingProcess( |
||
302 | |||
303 | |||
304 | /** |
||
305 | * @param OutputInterface $output |
||
306 | * @param IFullTextSearchProvider $testProvider |
||
307 | * |
||
308 | * @throws Exception |
||
309 | */ |
||
310 | private function testResetTest(OutputInterface $output, IFullTextSearchProvider $testProvider |
||
316 | |||
317 | |||
318 | /** |
||
319 | * @param OutputInterface $output |
||
320 | * @param IFullTextSearchPlatform $testPlatform |
||
321 | */ |
||
322 | private function testInitIndexing(OutputInterface $output, IFullTextSearchPlatform $testPlatform |
||
328 | |||
329 | |||
330 | /** |
||
331 | * @param OutputInterface $output |
||
332 | * @param IFullTextSearchPlatform $testPlatform |
||
333 | * @param IFullTextSearchProvider $testProvider |
||
334 | * |
||
335 | * @throws InterruptException |
||
336 | * @throws TickDoesNotExistException |
||
337 | */ |
||
338 | private function testIndexingDocuments( |
||
353 | |||
354 | |||
355 | /** |
||
356 | * @param OutputInterface $output |
||
357 | * @param IFullTextSearchPlatform $testPlatform |
||
358 | * |
||
359 | * @throws Exception |
||
360 | */ |
||
361 | private function testContentLicense( |
||
388 | |||
389 | |||
390 | /** |
||
391 | * @param OutputInterface $output |
||
392 | * @param IFullTextSearchPlatform $testPlatform |
||
393 | * |
||
394 | * @param IFullTextSearchProvider $testProvider |
||
395 | * |
||
396 | * @throws Exception |
||
397 | */ |
||
398 | private function testSearchSimple( |
||
438 | |||
439 | |||
440 | /** |
||
441 | * @param OutputInterface $output |
||
442 | * @param IFullTextSearchPlatform $testPlatform |
||
443 | * @param IFullTextSearchProvider $testProvider |
||
444 | * |
||
445 | * @throws InterruptException |
||
446 | * @throws TickDoesNotExistException |
||
447 | */ |
||
448 | private function testUpdatingDocumentsAccess( |
||
465 | |||
466 | |||
467 | /** |
||
468 | * @param OutputInterface $output |
||
469 | * @param IFullTextSearchPlatform $platform |
||
470 | * |
||
471 | * @param IFullTextSearchProvider $provider |
||
472 | * |
||
473 | * @throws Exception |
||
474 | */ |
||
475 | private function testSearchAccess( |
||
499 | |||
500 | |||
501 | /** |
||
502 | * @param OutputInterface $output |
||
503 | * @param IFullTextSearchPlatform $platform |
||
504 | * |
||
505 | * @param IFullTextSearchProvider $provider |
||
506 | * |
||
507 | * @throws Exception |
||
508 | */ |
||
509 | private function testSearchShare( |
||
520 | |||
521 | |||
522 | /** |
||
523 | * @param OutputInterface $output |
||
524 | * |
||
525 | * @throws TickDoesNotExistException |
||
526 | */ |
||
527 | private function testUnlockingProcess(OutputInterface $output) { |
||
532 | |||
533 | |||
534 | /** |
||
535 | * @param OutputInterface $output |
||
536 | * @param IFullTextSearchPlatform $testPlatform |
||
537 | * @param IFullTextSearchProvider $testProvider |
||
538 | * @param DocumentAccess $access |
||
539 | * @param string $search |
||
540 | * @param array $expected |
||
541 | * @param string $moreOutput |
||
542 | * |
||
543 | * @throws Exception |
||
544 | */ |
||
545 | private function search( |
||
566 | |||
567 | |||
568 | /** |
||
569 | * @param OutputInterface $output |
||
570 | * @param IFullTextSearchPlatform $testPlatform |
||
571 | * @param IFullTextSearchProvider $testProvider |
||
572 | * @param array $groups |
||
573 | * @param array $expected |
||
574 | * |
||
575 | * @throws Exception |
||
576 | */ |
||
577 | View Code Duplication | private function searchGroups( |
|
591 | |||
592 | |||
593 | /** |
||
594 | * @param OutputInterface $output |
||
595 | * @param IFullTextSearchPlatform $testPlatform |
||
596 | * @param IFullTextSearchProvider $testProvider |
||
597 | * @param string $user |
||
598 | * @param array $expected |
||
599 | * |
||
600 | * @throws Exception |
||
601 | */ |
||
602 | View Code Duplication | private function searchUsers( |
|
613 | |||
614 | |||
615 | /** |
||
616 | * @param SearchResult $searchResult |
||
617 | * @param $entries |
||
618 | * |
||
619 | * @throws Exception |
||
620 | */ |
||
621 | private function compareSearchResult(SearchResult $searchResult, $entries) { |
||
633 | |||
634 | |||
635 | /** |
||
636 | * @param OutputInterface $output |
||
637 | * @param int $s |
||
638 | * |
||
639 | * @throws InterruptException |
||
640 | */ |
||
641 | private function pause(OutputInterface $output, $s) { |
||
654 | |||
655 | } |
||
656 | |||
659 |
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.