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 GetByRefTest extends \PHPUnit_Framework_TestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @expectedException Exception |
||
| 14 | * @expectedExceptionMessage Please provide a documentReference object with a type and id |
||
| 15 | */ |
||
| 16 | public function invalid() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @test |
||
| 23 | * @throws Exception |
||
| 24 | */ |
||
| 25 | View Code Duplication | public function singlePathToAttribute() |
|
| 26 | { |
||
| 27 | $result = $this->getMockConnector()->getByRef([ |
||
| 28 | 'rootDocumentEntityType' => 'Test', |
||
| 29 | 'rootDocumentId' => 'X', |
||
| 30 | 'path' => [ |
||
| 31 | [ |
||
| 32 | 'field' => 'singlePathToAttribute' |
||
| 33 | ] |
||
| 34 | ] |
||
| 35 | ]); |
||
| 36 | |||
| 37 | $this->assertSame('value', $result); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @test |
||
| 42 | * @throws Exception |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function multiplePathToAttribute() |
|
| 45 | { |
||
| 46 | $result = $this->getMockConnector()->getByRef([ |
||
| 47 | 'rootDocumentEntityType' => 'Test', |
||
| 48 | 'rootDocumentId' => 'X', |
||
| 49 | 'path' => [ |
||
| 50 | [ |
||
| 51 | 'field' => 'multiplePathToAttribute' |
||
| 52 | ], |
||
| 53 | [ |
||
| 54 | 'field' => 'field' |
||
| 55 | ] |
||
| 56 | ] |
||
| 57 | ]); |
||
| 58 | |||
| 59 | $this->assertSame('value', $result); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @test |
||
| 64 | * @throws Exception |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function multiplePathToAttributeDepth() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @test |
||
| 89 | * @throws Exception |
||
| 90 | */ |
||
| 91 | public function singlePathToArrayObject() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @test |
||
| 109 | * @throws Exception |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function multiplePathToArrayObject() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @return \PHPUnit_Framework_MockObject_MockObject|Connector |
||
| 132 | */ |
||
| 133 | protected function getMockConnector() |
||
| 177 | } |
||
| 178 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: