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 EntityFactoryTest extends AbstractEntityManagerTest |
||
11 | { |
||
12 | public function testEntityLoading() |
||
40 | |||
41 | View Code Duplication | public function testInheritanceLoading() |
|
69 | |||
70 | public function testCompositeKeyLoading() |
||
71 | { |
||
72 | $repository = $this->getManager()->getRepository(CompositeKeyEntity::class); |
||
73 | $this->getClient()->push( |
||
74 | $this->getResponseMock( |
||
75 | true, |
||
76 | (object)[ |
||
77 | 'first_key' => 2, |
||
78 | 'second_key' => 'test', |
||
79 | 'payload' => 'test-payload', |
||
80 | ] |
||
81 | ), |
||
82 | function (RpcRequestInterface $request) { |
||
83 | self::assertEquals('composite-key-entity/find', $request->getMethod()); |
||
84 | self::assertEquals(['first_key' => 2, 'second_key' => 'test'], $request->getParameters()); |
||
85 | |||
86 | return true; |
||
87 | } |
||
88 | ); |
||
89 | |||
90 | /** @var CompositeKeyEntity $entity */ |
||
91 | $entity = $repository->find(['firstKey' => '2', 'secondKey' => 'test']); |
||
92 | |||
93 | self::assertInstanceOf(CompositeKeyEntity::class, $entity); |
||
94 | self::assertEquals(2, $entity->getFirstKey()); |
||
95 | self::assertEquals('test', $entity->getSecondKey()); |
||
96 | self::assertEquals('test-payload', $entity->getPayload()); |
||
97 | } |
||
98 | |||
99 | protected function getClientNames() |
||
103 | } |
||
104 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.