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 |
||
24 | class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var EventDispatcherInterface |
||
28 | */ |
||
29 | protected $dispatcher; |
||
30 | |||
31 | /** |
||
32 | * Optional parameters. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $options = [ |
||
37 | 'identifier' => 'id', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * PropertyAccessor instance. |
||
42 | * |
||
43 | * @var PropertyAccessorInterface |
||
44 | */ |
||
45 | protected $propertyAccessor; |
||
46 | |||
47 | /** |
||
48 | * Instanciates a new Mapper. |
||
49 | * |
||
50 | * @param array $options |
||
51 | * @param EventDispatcherInterface $dispatcher |
||
52 | */ |
||
53 | public function __construct(array $options = [], EventDispatcherInterface $dispatcher = null) |
||
58 | |||
59 | /** |
||
60 | * Set the PropertyAccessor. |
||
61 | * |
||
62 | * @param PropertyAccessorInterface $propertyAccessor |
||
63 | */ |
||
64 | public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) |
||
68 | |||
69 | /** |
||
70 | * Transforms an object into an elastica object having the required keys. |
||
71 | * |
||
72 | * @param object $object the object to convert |
||
73 | * @param array $fields the keys we want to have in the returned array |
||
74 | * |
||
75 | * @return Document |
||
76 | **/ |
||
77 | public function transform($object, array $fields) |
||
84 | |||
85 | /** |
||
86 | * transform a nested document or an object property into an array of ElasticaDocument. |
||
87 | * |
||
88 | * @param array|\Traversable|\ArrayAccess $objects the object to convert |
||
89 | * @param array $fields the keys we want to have in the returned array |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function transformNested($objects, array $fields) |
||
111 | |||
112 | /** |
||
113 | * Attempts to convert any type to a string or an array of strings. |
||
114 | * |
||
115 | * @param mixed $value |
||
116 | * |
||
117 | * @return string|array |
||
118 | */ |
||
119 | protected function normalizeValue($value) |
||
138 | |||
139 | /** |
||
140 | * Transforms the given object to an elastica document. |
||
141 | * |
||
142 | * @param object $object the object to convert |
||
143 | * @param array $fields the keys we want to have in the returned array |
||
144 | * @param string $identifier the identifier for the new document |
||
145 | * |
||
146 | * @return Document |
||
147 | */ |
||
148 | protected function transformObjectToDocument($object, array $fields, $identifier = '') |
||
211 | } |
||
212 |