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 |
||
17 | class ObjectPersister implements ObjectPersisterInterface |
||
18 | { |
||
19 | protected $type; |
||
20 | protected $transformer; |
||
21 | protected $objectClass; |
||
22 | protected $fields; |
||
23 | protected $logger; |
||
24 | |||
25 | 13 | public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) |
|
32 | |||
33 | /** |
||
34 | * If the ObjectPersister handles a given object. |
||
35 | * |
||
36 | * @param object $object |
||
37 | * |
||
38 | * @return bool |
||
39 | */ |
||
40 | public function handlesObject($object) |
||
44 | |||
45 | /** |
||
46 | * @param LoggerInterface $logger |
||
47 | */ |
||
48 | public function setLogger(LoggerInterface $logger) |
||
52 | |||
53 | /** |
||
54 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
55 | * |
||
56 | * @param BulkException $e |
||
57 | * |
||
58 | * @throws BulkException |
||
59 | */ |
||
60 | private function log(BulkException $e) |
||
68 | |||
69 | /** |
||
70 | * Insert one object into the type |
||
71 | * The object will be transformed to an elastica document. |
||
72 | * |
||
73 | * @param object $object |
||
74 | */ |
||
75 | 4 | public function insertOne($object) |
|
79 | |||
80 | /** |
||
81 | * Replaces one object in the type. |
||
82 | * |
||
83 | * @param object $object |
||
84 | **/ |
||
85 | 4 | public function replaceOne($object) |
|
89 | |||
90 | /** |
||
91 | * Deletes one object in the type. |
||
92 | * |
||
93 | * @param object $object |
||
94 | **/ |
||
95 | 3 | public function deleteOne($object) |
|
99 | |||
100 | /** |
||
101 | * Deletes one object in the type by id. |
||
102 | * |
||
103 | * @param mixed $id |
||
104 | **/ |
||
105 | public function deleteById($id) |
||
109 | |||
110 | /** |
||
111 | * Bulk insert an array of objects in the type for the given method. |
||
112 | * |
||
113 | * @param array $objects array of domain model objects |
||
114 | * @param string Method to call |
||
115 | */ |
||
116 | 7 | View Code Duplication | public function insertMany(array $objects) |
128 | |||
129 | /** |
||
130 | * Bulk update an array of objects in the type. Create document if it does not already exist. |
||
131 | * |
||
132 | * @param array $objects array of domain model objects |
||
133 | */ |
||
134 | 4 | public function replaceMany(array $objects) |
|
149 | |||
150 | /** |
||
151 | * Bulk deletes an array of objects in the type. |
||
152 | * |
||
153 | * @param array $objects array of domain model objects |
||
154 | */ |
||
155 | 3 | View Code Duplication | public function deleteMany(array $objects) |
167 | |||
168 | /** |
||
169 | * Bulk deletes records from an array of identifiers. |
||
170 | * |
||
171 | * @param array $identifiers array of domain model object identifiers |
||
172 | */ |
||
173 | public function deleteManyByIdentifiers(array $identifiers) |
||
181 | |||
182 | /** |
||
183 | * Transforms an object to an elastica document. |
||
184 | * |
||
185 | * @param object $object |
||
186 | * |
||
187 | * @return Document the elastica document |
||
188 | */ |
||
189 | 4 | public function transformToElasticaDocument($object) |
|
193 | } |
||
194 |
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.