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->evidence = 'c'; |
||
| 26 | $this->object->prefix = ''; |
||
| 27 | $this->object->company = ''; |
||
| 28 | $this->object->nameSpace = 'companies'; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Tears down the fixture, for example, closes a network connection. |
||
| 33 | * This method is called after a test is executed. |
||
| 34 | */ |
||
| 35 | protected function tearDown() |
||
| 36 | { |
||
| 37 | |||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @covers FlexiPeeHP\FlexiBeeRO::curlInit |
||
| 42 | */ |
||
| 43 | public function testCurlInit() |
||
| 44 | { |
||
| 45 | $this->object->curlInit(); |
||
| 46 | $this->assertTrue(is_resource($this->object->curl)); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @covers FlexiPeeHP\FlexiBeeRO::processInit |
||
| 51 | */ |
||
| 52 | public function testProcessInit() |
||
| 53 | { |
||
| 54 | $this->object->processInit(['id' => 1]); |
||
| 55 | $this->assertEquals(1, $this->object->getDataValue('id')); |
||
| 56 | if (!is_null($this->object->evidence) && $this->object->evidence != 'test') { |
||
| 57 | $firstID = $this->object->getColumnsFromFlexibee('id', |
||
|
|
|||
| 58 | ['limit' => 1]); |
||
| 59 | $this->object->processInit((int) current($firstID)); |
||
| 60 | $this->assertNotEmpty($this->object->__toString()); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @covers FlexiPeeHP\FlexiBeeRO::setEvidence |
||
| 66 | */ |
||
| 67 | public function testSetEvidence() |
||
| 68 | { |
||
| 69 | $this->object->setEvidence('nastaveni'); |
||
| 70 | $this->assertEquals('nastaveni', $this->object->evidence); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @covers FlexiPeeHP\FlexiBeeRO::object2array |
||
| 75 | */ |
||
| 76 | public function testObject2array() |
||
| 77 | { |
||
| 78 | $this->assertNull($this->object->object2array(new \stdClass())); |
||
| 79 | $this->assertEquals( |
||
| 80 | [ |
||
| 81 | 'item' => 1, |
||
| 82 | 'arrItem' => ['a', 'b' => 'c'] |
||
| 83 | ] |
||
| 84 | , $this->object->object2array(new \Test\ObjectForTesting())); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @covers FlexiPeeHP\FlexiBeeRO::objectToID |
||
| 89 | */ |
||
| 90 | public function testObjectToID() |
||
| 91 | { |
||
| 92 | $id = \Ease\Sand::randomNumber(1, 9999); |
||
| 93 | $this->object->setMyKey($id); |
||
| 94 | $this->assertEquals([$id], $this->object->objectToID([$this->object])); |
||
| 95 | |||
| 96 | $this->object->setDataValue('kod', 'TEST'); |
||
| 97 | $this->assertEquals('code:TEST', |
||
| 98 | $this->object->objectToID($this->object)); |
||
| 99 | |||
| 100 | $this->assertEquals('TEST', $this->object->objectToID('TEST')); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @covers FlexiPeeHP\FlexiBeeRO::performRequest |
||
| 105 | */ |
||
| 106 | public function testPerformRequest() |
||
| 107 | { |
||
| 108 | $evidence = $this->object->getEvidence(); |
||
| 109 | switch ($evidence) { |
||
| 110 | case null: |
||
| 111 | case '': |
||
| 112 | case 'test': |
||
| 113 | case 'c': |
||
| 114 | $this->object->evidence = 'c'; |
||
| 115 | $this->object->prefix = ''; |
||
| 116 | $this->object->company = ''; |
||
| 117 | $this->object->nameSpace = 'companies'; |
||
| 118 | $json = $this->object->performRequest(); |
||
| 119 | $this->assertArrayHasKey('company', $json); |
||
| 120 | |||
| 121 | $xml = $this->object->performRequest(null, 'GET', 'xml'); |
||
| 122 | $this->assertArrayHasKey('company', $xml); |
||
| 123 | break; |
||
| 124 | |||
| 125 | default: |
||
| 126 | $json = $this->object->performRequest($evidence.'.json'); |
||
| 127 | if (array_key_exists('message', $json)) { |
||
| 128 | $this->assertArrayHasKey('@version', $json); |
||
| 129 | } else { |
||
| 130 | $this->assertArrayHasKey($evidence, $json); |
||
| 131 | } |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | $err = $this->object->performRequest('error.json'); |
||
| 137 | $this->assertArrayHasKey('success', $err); |
||
| 138 | $this->assertEquals('false', $err['success']); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @covers FlexiPeeHP\FlexiBeeRO::setAction |
||
| 143 | */ |
||
| 144 | public function testSetAction() |
||
| 145 | { |
||
| 146 | $this->assertTrue($this->object->setAction('none')); |
||
| 147 | $this->object->actionsAvailable = []; |
||
| 148 | $this->assertFalse($this->object->setAction('none')); |
||
| 149 | $this->object->actionsAvailable = ['copy']; |
||
| 150 | $this->assertFalse($this->object->setAction('none')); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @covers FlexiPeeHP\FlexiBeeRO::getEvidence |
||
| 155 | */ |
||
| 156 | public function testGetEvidence() |
||
| 157 | { |
||
| 158 | $this->assertEquals($this->object->evidence, |
||
| 159 | $this->object->getEvidence()); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @covers FlexiPeeHP\FlexiBeeRO::getResponseEvidence |
||
| 164 | */ |
||
| 165 | public function testGetResponseEvidence() |
||
| 166 | { |
||
| 167 | $this->assertEquals($this->object->getEvidence(), |
||
| 168 | $this->object->getResponseEvidence()); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @covers FlexiPeeHP\FlexiBeeRO::getLastInsertedId |
||
| 173 | * @depends testInsertToFlexiBee |
||
| 174 | */ |
||
| 175 | public function testGetLastInsertedId() |
||
| 176 | { |
||
| 177 | $this->assertNotEmpty($this->object->getLastInsertedId()); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @covers FlexiPeeHP\FlexiBeeRO::xml2array |
||
| 182 | */ |
||
| 183 | public function testXml2array() |
||
| 184 | { |
||
| 185 | $xml = '<card xmlns="http://businesscard.org"> |
||
| 186 | <name>John Doe</name> |
||
| 187 | <title>CEO, Widget Inc.</title> |
||
| 188 | <email>[email protected]</email> |
||
| 189 | <phone>(202) 456-1414</phone> |
||
| 190 | <logo url="widget.gif"/> |
||
| 191 | <a><b>c</b></a> |
||
| 192 | </card>'; |
||
| 193 | |||
| 194 | $data = ['name' => 'John Doe', 'title' => 'CEO, Widget Inc.', 'email' => '[email protected]', |
||
| 195 | 'phone' => '(202) 456-1414', 'logo' => '', 'a' => [['b' => 'c']]]; |
||
| 196 | |||
| 197 | |||
| 198 | $this->assertEquals($data, $this->object->xml2array($xml)); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @covers FlexiPeeHP\FlexiBeeRO::disconnect |
||
| 203 | * |
||
| 204 | * @depends testPerformRequest |
||
| 205 | * @depends testLoadFlexiData |
||
| 206 | * @depends testGetFlexiRow |
||
| 207 | * @depends testGetFlexiData |
||
| 208 | * @depends testLoadFromFlexiBee |
||
| 209 | * @depends testInsertToFlexiBee |
||
| 210 | * @depends testIdExists |
||
| 211 | * @depends testRecordExists |
||
| 212 | * @depends testGetColumnsFromFlexibee |
||
| 213 | * @depends testSearchString |
||
| 214 | */ |
||
| 215 | public function testDisconnect() |
||
| 216 | { |
||
| 217 | $this->object->disconnect(); |
||
| 218 | $this->assertNull($this->object->curl); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @covers FlexiPeeHP\FlexiBeeRO::__destruct |
||
| 223 | * @depends testDisconnect |
||
| 224 | */ |
||
| 225 | public function testdestruct() |
||
| 226 | { |
||
| 227 | $this->markTestSkipped(); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @covers FlexiPeeHP\FlexiBeeRO::getFlexiRow |
||
| 232 | */ |
||
| 233 | public function testGetFlexiRow() |
||
| 234 | { |
||
| 235 | $this->object->getFlexiRow(0); |
||
| 236 | $this->object->getFlexiRow(1); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @covers FlexiPeeHP\FlexiBeeRO::getFlexiData |
||
| 241 | */ |
||
| 242 | public function testGetFlexiData() |
||
| 243 | { |
||
| 244 | $evidence = $this->object->getEvidence(); |
||
| 245 | |||
| 246 | switch ($evidence) { |
||
| 247 | case null: |
||
| 248 | $this->markTestSkipped('Unsupported evidence'); |
||
| 249 | break; |
||
| 250 | case 'c': |
||
| 251 | $this->object->evidence = 'c'; |
||
| 252 | $this->object->prefix = ''; |
||
| 253 | $this->object->company = ''; |
||
| 254 | $this->object->nameSpace = 'companies'; |
||
| 255 | $flexidata = $this->object->getFlexiData(); |
||
| 256 | $this->assertArrayHasKey('company', $flexidata); |
||
| 257 | break; |
||
| 258 | |||
| 259 | default: |
||
| 260 | $flexidata = $this->object->getFlexiData(); |
||
| 261 | View Code Duplication | if (is_array($flexidata) && !count($flexidata)) { |
|
| 262 | $this->markTestSkipped('Empty evidence'); |
||
| 263 | } else { |
||
| 264 | $this->assertArrayHasKey(0, $flexidata); |
||
| 265 | $this->assertArrayHasKey('id', $flexidata[0]); |
||
| 266 | $filtrered = $this->object->getFlexiData(null, |
||
| 267 | "id = ".$flexidata[0]['id']); |
||
| 268 | $this->assertArrayHasKey(0, $filtrered); |
||
| 269 | $this->assertArrayHasKey('id', $filtrered[0]); |
||
| 270 | } |
||
| 271 | break; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @covers FlexiPeeHP\FlexiBeeRO::loadFromFlexiBee |
||
| 277 | */ |
||
| 278 | public function testLoadFromFlexiBee() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @covers FlexiPeeHP\FlexiBeeRO::jsonizeData |
||
| 286 | */ |
||
| 287 | public function testJsonizeData() |
||
| 288 | { |
||
| 289 | $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"}}}', |
||
| 290 | $this->object->jsonizeData(['key' => 'value'])); |
||
| 291 | $this->object->setAction('copy'); |
||
| 292 | $this->assertEquals('{"'.$this->object->nameSpace.'":{"@version":"1.0","'.$this->object->evidence.'":{"key":"value"},"'.$this->object->evidence.'@action":"copy"}}', |
||
| 293 | $this->object->jsonizeData(['key' => 'value'])); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @covers FlexiPeeHP\FlexiBeeRO::idExists |
||
| 298 | */ |
||
| 299 | public function testIdExists() |
||
| 300 | { |
||
| 301 | // Remove the following lines when you implement this test. |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @covers FlexiPeeHP\FlexiBeeRO::recordExists |
||
| 309 | */ |
||
| 310 | public function testRecordExists() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @covers FlexiPeeHP\FlexiBeeRO::getColumnsFromFlexibee |
||
| 340 | */ |
||
| 341 | public function testGetColumnsFromFlexibee() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @covers FlexiPeeHP\FlexiBeeRO::getKod |
||
| 351 | */ |
||
| 352 | public function testGetKod() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @covers FlexiPeeHP\FlexiBeeRO::logResult |
||
| 382 | */ |
||
| 383 | public function testLogResult() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @covers FlexiPeeHP\FlexiBeeRO::flexiUrl |
||
| 407 | */ |
||
| 408 | public function testFlexiUrl() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @covers FlexiPeeHP\FlexiBeeRO::unifyResponseFormat |
||
| 422 | */ |
||
| 423 | public function testunifyResponseFormat() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @covers FlexiPeeHP\FlexiBeeRO::__toString |
||
| 464 | */ |
||
| 465 | public function testtoString() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @covers FlexiPeeHP\FlexiBeeRO::draw |
||
| 482 | */ |
||
| 483 | public function testDraw($whatWant = NULL) |
||
| 488 | |||
| 489 | } |
||
| 490 |
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: