Complex classes like OrmTotalsExtension 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 OrmTotalsExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class OrmTotalsExtension extends AbstractExtension |
||
31 | { |
||
32 | /** @var TranslatorInterface */ |
||
33 | protected $translator; |
||
34 | |||
35 | /** @var QueryBuilder */ |
||
36 | protected $masterQB; |
||
37 | |||
38 | /** @var NumberFormatter */ |
||
39 | protected $numberFormatter; |
||
40 | |||
41 | /** @var DateTimeFormatter */ |
||
42 | protected $dateTimeFormatter; |
||
43 | |||
44 | /** @var AclHelper */ |
||
45 | protected $aclHelper; |
||
46 | |||
47 | /** @var array */ |
||
48 | protected $groupParts = []; |
||
49 | |||
50 | /** |
||
51 | * @param TranslatorInterface $translator |
||
52 | * @param NumberFormatter $numberFormatter |
||
53 | * @param DateTimeFormatter $dateTimeFormatter |
||
54 | * @param AclHelper $aclHelper |
||
55 | */ |
||
56 | public function __construct( |
||
57 | TranslatorInterface $translator, |
||
58 | NumberFormatter $numberFormatter, |
||
59 | DateTimeFormatter $dateTimeFormatter, |
||
60 | AclHelper $aclHelper |
||
61 | ) { |
||
62 | $this->translator = $translator; |
||
63 | $this->numberFormatter = $numberFormatter; |
||
64 | $this->dateTimeFormatter = $dateTimeFormatter; |
||
65 | $this->aclHelper = $aclHelper; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function isApplicable(DatagridConfiguration $config) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function processConfigs(DatagridConfiguration $config) |
||
80 | { |
||
81 | $totalRows = $this->validateConfiguration( |
||
82 | new Configuration(), |
||
83 | ['totals' => $config->offsetGetByPath(Configuration::TOTALS_PATH)] |
||
84 | ); |
||
85 | |||
86 | if (!empty($totalRows)) { |
||
87 | foreach ($totalRows as $rowName => $rowConfig) { |
||
88 | $this->mergeTotals($totalRows, $rowName, $rowConfig, $config->getName()); |
||
89 | } |
||
90 | |||
91 | $config->offsetSetByPath(Configuration::TOTALS_PATH, $totalRows); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function visitResult(DatagridConfiguration $config, ResultsObject $result) |
||
107 | { |
||
108 | $onlyOnePage = $result->getTotalRecords() === count($result->getData()); |
||
109 | |||
110 | $totalData = []; |
||
111 | $totals = $config->offsetGetByPath(Configuration::TOTALS_PATH); |
||
112 | if (null !== $totals && $result->getData()) { |
||
113 | foreach ($totals as $rowName => $rowConfig) { |
||
114 | if ($onlyOnePage && $rowConfig[Configuration::TOTALS_HIDE_IF_ONE_PAGE_KEY]) { |
||
115 | unset($totals[$rowName]); |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | $totalData[$rowName] = $this->getTotalData( |
||
120 | $rowConfig, |
||
121 | $this->getData( |
||
122 | $result, |
||
123 | $rowConfig['columns'], |
||
124 | $rowConfig[Configuration::TOTALS_PER_PAGE_ROW_KEY], |
||
125 | $config->isDatasourceSkipAclApply() |
||
126 | ) |
||
127 | ); |
||
128 | } |
||
129 | } |
||
130 | $result->offsetAddToArray('options', ['totals' => $totalData]); |
||
131 | |||
132 | return $result; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function visitMetadata(DatagridConfiguration $config, MetadataObject $metaData) |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function getPriority() |
||
155 | |||
156 | /** |
||
157 | * Get Group by part of master query as array |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | protected function getGroupParts() |
||
183 | |||
184 | /** |
||
185 | * Get total row frontend data |
||
186 | * |
||
187 | * @param array $rowConfig Total row config |
||
188 | * @param array $data Db result data for current total row config |
||
189 | * @return array Array with array of columns total values and labels |
||
190 | */ |
||
191 | protected function getTotalData($rowConfig, $data) |
||
219 | |||
220 | /** |
||
221 | * Get root entities config data |
||
222 | * |
||
223 | * @param QueryBuilder $query |
||
224 | * @return array with root entities config |
||
225 | */ |
||
226 | protected function getRootIds(QueryBuilder $query) |
||
276 | |||
277 | /** |
||
278 | * Get total row data from database |
||
279 | * |
||
280 | * @param ResultsObject $pageData Grid page data |
||
281 | * @param array $columnsConfig Total row columns config |
||
282 | * @param bool $perPage Get data only for page data or for all data |
||
283 | * @param bool $skipAclWalkerCheck Check Acl with acl helper or not |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function getData(ResultsObject $pageData, $columnsConfig, $perPage = false, $skipAclWalkerCheck = false) |
||
326 | |||
327 | /** |
||
328 | * Add "in" expression as page limit to query builder |
||
329 | * |
||
330 | * @param QueryBuilder $dataQueryBuilder |
||
331 | * @param ResultsObject $pageData |
||
332 | * @param bool $perPage |
||
333 | */ |
||
334 | protected function addPageLimits(QueryBuilder $dataQueryBuilder, ResultsObject $pageData, $perPage) |
||
335 | { |
||
336 | $rootIdentifiers = $this->getRootIds($dataQueryBuilder); |
||
337 | |||
338 | if (!$perPage) { |
||
339 | $queryBuilder = clone $this->masterQB; |
||
340 | $data = $queryBuilder |
||
341 | ->getQuery() |
||
342 | ->setFirstResult(null) |
||
343 | ->setMaxResults(null) |
||
344 | ->getScalarResult(); |
||
345 | } else { |
||
346 | $data = $pageData->getData(); |
||
347 | } |
||
348 | foreach ($rootIdentifiers as $identifier) { |
||
349 | $ids = ArrayUtil::arrayColumn($data, $identifier['alias']); |
||
350 | |||
351 | $field = isset($identifier['entityAlias']) |
||
352 | ? $identifier['entityAlias'] . '.' . $identifier['fieldAlias'] |
||
353 | : $identifier['fieldAlias']; |
||
354 | |||
355 | $filteredIds = array_filter($ids); |
||
356 | if (empty($filteredIds)) { |
||
357 | continue; |
||
358 | } |
||
359 | |||
360 | $dataQueryBuilder->andWhere($dataQueryBuilder->expr()->in($field, $ids)); |
||
361 | } |
||
362 | |||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Apply formatting to totals values |
||
367 | * |
||
368 | * @param mixed|null $val |
||
369 | * @param string|null $formatter |
||
370 | * @return string|null |
||
371 | */ |
||
372 | protected function applyFrontendFormatting($val = null, $formatter = null) |
||
404 | |||
405 | /** |
||
406 | * Merge total rows configs |
||
407 | * |
||
408 | * @param array $totalRows |
||
409 | * @param string $rowName |
||
410 | * @param array $rowConfig |
||
411 | * @param string $gridName |
||
412 | * @return array |
||
413 | * @throws LogicException |
||
414 | */ |
||
415 | protected function mergeTotals(&$totalRows, $rowName, $rowConfig, $gridName) |
||
445 | } |
||
446 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: