Complex classes like FixReportsQuery 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 FixReportsQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class FixReportsQuery extends ParametrizedMigrationQuery |
||
18 | { |
||
19 | const LIMIT = 100; |
||
20 | |||
21 | /** @var array */ |
||
22 | protected $fixes = [ |
||
23 | 'filters' => [ |
||
24 | 'OroCRM\Bundle\SalesBundle\Entity\Opportunity' => 'status', |
||
25 | 'OroCRM\Bundle\SalesBundle\Entity\Lead' => 'status', |
||
26 | ], |
||
27 | 'removedFields' => [ |
||
28 | 'status+OroCRM\Bundle\SalesBundle\Entity\OpportunityStatus::label' => 'status', |
||
29 | 'status+OroCRM\Bundle\SalesBundle\Entity\OpportunityStatus::name' => 'status', |
||
30 | 'status+OroCRM\Bundle\SalesBundle\Entity\LeadStatus::label' => 'status', |
||
31 | 'status+OroCRM\Bundle\SalesBundle\Entity\LeadStatus::name' => 'status', |
||
32 | ], |
||
33 | 'chainFilters' => [ |
||
34 | 'OroCRM\Bundle\SalesBundle\Entity\Opportunity::status', |
||
35 | 'OroCRM\Bundle\SalesBundle\Entity\Lead::status', |
||
36 | ], |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function getDescription() |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function setConnection(Connection $connection) |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function execute(LoggerInterface $logger) |
||
65 | |||
66 | /** |
||
67 | * @param LoggerInterface $logger |
||
68 | * @param bool $dryRun |
||
69 | */ |
||
70 | protected function doExecute(LoggerInterface $logger, $dryRun = false) |
||
77 | |||
78 | /** |
||
79 | * @param LoggerInterface $logger |
||
80 | * @param string $table |
||
81 | * @param bool $dryRun |
||
82 | */ |
||
83 | protected function updateRows(LoggerInterface $logger, $table, $dryRun = false) |
||
103 | |||
104 | /** |
||
105 | * @param LoggerInterface $logger |
||
106 | * @param string $table |
||
107 | * @param array $row |
||
108 | * @param bool $dryRun |
||
109 | */ |
||
110 | protected function saveChanges(LoggerInterface $logger, $table, array $row, $dryRun) |
||
127 | |||
128 | /** |
||
129 | * @param array $row |
||
130 | * |
||
131 | * @return bool True if there are changes, false otherwise |
||
132 | */ |
||
133 | protected function processRow(array &$row) |
||
162 | |||
163 | /** |
||
164 | * If you add same field twice in "Columns" section in query designer, |
||
165 | * you'll get exception that alias is already defined |
||
166 | * |
||
167 | * Also there is no point in having duplicated stuff |
||
168 | * |
||
169 | * @param array $def |
||
170 | */ |
||
171 | protected function removeDuplicates(array &$def) |
||
189 | |||
190 | /** |
||
191 | * @param array $def |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | protected function fixColumns(array &$def) |
||
210 | |||
211 | /** |
||
212 | * @param array $row |
||
213 | * @param array $def |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | protected function fixFilters(array $row, array &$def) |
||
260 | |||
261 | /** |
||
262 | * @param mixed $config |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function fixRemovedField(&$config, $key) |
||
287 | |||
288 | /** |
||
289 | * @param mixed $filter |
||
290 | * |
||
291 | * @return mixed |
||
292 | */ |
||
293 | protected function filterValue($filter) |
||
297 | |||
298 | /** |
||
299 | * @param array $def |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | protected function fixGroupingColumns(array &$def) |
||
314 | |||
315 | /** |
||
316 | * @param array $row |
||
317 | * @param mixed $filter |
||
318 | * |
||
319 | * @return bool |
||
320 | */ |
||
321 | protected function isNeedToUpdateEnumFilter(array $row, $filter) |
||
342 | |||
343 | /** |
||
344 | * Converts string operators to enum operators |
||
345 | * |
||
346 | * @param int $stringType |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | protected function getEnumType($stringType) |
||
354 | |||
355 | /** |
||
356 | * @return int |
||
357 | */ |
||
358 | protected function getCount($table) |
||
365 | |||
366 | /** |
||
367 | * @return QueryBuilder |
||
368 | */ |
||
369 | protected function createQb($table) |
||
375 | } |
||
376 |