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 |
||
14 | class BlockRepository |
||
15 | { |
||
16 | /** |
||
17 | * Database table name that this repository maintains. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | const TABLE = 'page_block'; |
||
22 | |||
23 | /** |
||
24 | * @var Connection |
||
25 | */ |
||
26 | private $connection; |
||
27 | |||
28 | /** |
||
29 | * BlockRepository constructor. |
||
30 | * |
||
31 | * @param Connection $connection |
||
32 | */ |
||
33 | public function __construct(Connection $connection) |
||
37 | |||
38 | /** |
||
39 | * Fetches all blocks of a page. |
||
40 | * |
||
41 | * @param array $where |
||
42 | * |
||
43 | * @return BlockEntity[] |
||
44 | */ |
||
45 | public function fetchBy(array $where = []) |
||
73 | |||
74 | /** |
||
75 | * Creates a block in the database. |
||
76 | * |
||
77 | * @param BlockEntity $entity |
||
78 | * |
||
79 | * @return BlockEntity |
||
80 | * |
||
81 | * @throws RecordAlreadyExistsException |
||
82 | */ |
||
83 | public function create(BlockEntity $entity) |
||
98 | |||
99 | /** |
||
100 | * Update a page in the database. |
||
101 | * |
||
102 | * @param BlockEntity $entity |
||
103 | * |
||
104 | * @return BlockEntity |
||
105 | * |
||
106 | * @throws RecordNotPersistedException |
||
107 | */ |
||
108 | View Code Duplication | public function update(BlockEntity $entity) |
|
126 | |||
127 | /** |
||
128 | * Removes a page from the database. |
||
129 | * |
||
130 | * @param BlockEntity $entity |
||
131 | * |
||
132 | * @return BlockEntity |
||
133 | * |
||
134 | * @throws RecordNotPersistedException |
||
135 | */ |
||
136 | View Code Duplication | public function remove(BlockEntity $entity) |
|
154 | |||
155 | /** |
||
156 | * Maps the given entity to the database array. |
||
157 | * |
||
158 | * @param BlockEntity $entity |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | View Code Duplication | public function getDatabaseArrayFromEntity(BlockEntity $entity) |
|
174 | |||
175 | /** |
||
176 | * Prepares database array from properties. |
||
177 | * |
||
178 | * @param array $data |
||
179 | * |
||
180 | * @return BlockEntity |
||
181 | */ |
||
182 | View Code Duplication | public function getEntityFromDatabaseArray(array $data) |
|
195 | } |
||
196 |
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.