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:
Complex classes like UpdateReport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UpdateReport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class UpdateReport extends ParametrizedMigrationQuery implements Migration, OrderedMigrationInterface |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public function getOrder() |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function up(Schema $schema, QueryBag $queries) |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function getDescription() |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function execute(LoggerInterface $logger) |
||
54 | |||
55 | /** |
||
56 | * @param LoggerInterface $logger |
||
57 | * @param bool $dryRun |
||
58 | */ |
||
59 | public function doExecute(LoggerInterface $logger, $dryRun = false) |
||
64 | |||
65 | /** |
||
66 | * @param LoggerInterface $logger |
||
67 | * @param bool $dryRun |
||
68 | * @param array $def |
||
69 | * @param array $row |
||
70 | * @throws \Doctrine\DBAL\DBALException |
||
71 | */ |
||
72 | protected function updateReport(LoggerInterface $logger, $dryRun, $def, $row) |
||
77 | |||
78 | /** |
||
79 | * @param LoggerInterface $logger |
||
80 | * @param bool $dryRun |
||
81 | * @param array $def |
||
82 | * @param array $row |
||
83 | * @throws \Doctrine\DBAL\DBALException |
||
84 | */ |
||
85 | protected function updateSegment(LoggerInterface $logger, $dryRun, $def, $row) |
||
90 | |||
91 | /** |
||
92 | * @param LoggerInterface $logger |
||
93 | * @param bool $dryRun |
||
94 | */ |
||
95 | View Code Duplication | protected function migrateReport(LoggerInterface $logger, $dryRun) |
|
110 | |||
111 | /** |
||
112 | * @param LoggerInterface $logger |
||
113 | * @param bool $dryRun |
||
114 | */ |
||
115 | View Code Duplication | protected function migrateSegment(LoggerInterface $logger, $dryRun) |
|
130 | |||
131 | /** |
||
132 | * @param LoggerInterface $logger |
||
133 | * @param bool $dryRun |
||
134 | * @param array $def |
||
135 | * @param array $row |
||
136 | * @param string $className |
||
137 | * @param string $oldField |
||
138 | * @param string $newField |
||
139 | */ |
||
140 | View Code Duplication | protected function fixSegmentDefs(LoggerInterface $logger, $dryRun, $def, $row, $className, $oldField, $newField) |
|
160 | |||
161 | /** |
||
162 | * @param LoggerInterface $logger |
||
163 | * @param bool $dryRun |
||
164 | * @param array $def |
||
165 | * @param array $row |
||
166 | * @param string $className |
||
167 | * @param string $oldField |
||
168 | * @param string $newField |
||
169 | */ |
||
170 | protected function fixReportDefs(LoggerInterface $logger, $dryRun, $def, $row, $className, $oldField, $newField) |
||
208 | |||
209 | /** |
||
210 | * @param LoggerInterface $logger |
||
211 | * @param bool $dryRun |
||
212 | * @param array $def |
||
213 | * @param array $row |
||
214 | * @param string $query |
||
215 | * @throws \Doctrine\DBAL\DBALException |
||
216 | */ |
||
217 | View Code Duplication | protected function executeQuery(LoggerInterface $logger, $dryRun, $def, $row, $query) |
|
226 | |||
227 | /** |
||
228 | * @param array $def |
||
229 | * @param array $row |
||
230 | * @param string $className |
||
231 | * @param string $oldField |
||
232 | * @param string $newField |
||
233 | * @param array $field |
||
234 | * @param string $key |
||
235 | * @return mixed |
||
236 | */ |
||
237 | View Code Duplication | protected function processFilterDefinition($def, $row, $className, $oldField, $newField, $field, $key) |
|
248 | |||
249 | /** |
||
250 | * @param array $def |
||
251 | * @param array $field |
||
252 | * @param string $key |
||
253 | * @return array |
||
254 | */ |
||
255 | View Code Duplication | protected function fixFilterCriterion($def, $field, $key) |
|
269 | } |
||
270 |
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.