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 |
||
10 | class TAssociationEndTypeTest extends TestCase |
||
11 | { |
||
12 | public function testBadMultiplicity() |
||
13 | { |
||
14 | $expected = "Multiplicity must be a valid TMultiplicity"; |
||
15 | $actual = null; |
||
16 | |||
17 | $foo = new TAssociationEndType(); |
||
18 | $mult = 'abc'; |
||
19 | |||
20 | try { |
||
21 | $foo->setMultiplicity($mult); |
||
22 | } catch (\InvalidArgumentException $e) { |
||
23 | $actual = $e->getMessage(); |
||
24 | } |
||
25 | $this->assertEquals($expected, $actual); |
||
26 | } |
||
27 | |||
28 | public function testAddToOnDeleteBadData() |
||
44 | |||
45 | View Code Duplication | public function testAddToOnDeleteGoodData() |
|
46 | { |
||
47 | $foo = new TAssociationEndType(); |
||
48 | $onDelete = m::mock(TOnActionType::class)->makePartial(); |
||
49 | $onDelete->shouldReceive('isOK')->andReturn(true)->once(); |
||
50 | |||
51 | $foo->addToOnDelete($onDelete); |
||
1 ignored issue
–
show
|
|||
52 | $this->assertTrue($foo->issetOnDelete(0)); |
||
53 | $this->assertFalse($foo->issetOnDelete(1)); |
||
54 | |||
55 | $foo->unsetOnDelete(0); |
||
56 | $this->assertFalse($foo->issetOnDelete(0)); |
||
57 | $this->assertFalse($foo->issetOnDelete(1)); |
||
58 | } |
||
59 | |||
60 | View Code Duplication | public function testSetOnDeleteBadItem() |
|
77 | |||
78 | View Code Duplication | public function testIsTOperationsOKBadData() |
|
93 | } |
||
94 |
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: