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 |
||
19 | class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer |
||
20 | { |
||
21 | /** |
||
22 | * Propel model class to map to Elastica documents. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $objectClass = null; |
||
27 | |||
28 | /** |
||
29 | * Transformer options. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $options = array( |
||
34 | 'hydrate' => true, |
||
35 | 'identifier' => 'id', |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * Constructor. |
||
40 | * |
||
41 | * @param string $objectClass |
||
42 | * @param array $options |
||
43 | */ |
||
44 | public function __construct($objectClass, array $options = array()) |
||
49 | |||
50 | /** |
||
51 | * Transforms an array of Elastica document into an array of Propel entities |
||
52 | * fetched from the database. |
||
53 | * |
||
54 | * @param array $elasticaObjects |
||
55 | * |
||
56 | * @return array|\ArrayObject |
||
57 | */ |
||
58 | public function transform(array $elasticaObjects) |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | View Code Duplication | public function hybridTransform(array $elasticaObjects) |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function getObjectClass() |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getIdentifierField() |
||
122 | |||
123 | /** |
||
124 | * Fetch Propel entities for the given identifier values. |
||
125 | * |
||
126 | * If $hydrate is false, the returned array elements will be arrays. |
||
127 | * Otherwise, the results will be hydrated to instances of the model class. |
||
128 | * |
||
129 | * @param array $identifierValues Identifier values |
||
130 | * @param boolean $hydrate Whether or not to hydrate the results |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | protected function findByIdentifiers(array $identifierValues, $hydrate) |
||
148 | |||
149 | /** |
||
150 | * Create a query to use in the findByIdentifiers() method. |
||
151 | * |
||
152 | * @param string $class Propel model class |
||
153 | * @param string $identifierField Identifier field name (e.g. "id") |
||
154 | * @param array $identifierValues Identifier values |
||
155 | * |
||
156 | * @return \ModelCriteria |
||
157 | */ |
||
158 | protected function createQuery($class, $identifierField, array $identifierValues) |
||
165 | |||
166 | /** |
||
167 | * @see https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Util/Inflector.php |
||
168 | * |
||
169 | * @param string $str |
||
170 | */ |
||
171 | private function camelize($str) |
||
175 | } |
||
176 |
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.