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 |
||
15 | class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var EventDispatcherInterface |
||
19 | */ |
||
20 | protected $dispatcher; |
||
21 | |||
22 | /** |
||
23 | * Optional parameters. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $options = array( |
||
28 | 'identifier' => 'id', |
||
29 | ); |
||
30 | |||
31 | /** |
||
32 | * PropertyAccessor instance. |
||
33 | * |
||
34 | * @var PropertyAccessorInterface |
||
35 | */ |
||
36 | protected $propertyAccessor; |
||
37 | |||
38 | /** |
||
39 | * Instanciates a new Mapper. |
||
40 | * |
||
41 | * @param array $options |
||
42 | * @param EventDispatcherInterface $dispatcher |
||
43 | */ |
||
44 | 41 | public function __construct(array $options = array(), EventDispatcherInterface $dispatcher = null) |
|
45 | { |
||
46 | 41 | $this->options = array_merge($this->options, $options); |
|
47 | 41 | $this->dispatcher = $dispatcher; |
|
48 | 41 | } |
|
49 | |||
50 | /** |
||
51 | * Set the PropertyAccessor. |
||
52 | * |
||
53 | * @param PropertyAccessorInterface $propertyAccessor |
||
54 | */ |
||
55 | 41 | public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) |
|
59 | |||
60 | /** |
||
61 | * Transforms an object into an elastica object having the required keys. |
||
62 | * |
||
63 | * @param object $object the object to convert |
||
64 | * @param array $fields the keys we want to have in the returned array |
||
65 | * |
||
66 | * @return Document |
||
67 | **/ |
||
68 | 26 | public function transform($object, array $fields) |
|
75 | |||
76 | /** |
||
77 | * transform a nested document or an object property into an array of ElasticaDocument. |
||
78 | * |
||
79 | * @param array|\Traversable|\ArrayAccess $objects the object to convert |
||
80 | * @param array $fields the keys we want to have in the returned array |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 4 | protected function transformNested($objects, array $fields) |
|
102 | |||
103 | /** |
||
104 | * Attempts to convert any type to a string or an array of strings. |
||
105 | * |
||
106 | * @param mixed $value |
||
107 | * |
||
108 | * @return string|array |
||
109 | */ |
||
110 | protected function normalizeValue($value) |
||
129 | |||
130 | /** |
||
131 | * Transforms the given object to an elastica document |
||
132 | * |
||
133 | * @param object $object the object to convert |
||
134 | * @param array $fields the keys we want to have in the returned array |
||
135 | * @param string $identifier the identifier for the new document |
||
136 | * @return Document |
||
137 | */ |
||
138 | 26 | protected function transformObjectToDocument($object, array $fields, $identifier = '') |
|
201 | } |
||
202 |
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.