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 AbstractFilter 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 AbstractFilter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | abstract class AbstractFilter implements FilterInterface |
||
38 | { |
||
39 | protected $managerRegistry; |
||
40 | protected $requestStack; |
||
41 | protected $logger; |
||
42 | protected $properties; |
||
43 | |||
44 | public function __construct(ManagerRegistry $managerRegistry, RequestStack $requestStack, LoggerInterface $logger = null, array $properties = null) |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) |
||
66 | |||
67 | /** |
||
68 | * Passes a property through the filter. |
||
69 | * |
||
70 | * @param string $property |
||
71 | * @param mixed $value |
||
72 | * @param QueryBuilder $queryBuilder |
||
73 | * @param QueryNameGeneratorInterface $queryNameGenerator |
||
74 | * @param string $resourceClass |
||
75 | * @param string|null $operationName |
||
76 | */ |
||
77 | abstract protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null); |
||
78 | |||
79 | /** |
||
80 | * Gets class metadata for the given resource. |
||
81 | * |
||
82 | * @param string $resourceClass |
||
83 | * |
||
84 | * @return ClassMetadata |
||
85 | */ |
||
86 | protected function getClassMetadata(string $resourceClass): ClassMetadata |
||
93 | |||
94 | /** |
||
95 | * Determines whether the given property is enabled. |
||
96 | * |
||
97 | * @param string $property |
||
98 | * |
||
99 | * @return bool |
||
100 | */ |
||
101 | protected function isPropertyEnabled(string $property/*, string $resourceClass*/): bool |
||
122 | |||
123 | /** |
||
124 | * Determines whether the given property is mapped. |
||
125 | * |
||
126 | * @param string $property |
||
127 | * @param string $resourceClass |
||
128 | * @param bool $allowAssociation |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | protected function isPropertyMapped(string $property, string $resourceClass, bool $allowAssociation = false): bool |
||
144 | |||
145 | /** |
||
146 | * Determines whether the given property is nested. |
||
147 | * |
||
148 | * @param string $property |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | protected function isPropertyNested(string $property/*, string $resourceClass*/): bool |
||
172 | |||
173 | /** |
||
174 | * Determines whether the given property is embedded. |
||
175 | * |
||
176 | * @param string $property |
||
177 | * @param string $resourceClass |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | protected function isPropertyEmbedded(string $property, string $resourceClass): bool |
||
185 | |||
186 | /** |
||
187 | * Gets nested class metadata for the given resource. |
||
188 | * |
||
189 | * @param string $resourceClass |
||
190 | * @param string[] $associations |
||
191 | * |
||
192 | * @return ClassMetadata |
||
193 | */ |
||
194 | protected function getNestedMetadata(string $resourceClass, array $associations): ClassMetadata |
||
211 | |||
212 | /** |
||
213 | * Splits the given property into parts. |
||
214 | * |
||
215 | * Returns an array with the following keys: |
||
216 | * - associations: array of associations according to nesting order |
||
217 | * - field: string holding the actual field (leaf node) |
||
218 | * |
||
219 | * @param string $property |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | protected function splitPropertyParts(string $property/*, string $resourceClass*/): array |
||
262 | |||
263 | /** |
||
264 | * Extracts properties to filter from the request. |
||
265 | * |
||
266 | * @param Request $request |
||
267 | * |
||
268 | * @return array |
||
269 | */ |
||
270 | protected function extractProperties(Request $request/*, string $resourceClass*/): array |
||
300 | |||
301 | /** |
||
302 | * Adds the necessary joins for a nested property. |
||
303 | * |
||
304 | * @param string $property |
||
305 | * @param string $rootAlias |
||
306 | * @param QueryBuilder $queryBuilder |
||
307 | * @param QueryNameGeneratorInterface $queryNameGenerator |
||
308 | * |
||
309 | * @throws InvalidArgumentException If property is not nested |
||
310 | * |
||
311 | * @return array An array where the first element is the join $alias of the leaf entity, |
||
312 | * the second element is the $field name |
||
313 | * the third element is the $associations array |
||
314 | */ |
||
315 | protected function addJoinsForNestedProperty(string $property, string $rootAlias, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator/*, string $resourceClass*/): array |
||
343 | |||
344 | /** |
||
345 | * Get the existing join from queryBuilder DQL parts. |
||
346 | * |
||
347 | * @param QueryBuilder $queryBuilder |
||
348 | * @param string $alias |
||
349 | * @param string $association the association field |
||
350 | * |
||
351 | * @return Join|null |
||
352 | */ |
||
353 | private function getExistingJoin(QueryBuilder $queryBuilder, string $alias, string $association) |
||
369 | |||
370 | /** |
||
371 | * Adds a join to the queryBuilder if none exists. |
||
372 | * |
||
373 | * @param QueryBuilder $queryBuilder |
||
374 | * @param QueryNameGeneratorInterface $queryNameGenerator |
||
375 | * @param string $alias |
||
376 | * @param string $association the association field |
||
377 | * |
||
378 | * @return string the new association alias |
||
379 | */ |
||
380 | protected function addJoinOnce(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $association): string |
||
400 | } |
||
401 |
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.