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 |
||
20 | abstract class AbstractHydrator |
||
21 | { |
||
22 | /** |
||
23 | * The ResultSetMapping. |
||
24 | * |
||
25 | * @var \Doctrine\ORM\Query\ResultSetMapping |
||
26 | */ |
||
27 | protected $rsm; |
||
28 | |||
29 | /** |
||
30 | * The EntityManager instance. |
||
31 | * |
||
32 | * @var EntityManagerInterface |
||
33 | */ |
||
34 | protected $em; |
||
35 | |||
36 | /** |
||
37 | * The dbms Platform instance. |
||
38 | * |
||
39 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
||
40 | */ |
||
41 | protected $platform; |
||
42 | |||
43 | /** |
||
44 | * The UnitOfWork of the associated EntityManager. |
||
45 | * |
||
46 | * @var \Doctrine\ORM\UnitOfWork |
||
47 | */ |
||
48 | protected $uow; |
||
49 | |||
50 | /** |
||
51 | * Local ClassMetadata cache to avoid going to the EntityManager all the time. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $metadataCache = []; |
||
56 | |||
57 | /** |
||
58 | * The cache used during row-by-row hydration. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $cache = []; |
||
63 | |||
64 | /** |
||
65 | * The statement that provides the data to hydrate. |
||
66 | * |
||
67 | * @var \Doctrine\DBAL\Driver\Statement |
||
68 | */ |
||
69 | protected $stmt; |
||
70 | |||
71 | /** |
||
72 | * The query hints. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $hints; |
||
77 | |||
78 | /** |
||
79 | * Initializes a new instance of a class derived from <tt>AbstractHydrator</tt>. |
||
80 | * |
||
81 | * @param EntityManagerInterface $em The EntityManager to use. |
||
82 | */ |
||
83 | public function __construct(EntityManagerInterface $em) |
||
89 | |||
90 | /** |
||
91 | * Initiates a row-by-row hydration. |
||
92 | * |
||
93 | * @param object $stmt |
||
94 | * @param object $resultSetMapping |
||
95 | * @param array $hints |
||
96 | * |
||
97 | * @return IterableResult |
||
98 | */ |
||
99 | View Code Duplication | public function iterate($stmt, $resultSetMapping, array $hints = []) |
|
113 | |||
114 | /** |
||
115 | * Hydrates all rows returned by the passed statement instance at once. |
||
116 | 11 | * |
|
117 | * @param object $stmt |
||
118 | 11 | * @param object $resultSetMapping |
|
119 | 11 | * @param array $hints |
|
120 | 11 | * |
|
121 | * @return array |
||
122 | 11 | */ |
|
123 | View Code Duplication | public function hydrateAll($stmt, $resultSetMapping, array $hints = []) |
|
139 | |||
140 | 960 | /** |
|
141 | * Hydrates a single row returned by the current statement instance during |
||
142 | 960 | * row-by-row hydration with {@link iterate()}. |
|
143 | 960 | * |
|
144 | 960 | * @return mixed |
|
145 | */ |
||
146 | 960 | View Code Duplication | public function hydrateRow() |
162 | |||
163 | 10 | /** |
|
164 | * When executed in a hydrate() loop we have to clear internal state to |
||
165 | 10 | * decrease memory consumption. |
|
166 | 7 | * |
|
167 | * @param mixed $eventArgs |
||
168 | 7 | * |
|
169 | * @return void |
||
170 | */ |
||
171 | 10 | public function onClear($eventArgs) |
|
174 | |||
175 | 10 | /** |
|
176 | * Executes one-time preparation tasks, once each time hydration is started |
||
177 | * through {@link hydrateAll} or {@link iterate()}. |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | protected function prepare() |
||
184 | |||
185 | /** |
||
186 | 6 | * Executes one-time cleanup tasks at the end of a hydration that was initiated |
|
187 | * through {@link hydrateAll} or {@link iterate()}. |
||
188 | 6 | * |
|
189 | * @return void |
||
190 | */ |
||
191 | protected function cleanup() |
||
204 | |||
205 | /** |
||
206 | 956 | * Hydrates a single row from the current statement instance. |
|
207 | * |
||
208 | 956 | * Template method. |
|
209 | * |
||
210 | 956 | * @param array $data The row data. |
|
211 | 956 | * @param array $result The result to fill. |
|
212 | 956 | * |
|
213 | 956 | * @return void |
|
214 | 956 | * |
|
215 | * @throws HydrationException |
||
216 | */ |
||
217 | protected function hydrateRowData(array $data, array &$result) |
||
221 | |||
222 | /** |
||
223 | * Hydrates all rows from the current statement instance at once. |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | abstract protected function hydrateAllData(); |
||
228 | |||
229 | /** |
||
230 | * Processes a row of the result set. |
||
231 | * |
||
232 | * Used for identity-based hydration (HYDRATE_OBJECT and HYDRATE_ARRAY). |
||
233 | * Puts the elements of a result row into a new array, grouped by the dql alias |
||
234 | * they belong to. The column names in the result set are mapped to their |
||
235 | * field names during this procedure as well as any necessary conversions on |
||
236 | * the values applied. Scalar values are kept in a specific key 'scalars'. |
||
237 | * |
||
238 | * @param array $data SQL Result Row. |
||
239 | * @param array &$id Dql-Alias => ID-Hash. |
||
240 | * @param array &$nonemptyComponents Does this DQL-Alias has at least one non NULL value? |
||
241 | * |
||
242 | * @return array An array with all the fields (name => value) of the data row, |
||
243 | * grouped by their component alias. |
||
244 | */ |
||
245 | protected function gatherRowData(array $data, array &$id, array &$nonemptyComponents) |
||
310 | |||
311 | /** |
||
312 | * Processes a row of the result set. |
||
313 | * |
||
314 | * Used for HYDRATE_SCALAR. This is a variant of _gatherRowData() that |
||
315 | * simply converts column names to field names and properly converts the |
||
316 | * values according to their types. The resulting row has the same number |
||
317 | * of elements as before. |
||
318 | * |
||
319 | * @param array $data |
||
320 | * |
||
321 | * @return array The processed row. |
||
322 | */ |
||
323 | protected function gatherScalarRowData(&$data) |
||
350 | |||
351 | /** |
||
352 | * Retrieve column information from ResultSetMapping. |
||
353 | * |
||
354 | * @param string $key Column name |
||
355 | * |
||
356 | * @return array|null |
||
357 | */ |
||
358 | protected function hydrateColumnInfo($key) |
||
435 | 878 | ||
436 | /** |
||
437 | * Retrieve ClassMetadata associated to entity class name. |
||
438 | * |
||
439 | * @param string $className |
||
440 | * |
||
441 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
442 | */ |
||
443 | protected function getClassMetadata($className) |
||
451 | } |
||
452 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.