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 CollectionLoadingTest extends AbstractEntityManagerTest |
||
| 11 | { |
||
| 12 | 2 | protected function getClientNames() |
|
| 13 | { |
||
| 14 | 2 | return [self::DEFAULT_CLIENT, 'test-reference-client']; |
|
| 15 | } |
||
| 16 | |||
| 17 | 1 | public function testLazyCollections() |
|
| 18 | { |
||
| 19 | 1 | $repository = $this->getManager()->getRepository(SubEntity::class); |
|
| 20 | /** @var SubEntity[]|ArrayCollection|ApiCollection $entities */ |
||
| 21 | 1 | $entities = $repository->findBy(['subPayload' => 'sub-payload']); |
|
| 22 | |||
| 23 | 1 | self::assertInstanceOf(ApiCollection::class, $entities); |
|
| 24 | 1 | self::assertFalse($entities->isInitialized()); |
|
| 25 | |||
| 26 | try { |
||
| 27 | 1 | $entities->count(); |
|
| 28 | 1 | } catch (\OutOfBoundsException $exception) { |
|
| 29 | 1 | self::assertEquals('Mock queue is empty', $exception->getMessage()); |
|
| 30 | } |
||
| 31 | 1 | } |
|
| 32 | |||
| 33 | 2 | public function testFindBy() |
|
| 34 | { |
||
| 35 | 1 | $repository = $this->getManager()->getRepository(SubEntity::class); |
|
| 36 | |||
| 37 | 1 | $this->getResponseMock()->append( |
|
| 38 | 1 | new Response( |
|
| 39 | 1 | 200, |
|
| 40 | 1 | [], |
|
| 41 | 1 | json_encode( |
|
| 42 | [ |
||
| 43 | 1 | 'jsonrpc' => '2.0', |
|
| 44 | 1 | 'id' => 'test', |
|
| 45 | 'result' => [ |
||
| 46 | [ |
||
| 47 | 1 | 'id' => '1', |
|
| 48 | 1 | 'payload' => 'test-payload-1', |
|
| 49 | 1 | 'sub-payload' => 'sub-payload', |
|
| 50 | 1 | 'string-payload' => null, |
|
| 51 | 1 | ], |
|
| 52 | [ |
||
| 53 | 1 | 'id' => '2', |
|
| 54 | 1 | 'payload' => 'test-payload-2', |
|
| 55 | 1 | 'sub-payload' => 'sub-payload', |
|
| 56 | 1 | 'string-payload' => 123456, |
|
| 57 | 1 | ], |
|
| 58 | [ |
||
| 59 | 1 | 'id' => '3', |
|
| 60 | 1 | 'payload' => 'test-payload-3', |
|
| 61 | 1 | 'sub-payload' => 'sub-payload', |
|
| 62 | 1 | 'string-payload' => 'sub-payload', |
|
| 63 | 1 | ], |
|
| 64 | 1 | ], |
|
| 65 | ] |
||
| 66 | 1 | ) |
|
| 67 | 1 | ) |
|
| 68 | 1 | ); |
|
| 69 | |||
| 70 | /** @var SubEntity[]|ArrayCollection $entities */ |
||
| 71 | 1 | $entities = $repository->findBy(['subPayload' => 'sub-payload']); |
|
| 72 | 1 | self::assertInstanceOf(\Countable::class, $entities); |
|
| 73 | 1 | self::assertCount(3, $entities); |
|
| 74 | |||
| 75 | 1 | foreach ($entities as $entity) { |
|
| 76 | 1 | self::assertInternalType('int', $entity->getId()); |
|
| 77 | 1 | self::assertInstanceOf(SubEntity::class, $entity); |
|
| 78 | 1 | self::assertEquals('test-payload-' . $entity->getId(), $entity->getPayload()); |
|
| 79 | 2 | self::assertEquals('sub-payload', $entity->getSubPayload()); |
|
| 80 | |||
| 81 | 1 | if (null !== $entity->getStringPayload()) { |
|
| 82 | 1 | self::assertInternalType('string', $entity->getStringPayload()); |
|
| 83 | 1 | } |
|
| 84 | 1 | } |
|
| 85 | 1 | } |
|
| 86 | } |
||
| 87 |