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:
Complex classes like FlexiBeeROTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FlexiBeeROTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class FlexiBeeROTest extends \Test\Ease\BrickTest |
||
11 | { |
||
12 | /** |
||
13 | * @var FlexiBeeRO |
||
14 | */ |
||
15 | protected $object; |
||
16 | |||
17 | /** |
||
18 | * Sets up the fixture, for example, opens a network connection. |
||
19 | * This method is called before a test is executed. |
||
20 | * @covers FlexiPeeHP\FlexiBeeRO::__construct |
||
21 | */ |
||
22 | protected function setUp() |
||
23 | { |
||
24 | $this->object = new FlexiBeeRO(); |
||
25 | $this->object->prefix = ''; |
||
26 | $this->object->company = ''; |
||
27 | $this->object->debug = true; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Tears down the fixture, for example, closes a network connection. |
||
32 | * This method is called after a test is executed. |
||
33 | */ |
||
34 | protected function tearDown() |
||
35 | { |
||
36 | |||
37 | } |
||
38 | |||
39 | public function testConstructor() |
||
40 | { |
||
41 | $classname = get_class($this->object); |
||
42 | $evidence = $this->object->getEvidence(); |
||
43 | |||
44 | // Get mock, without the constructor being called |
||
45 | $mock = $this->getMockBuilder($classname) |
||
46 | ->disableOriginalConstructor() |
||
47 | ->getMockForAbstractClass(); |
||
48 | $mock->__construct(''); |
||
49 | |||
50 | if (!isset(\FlexiPeeHP\EvidenceList::$name[$evidence])) { |
||
51 | $evidence = 'adresar'; |
||
52 | } |
||
53 | |||
54 | $mock->__construct('', |
||
55 | [ |
||
56 | 'company' => 'Firma_s_r_o_', |
||
57 | 'url' => 'https://flexibee.firma.cz/', |
||
58 | 'user' => 'rest', |
||
59 | 'password' => '-dj3x21xaA_', |
||
60 | 'debug' => true, |
||
61 | 'prefix' => 'c', |
||
62 | 'evidence' => $evidence]); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @covers FlexiPeeHP\FlexiBeeRO::curlInit |
||
67 | */ |
||
68 | public function testCurlInit() |
||
73 | |||
74 | /** |
||
75 | * @covers FlexiPeeHP\FlexiBeeRO::processInit |
||
76 | */ |
||
77 | public function testProcessInit() |
||
100 | |||
101 | /** |
||
102 | * @covers FlexiPeeHP\Flexi/home/vitex/Projects/Spoje.Net/flexipeehp/testing/src/FlexiPeeHP/FlexiBeeROTest.php:86BeeRO::setUp |
||
103 | */ |
||
104 | public function testSetUp() |
||
123 | |||
124 | /** |
||
125 | * @covers FlexiPeeHP\FlexiBeeRO::setPrefix |
||
126 | * @expectedException \Exception |
||
127 | */ |
||
128 | public function testSetPrefix() |
||
136 | |||
137 | /** |
||
138 | * @covers FlexiPeeHP\FlexiBeeRO::setFormat |
||
139 | */ |
||
140 | public function testSetFormat() |
||
145 | |||
146 | /** |
||
147 | * We can set only evidence defined in EvidenceList class |
||
148 | * |
||
149 | * @covers FlexiPeeHP\FlexiBeeRO::setEvidence |
||
150 | * @expectedException \Exception |
||
151 | */ |
||
152 | public function testSetEvidence() |
||
159 | |||
160 | /** |
||
161 | * @covers FlexiPeeHP\FlexiBeeRO::setCompany |
||
162 | */ |
||
163 | public function testSetCompany() |
||
168 | |||
169 | /** |
||
170 | * @covers FlexiPeeHP\FlexiBeeRO::object2array |
||
171 | */ |
||
172 | public function testObject2array() |
||
182 | |||
183 | /** |
||
184 | * @covers FlexiPeeHP\FlexiBeeRO::objectToID |
||
185 | */ |
||
186 | public function testObjectToID() |
||
198 | |||
199 | /** |
||
200 | * @covers FlexiPeeHP\FlexiBeeRO::performRequest |
||
201 | */ |
||
202 | public function testPerformRequest() |
||
230 | |||
231 | /** |
||
232 | * @covers FlexiPeeHP\FlexiBeeRO::setAction |
||
233 | */ |
||
234 | public function testSetAction() |
||
246 | |||
247 | /** |
||
248 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidence |
||
249 | */ |
||
250 | public function testGetEvidence() |
||
255 | |||
256 | /** |
||
257 | * @covers FlexiPeeHP\FlexiBeeRO::getCompany |
||
258 | */ |
||
259 | public function testGetCompany() |
||
263 | |||
264 | /** |
||
265 | * @covers FlexiPeeHP\FlexiBeeRO::getResponseEvidence |
||
266 | */ |
||
267 | public function testGetResponseEvidence() |
||
272 | |||
273 | /** |
||
274 | * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId |
||
275 | * @depends testInsertToFlexiBee |
||
276 | */ |
||
277 | public function testGetLastInsertedId() |
||
281 | |||
282 | /** |
||
283 | * @covers FlexiPeeHP\FlexiBeeRO::xml2array |
||
284 | */ |
||
285 | public function testXml2array() |
||
302 | |||
303 | /** |
||
304 | * @covers FlexiPeeHP\FlexiBeeRO::disconnect |
||
305 | * |
||
306 | * @depends testPerformRequest |
||
307 | * @depends testLoadFlexiData |
||
308 | * @depends testGetFlexiRow |
||
309 | * @depends testGetFlexiData |
||
310 | * @depends testLoadFromFlexiBee |
||
311 | * @depends testInsertToFlexiBee |
||
312 | * @depends testIdExists |
||
313 | * @depends testRecordExists |
||
314 | * @depends testGetColumnsFromFlexibee |
||
315 | * @depends testSearchString |
||
316 | */ |
||
317 | public function testDisconnect() |
||
322 | |||
323 | /** |
||
324 | * @covers FlexiPeeHP\FlexiBeeRO::__destruct |
||
325 | * @depends testDisconnect |
||
326 | */ |
||
327 | public function testdestruct() |
||
331 | |||
332 | /** |
||
333 | * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow |
||
334 | */ |
||
335 | public function testGetFlexiRow() |
||
340 | |||
341 | /** |
||
342 | * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData |
||
343 | */ |
||
344 | public function testGetFlexiData() |
||
376 | |||
377 | /** |
||
378 | * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee |
||
379 | */ |
||
380 | public function testLoadFromFlexiBee() |
||
385 | |||
386 | /** |
||
387 | * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData |
||
388 | */ |
||
389 | public function testJsonizeData() |
||
400 | |||
401 | /** |
||
402 | * @covers FlexiPeeHP\FlexiBeeRO::idExists |
||
403 | */ |
||
404 | public function testIdExists() |
||
411 | |||
412 | /** |
||
413 | * @covers FlexiPeeHP\FlexiBeeRO::getRecordID |
||
414 | * @todo Implement testGetRecordID(). |
||
415 | */ |
||
416 | public function testGetRecordID() |
||
423 | |||
424 | /** |
||
425 | * @covers FlexiPeeHP\FlexiBeeRO::recordExists |
||
426 | */ |
||
427 | public function testRecordExists() |
||
457 | |||
458 | /** |
||
459 | * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee |
||
460 | */ |
||
461 | public function testGetColumnsFromFlexibee() |
||
468 | |||
469 | /** |
||
470 | * @covers FlexiPeeHP\FlexiBeeRO::getExternalID |
||
471 | * @todo Implement testGetExternalID(). |
||
472 | */ |
||
473 | public function testGetExternalID() |
||
480 | |||
481 | /** |
||
482 | * @covers FlexiPeeHP\FlexiBeeRO::getGlobalVersion |
||
483 | * @todo Implement testGetGlobalVersion(). |
||
484 | */ |
||
485 | public function testGetGlobalVersion() |
||
492 | |||
493 | /** |
||
494 | * @covers FlexiPeeHP\FlexiBeeRO::getResponseFormat |
||
495 | * @todo Implement testGetResponseFormat(). |
||
496 | */ |
||
497 | public function testGetResponseFormat() |
||
504 | |||
505 | /** |
||
506 | * @covers FlexiPeeHP\FlexiBeeRO::getKod |
||
507 | */ |
||
508 | public function testGetKod() |
||
535 | |||
536 | /** |
||
537 | * @covers FlexiPeeHP\FlexiBeeRO::logResult |
||
538 | */ |
||
539 | public function testLogResult() |
||
560 | |||
561 | /** |
||
562 | * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl |
||
563 | */ |
||
564 | public function testFlexiUrl() |
||
575 | |||
576 | /** |
||
577 | * @covers FlexiPeeHP\FlexiBeeRO::unifyResponseFormat |
||
578 | */ |
||
579 | public function testunifyResponseFormat() |
||
620 | |||
621 | /** |
||
622 | * @covers FlexiPeeHP\FlexiBeeRO::__toString |
||
623 | */ |
||
624 | public function testtoString() |
||
638 | |||
639 | /** |
||
640 | * @covers FlexiPeeHP\FlexiBeeRO::draw |
||
641 | */ |
||
642 | public function testDraw($whatWant = NULL) |
||
647 | |||
648 | /** |
||
649 | * @covers FlexiPeeHP\FlexiBeeRO::getColumnsInfo |
||
650 | */ |
||
651 | View Code Duplication | public function testgetColumnsInfo() |
|
669 | |||
670 | /** |
||
671 | * @covers FlexiPeeHP\FlexiBeeRO::getActionsInfo |
||
672 | */ |
||
673 | View Code Duplication | public function testgetActionsInfo() |
|
691 | |||
692 | /** |
||
693 | * @covers FlexiPeeHP\FlexiBeeRO::getRelationsInfo |
||
694 | */ |
||
695 | View Code Duplication | public function testgetRelationsInfo() |
|
713 | |||
714 | /** |
||
715 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceUrl |
||
716 | */ |
||
717 | public function testgetEvidenceUrl() |
||
722 | |||
723 | /** |
||
724 | * @covers FlexiPeeHP\FlexiBeeRO::evidenceToClassName |
||
725 | */ |
||
726 | public function testevidenceToClassName() |
||
731 | |||
732 | /** |
||
733 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceInfo |
||
734 | * @todo Implement testGetEvidenceInfo(). |
||
735 | */ |
||
736 | public function testGetEvidenceInfo() |
||
743 | |||
744 | /** |
||
745 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceName |
||
746 | * @todo Implement testGetEvidenceName(). |
||
747 | */ |
||
748 | public function testGetEvidenceName() |
||
755 | |||
756 | /** |
||
757 | * @covers FlexiPeeHP\FlexiBeeRO::performAction |
||
758 | * @todo Implement testPerformAction(). |
||
759 | */ |
||
760 | public function testPerformAction() |
||
767 | |||
768 | /** |
||
769 | * @covers FlexiPeeHP\FlexiBeeRO::saveResponseToFile |
||
770 | * @todo Implement testSaveResponseToFile(). |
||
771 | */ |
||
772 | public function testSaveResponseToFile() |
||
779 | |||
780 | /** |
||
781 | * @covers FlexiPeeHP\FlexiBeeRO::getVazby |
||
782 | * @todo Implement testGetVazby(). |
||
783 | */ |
||
784 | public function testGetVazby() |
||
791 | |||
792 | } |
||
793 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: