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 | /** |
||
26 | * @param Type $type |
||
27 | * @param ModelToElasticaTransformerInterface $transformer |
||
28 | * @param string $objectClass |
||
29 | * @param array $fields |
||
30 | */ |
||
31 | 21 | public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function handlesObject($object) |
||
43 | { |
||
44 | return $object instanceof $this->objectClass; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param LoggerInterface $logger |
||
49 | */ |
||
50 | public function setLogger(LoggerInterface $logger) |
||
51 | { |
||
52 | $this->logger = $logger; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
57 | * |
||
58 | * @param BulkException $e |
||
59 | * |
||
60 | * @throws BulkException |
||
61 | */ |
||
62 | private function log(BulkException $e) |
||
63 | { |
||
64 | if (! $this->logger) { |
||
65 | throw $e; |
||
66 | } |
||
67 | |||
68 | $this->logger->error($e); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 7 | public function insertOne($object) |
|
75 | { |
||
76 | 7 | $this->insertMany(array($object)); |
|
77 | 6 | } |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 5 | public function replaceOne($object) |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 3 | public function deleteOne($object) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function deleteById($id) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 10 | View Code Duplication | public function insertMany(array $objects) |
|
|||
107 | { |
||
108 | 10 | $documents = array(); |
|
109 | 10 | foreach ($objects as $object) { |
|
110 | 10 | $documents[] = $this->transformToElasticaDocument($object); |
|
111 | 8 | } |
|
112 | try { |
||
113 | 8 | $this->type->addDocuments($documents); |
|
114 | 8 | } catch (BulkException $e) { |
|
115 | $this->log($e); |
||
116 | } |
||
117 | 8 | } |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 5 | public function replaceMany(array $objects) |
|
123 | { |
||
124 | 5 | $documents = array(); |
|
125 | 5 | foreach ($objects as $object) { |
|
126 | 5 | $document = $this->transformToElasticaDocument($object); |
|
127 | 4 | $document->setDocAsUpsert(true); |
|
128 | 4 | $documents[] = $document; |
|
129 | 4 | } |
|
130 | |||
131 | try { |
||
132 | 4 | $this->type->updateDocuments($documents); |
|
133 | 4 | } catch (BulkException $e) { |
|
134 | $this->log($e); |
||
135 | } |
||
136 | 4 | } |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 3 | View Code Duplication | public function deleteMany(array $objects) |
142 | { |
||
143 | 3 | $documents = array(); |
|
144 | 3 | foreach ($objects as $object) { |
|
145 | 3 | $documents[] = $this->transformToElasticaDocument($object); |
|
146 | 2 | } |
|
147 | try { |
||
148 | 2 | $this->type->deleteDocuments($documents); |
|
149 | 2 | } catch (BulkException $e) { |
|
150 | $this->log($e); |
||
151 | } |
||
152 | 2 | } |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function deleteManyByIdentifiers(array $identifiers) |
||
165 | |||
166 | /** |
||
167 | * Transforms an object to an elastica document. |
||
168 | * |
||
169 | * @param object $object |
||
170 | * |
||
171 | * @return Document the elastica document |
||
172 | */ |
||
173 | 6 | public function transformToElasticaDocument($object) |
|
177 | } |
||
178 |
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.