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 |
||
26 | class ObjectPersister implements ObjectPersisterInterface |
||
27 | { |
||
28 | protected $type; |
||
29 | protected $transformer; |
||
30 | protected $objectClass; |
||
31 | protected $fields; |
||
32 | protected $logger; |
||
33 | |||
34 | /** |
||
35 | * @param Type $type |
||
36 | * @param ModelToElasticaTransformerInterface $transformer |
||
37 | * @param string $objectClass |
||
38 | * @param array $fields |
||
39 | */ |
||
40 | 21 | public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function handlesObject($object) |
||
55 | |||
56 | /** |
||
57 | * @param LoggerInterface $logger |
||
58 | */ |
||
59 | public function setLogger(LoggerInterface $logger) |
||
63 | |||
64 | /** |
||
65 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
66 | * |
||
67 | * @param BulkException $e |
||
68 | * |
||
69 | * @throws BulkException |
||
70 | */ |
||
71 | private function log(BulkException $e) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 7 | public function insertOne($object) |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 5 | public function replaceOne($object) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 3 | public function deleteOne($object) |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function deleteById($id) |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 10 | View Code Duplication | public function insertMany(array $objects) |
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 5 | public function replaceMany(array $objects) |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 3 | View Code Duplication | public function deleteMany(array $objects) |
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function deleteManyByIdentifiers(array $identifiers) |
||
174 | |||
175 | /** |
||
176 | * Transforms an object to an elastica document. |
||
177 | * |
||
178 | * @param object $object |
||
179 | * |
||
180 | * @return Document the elastica document |
||
181 | */ |
||
182 | 6 | public function transformToElasticaDocument($object) |
|
186 | } |
||
187 |