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 | private $options; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Type $type |
||
| 37 | * @param ModelToElasticaTransformerInterface $transformer |
||
| 38 | * @param string $objectClass |
||
| 39 | * @param array $fields |
||
| 40 | */ |
||
| 41 | 19 | public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields, array $options = []) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function handlesObject($object) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param LoggerInterface $logger |
||
| 60 | */ |
||
| 61 | public function setLogger(LoggerInterface $logger) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 7 | public function insertOne($object) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 5 | public function replaceOne($object) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 3 | public function deleteOne($object) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function deleteById($id, $routing = false) |
||
| 94 | { |
||
| 95 | $this->deleteManyByIdentifiers([$id]); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 10 | View Code Duplication | public function insertMany(array $objects) |
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | 5 | public function replaceMany(array $objects) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 3 | View Code Duplication | public function deleteMany(array $objects) |
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function deleteManyByIdentifiers(array $identifiers, $routing = false) |
||
| 153 | { |
||
| 154 | try { |
||
| 155 | $this->type->deleteIds($identifiers, $routing); |
||
| 156 | } catch (BulkException $e) { |
||
| 157 | $this->log($e); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Transforms an object to an elastica document. |
||
| 163 | * |
||
| 164 | * @param object $object |
||
| 165 | * |
||
| 166 | * @return Document the elastica document |
||
| 167 | */ |
||
| 168 | 6 | public function transformToElasticaDocument($object) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw. |
||
| 175 | * |
||
| 176 | * @param BulkException $e |
||
| 177 | * |
||
| 178 | * @throws BulkException |
||
| 179 | */ |
||
| 180 | private function log(BulkException $e) |
||
| 188 | } |
||
| 189 |