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 |
||
| 12 | class ReferenceLoadingTest extends AbstractEntityManagerTest |
||
| 13 | { |
||
| 14 | public function testAnyToOneLoading() |
||
| 54 | |||
| 55 | public function testOneToManyLoading() |
||
| 109 | |||
| 110 | public function testFindByReference() |
||
| 155 | |||
| 156 | public function testSubEntityRelations() |
||
| 157 | { |
||
| 158 | $repository = $this->getManager()->getRepository(SubEntity::class); |
||
| 159 | |||
| 160 | $this->getClient('test-client')->push( |
||
| 161 | $this->getResponseMock( |
||
| 162 | true, |
||
| 163 | (object)[ |
||
| 164 | 'id' => '1', |
||
| 165 | 'payload' => 'test-payload', |
||
| 166 | 'references' => ['5', '7'], |
||
| 167 | 'sub-payload' => 'sub-payload', |
||
| 168 | ] |
||
| 169 | ), |
||
| 170 | function (RpcRequestInterface $request) { |
||
| 171 | self::assertEquals('test-entity/find', $request->getMethod()); |
||
| 172 | self::assertEquals(['id' => 1], $request->getParameters()); |
||
| 173 | |||
| 174 | return true; |
||
| 175 | } |
||
| 176 | ); |
||
| 177 | |||
| 178 | $this->getClient('test-reference-client')->push( |
||
| 179 | $this->getResponseMock( |
||
| 180 | true, |
||
| 181 | [ |
||
| 182 | (object)[ |
||
| 183 | 'id' => '5', |
||
| 184 | 'reference-payload' => 'test-payload-5', |
||
| 185 | 'owner' => '1', |
||
| 186 | ], |
||
| 187 | (object)[ |
||
| 188 | 'id' => '7', |
||
| 189 | 'reference-payload' => 'test-payload-7', |
||
| 190 | 'owner' => '1', |
||
| 191 | ], |
||
| 192 | ] |
||
| 193 | ), |
||
| 194 | View Code Duplication | function (RpcRequestInterface $request) { |
|
| 195 | self::assertEquals('test-reference/search', $request->getMethod()); |
||
| 196 | self::assertEquals( |
||
| 197 | [ |
||
| 198 | 'criteria' => ['owner' => 1], |
||
| 199 | 'order' => [], |
||
| 200 | 'limit' => null, |
||
| 201 | 'offset' => null, |
||
| 202 | ], |
||
| 203 | $request->getParameters() |
||
| 204 | ); |
||
| 205 | |||
| 206 | return true; |
||
| 207 | } |
||
| 208 | ); |
||
| 209 | |||
| 210 | /** @var SubEntity $entity */ |
||
| 211 | $entity = $repository->find(1); |
||
| 212 | self::assertSame(1, $entity->getId()); |
||
| 213 | self::assertSame('test-payload', $entity->getPayload()); |
||
| 214 | self::assertSame('sub-payload', $entity->getSubPayload()); |
||
| 215 | self::assertNull($entity->getStringPayload()); |
||
| 216 | /** @var TestReference[] $references */ |
||
| 217 | $references = $entity->getReferences()->toArray(); |
||
| 218 | |||
| 219 | self::assertCount(2, $references); |
||
| 220 | foreach ($references as $reference) { |
||
| 221 | self::assertSame('test-payload-' . $reference->getId(), $reference->getReferencePayload()); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | protected function getClientNames() |
||
| 229 | } |
||
| 230 |
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: