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 |
||
18 | abstract class Repository implements RepositoryInterface { |
||
19 | |||
20 | use EntityManagerConsumerTrait; |
||
21 | |||
22 | /** |
||
23 | * The repository of entities. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $repository; |
||
28 | |||
29 | /** |
||
30 | * The entity type for this repository. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $entityType; |
||
35 | |||
36 | /** |
||
37 | * The sports db client. |
||
38 | * |
||
39 | * @var \TheSportsDb\Http\TheSportsDbClientInterface |
||
40 | */ |
||
41 | protected $sportsDbClient; |
||
42 | |||
43 | public function __construct(TheSportsDbClientInterface $sportsDbClient, EntityManagerInterface $entityManager = NULL) { |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 1 | View Code Duplication | public function byId($id) { |
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 1 | public function clear($id) { |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 1 | public function getEntityTypeName() { |
|
78 | |||
79 | /** |
||
80 | * Gets the entity from cache or loads it & updates it with given data. |
||
81 | * |
||
82 | * @param \stdClass $data |
||
83 | * The data of the entity. |
||
84 | * |
||
85 | * @return \TheSportsDb\Entity\EntityInterface |
||
86 | * The matched entity. |
||
87 | */ |
||
88 | 24 | protected function normalizeEntity(\stdClass $data) { |
|
99 | |||
100 | /** |
||
101 | * Gets an array of entities based on given data. |
||
102 | * |
||
103 | * @see \TheSportsDb\Entity\Repository\Repository::normilizeEntity() |
||
104 | * |
||
105 | * @param \stdClass[]|null $data |
||
106 | * The data of the entity. |
||
107 | * |
||
108 | * @return \TheSportsDb\Entity\EntityInterface[] |
||
109 | * The matched entities. |
||
110 | */ |
||
111 | 24 | protected function normalizeArray($data) { |
|
121 | } |
||
122 |
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.