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() |
||
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(1, ['debug' => false]); |
||
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::logBanner |
||
67 | */ |
||
68 | public function testLogBanner() |
||
69 | { |
||
70 | $this->object->logBanner(addslashes(get_class($this))); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @covers FlexiPeeHP\FlexiBeeRO::curlInit |
||
75 | */ |
||
76 | public function testSetupProperty() |
||
83 | |||
84 | /** |
||
85 | * @covers FlexiPeeHP\FlexiBeeRO::curlInit |
||
86 | */ |
||
87 | public function testCurlInit() |
||
92 | |||
93 | /** |
||
94 | * @covers FlexiPeeHP\FlexiBeeRO::processInit |
||
95 | */ |
||
96 | public function testProcessInit() |
||
97 | { |
||
98 | $this->object->processInit(['id' => 1]); |
||
99 | $this->assertEquals(1, $this->object->getDataValue('id')); |
||
100 | |||
101 | if (!is_null($this->object->evidence) && $this->object->evidence != 'test') { |
||
102 | |||
103 | |||
104 | $firstID = $this->object->getColumnsFromFlexibee(['id', 'kod'], |
||
105 | ['limit' => 1]); |
||
106 | |||
107 | if (count($firstID) && isset($firstID[0]['id'])) { |
||
108 | |||
109 | $this->object->processInit((int) current($firstID)); |
||
110 | $this->assertNotEmpty($this->object->__toString()); |
||
111 | |||
112 | if (isset($firstID[0]['kod'])) { |
||
113 | $this->object->processInit('code:'.$firstID[0]['kod']); |
||
114 | $this->assertNotEmpty($this->object->__toString()); |
||
115 | } |
||
116 | |||
117 | $this->object->processInit($this->object->getEvidenceURL().'/'.$firstID[0]['id'].'.xml'); |
||
118 | } else { |
||
119 | $this->markTestSkipped(sprintf('Evidence %s doed not contain first record', |
||
120 | $this->object->getEvidence())); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @covers FlexiPeeHP\FlexiBeeRO::setUp |
||
127 | */ |
||
128 | public function testSetUp() |
||
148 | |||
149 | /** |
||
150 | * @covers FlexiPeeHP\FlexiBeeRO::setPrefix |
||
151 | * @expectedException \Exception |
||
152 | */ |
||
153 | public function testSetPrefix() |
||
161 | |||
162 | /** |
||
163 | * @covers FlexiPeeHP\FlexiBeeRO::setFormat |
||
164 | */ |
||
165 | public function testSetFormat() |
||
170 | |||
171 | /** |
||
172 | * We can set only evidence defined in EvidenceList class |
||
173 | * |
||
174 | * @covers FlexiPeeHP\FlexiBeeRO::setEvidence |
||
175 | * @expectedException \Exception |
||
176 | */ |
||
177 | public function testSetEvidence() |
||
185 | |||
186 | /** |
||
187 | * @covers FlexiPeeHP\FlexiBeeRO::setCompany |
||
188 | */ |
||
189 | public function testSetCompany() |
||
194 | |||
195 | /** |
||
196 | * @covers FlexiPeeHP\FlexiBeeRO::object2array |
||
197 | */ |
||
198 | public function testObject2array() |
||
208 | |||
209 | /** |
||
210 | * @covers FlexiPeeHP\FlexiBeeRO::objectToID |
||
211 | */ |
||
212 | public function testObjectToID() |
||
224 | |||
225 | /** |
||
226 | * @covers FlexiPeeHP\FlexiBeeRO::performRequest |
||
227 | */ |
||
228 | public function testPerformRequest() |
||
261 | |||
262 | /** |
||
263 | * @covers FlexiPeeHP\FlexiBeeRO::setAction |
||
264 | */ |
||
265 | public function testSetAction() |
||
286 | |||
287 | /** |
||
288 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidence |
||
289 | */ |
||
290 | public function testGetEvidence() |
||
295 | |||
296 | /** |
||
297 | * @covers FlexiPeeHP\FlexiBeeRO::getCompany |
||
298 | */ |
||
299 | public function testGetCompany() |
||
303 | |||
304 | /** |
||
305 | * @covers FlexiPeeHP\FlexiBeeRO::getResponseEvidence |
||
306 | */ |
||
307 | View Code Duplication | public function testGetResponseEvidence() |
|
325 | |||
326 | /** |
||
327 | * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId |
||
328 | * @depends testInsertToFlexiBee |
||
329 | */ |
||
330 | public function testGetLastInsertedId() |
||
334 | |||
335 | /** |
||
336 | * @covers FlexiPeeHP\FlexiBeeRO::xml2array |
||
337 | */ |
||
338 | public function testXml2array() |
||
355 | |||
356 | /** |
||
357 | * @covers FlexiPeeHP\FlexiBeeRO::disconnect |
||
358 | * |
||
359 | * @depends testPerformRequest |
||
360 | * @depends testLoadFlexiData |
||
361 | * @depends testGetFlexiRow |
||
362 | * @depends testGetFlexiData |
||
363 | * @depends testLoadFromFlexiBee |
||
364 | * @depends testInsertToFlexiBee |
||
365 | * @depends testIdExists |
||
366 | * @depends testRecordExists |
||
367 | * @depends testGetColumnsFromFlexibee |
||
368 | * @depends testSearchString |
||
369 | */ |
||
370 | public function testDisconnect() |
||
375 | |||
376 | /** |
||
377 | * @covers FlexiPeeHP\FlexiBeeRO::__destruct |
||
378 | * @depends testDisconnect |
||
379 | */ |
||
380 | public function testdestruct() |
||
384 | |||
385 | /** |
||
386 | * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow |
||
387 | */ |
||
388 | public function testGetFlexiRow() |
||
393 | |||
394 | /** |
||
395 | * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData |
||
396 | */ |
||
397 | public function testGetFlexiData() |
||
438 | |||
439 | /** |
||
440 | * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee |
||
441 | */ |
||
442 | public function testLoadFromFlexiBee() |
||
447 | |||
448 | /** |
||
449 | * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData |
||
450 | */ |
||
451 | public function testJsonizeData() |
||
473 | |||
474 | /** |
||
475 | * @covers FlexiPeeHP\FlexiBeeRO::idExists |
||
476 | */ |
||
477 | public function testIdExists() |
||
501 | |||
502 | /** |
||
503 | * @covers FlexiPeeHP\FlexiBeeRO::getRecordID |
||
504 | */ |
||
505 | public function testGetRecordID() |
||
512 | |||
513 | /** |
||
514 | * @covers FlexiPeeHP\FlexiBeeRO::recordExists |
||
515 | */ |
||
516 | public function testRecordExists() |
||
550 | |||
551 | /** |
||
552 | * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee |
||
553 | */ |
||
554 | public function testGetColumnsFromFlexibee() |
||
573 | |||
574 | /** |
||
575 | * @covers FlexiPeeHP\FlexiBeeRO::getExternalID |
||
576 | */ |
||
577 | public function testGetExternalID() |
||
587 | |||
588 | /** |
||
589 | * @covers FlexiPeeHP\FlexiBeeRO::getGlobalVersion |
||
590 | */ |
||
591 | View Code Duplication | public function testGetGlobalVersion() |
|
610 | |||
611 | /** |
||
612 | * @covers FlexiPeeHP\FlexiBeeRO::getResponseFormat |
||
613 | */ |
||
614 | public function testGetResponseFormat() |
||
623 | |||
624 | /** |
||
625 | * @covers FlexiPeeHP\FlexiBeeRO::getKod |
||
626 | */ |
||
627 | public function testGetKod() |
||
655 | |||
656 | /** |
||
657 | * @covers FlexiPeeHP\FlexiBeeRO::logResult |
||
658 | */ |
||
659 | public function testLogResult() |
||
680 | |||
681 | /** |
||
682 | * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl |
||
683 | */ |
||
684 | public function testFlexiUrl() |
||
695 | |||
696 | /** |
||
697 | * @covers FlexiPeeHP\FlexiBeeRO::unifyResponseFormat |
||
698 | */ |
||
699 | public function testunifyResponseFormat() |
||
740 | |||
741 | /** |
||
742 | * @covers FlexiPeeHP\FlexiBeeRO::__toString |
||
743 | */ |
||
744 | public function testtoString() |
||
758 | |||
759 | /** |
||
760 | * @covers FlexiPeeHP\FlexiBeeRO::draw |
||
761 | */ |
||
762 | public function testDraw($whatWant = NULL) |
||
767 | |||
768 | /** |
||
769 | * @covers FlexiPeeHP\FlexiBeeRO::getColumnsInfo |
||
770 | */ |
||
771 | View Code Duplication | public function testgetColumnsInfo() |
|
789 | |||
790 | /** |
||
791 | * @covers FlexiPeeHP\FlexiBeeRO::getActionsInfo |
||
792 | */ |
||
793 | public function testgetActionsInfo() |
||
813 | |||
814 | /** |
||
815 | * @covers FlexiPeeHP\FlexiBeeRO::getRelationsInfo |
||
816 | */ |
||
817 | View Code Duplication | public function testgetRelationsInfo() |
|
837 | |||
838 | /** |
||
839 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceUrl |
||
840 | */ |
||
841 | public function testgetEvidenceUrl() |
||
846 | |||
847 | /** |
||
848 | * @covers FlexiPeeHP\FlexiBeeRO::evidenceToClassName |
||
849 | */ |
||
850 | public function testevidenceToClassName() |
||
855 | |||
856 | /** |
||
857 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceInfo |
||
858 | */ |
||
859 | public function testGetEvidenceInfo() |
||
876 | |||
877 | /** |
||
878 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidenceName |
||
879 | */ |
||
880 | public function testGetEvidenceName() |
||
897 | |||
898 | /** |
||
899 | * @covers FlexiPeeHP\FlexiBeeRO::performAction |
||
900 | * @expectedException \Exception |
||
901 | */ |
||
902 | public function testPerformAction() |
||
917 | |||
918 | /** |
||
919 | * @covers FlexiPeeHP\FlexiBeeRO::saveResponseToFile |
||
920 | */ |
||
921 | public function testSaveResponseToFile() |
||
927 | |||
928 | /** |
||
929 | * @covers FlexiPeeHP\FlexiBeeRO::getFirstRecordID() |
||
930 | */ |
||
931 | public function testgetFirstRecordID() |
||
952 | |||
953 | /** |
||
954 | * @covers FlexiPeeHP\FlexiBeeRO::getVazby |
||
955 | * @expectedException \Exception |
||
956 | */ |
||
957 | public function testGetVazby() |
||
984 | |||
985 | /** |
||
986 | * @covers FlexiPeeHP\FlexiBeeRO::evidenceUrlWithSuffix |
||
987 | */ |
||
988 | public function testEvidenceUrlWithSuffix() |
||
998 | |||
999 | /** |
||
1000 | * @covers FlexiPeeHP\FlexiBeeRO::addUrlParams |
||
1001 | */ |
||
1002 | public function testAddUrlParams() |
||
1008 | |||
1009 | /** |
||
1010 | * @covers FlexiPeeHP\FlexiBeeRO::addDefaultUrlParams |
||
1011 | */ |
||
1012 | public function testAddDefaultUrlParams() |
||
1021 | |||
1022 | public function testFlexiDateToDateTime() |
||
1027 | |||
1028 | public function testFlexiDateTimeToDateTime() |
||
1033 | } |
||
1034 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.