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 AbstractGrid 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 AbstractGrid, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | abstract class AbstractGrid implements GridInterface, \IteratorAggregate |
||
31 | { |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $options; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $children = []; |
||
41 | |||
42 | /** |
||
43 | * @var ObjectManager |
||
44 | */ |
||
45 | protected $objectManager; |
||
46 | |||
47 | /** |
||
48 | * @var TranslatorInterface |
||
49 | */ |
||
50 | protected $translator; |
||
51 | |||
52 | /** |
||
53 | * @var \Symfony\Component\PropertyAccess\PropertyAccessor |
||
54 | */ |
||
55 | protected $accessor; |
||
56 | |||
57 | /** |
||
58 | * @var null|string |
||
59 | */ |
||
60 | protected $primary = null; |
||
61 | |||
62 | /** |
||
63 | * @var \Twig_Environment |
||
64 | */ |
||
65 | protected $twig; |
||
66 | |||
67 | /** |
||
68 | * AbstractGrid constructor. |
||
69 | * @param array $options |
||
70 | */ |
||
71 | public function __construct(TranslatorInterface $translator, array $options = array()) |
||
80 | |||
81 | /** |
||
82 | * @param \Twig_Environment $twig |
||
83 | */ |
||
84 | public function setTwig(\Twig_Environment $twig) |
||
88 | |||
89 | /** |
||
90 | * @param ObjectManager $objectManager |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setObjectManager($objectManager) |
||
99 | |||
100 | /** |
||
101 | * generate gridid |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getId() |
||
114 | |||
115 | /** |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getOptions() : array |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function buildGrid(GridBuilderInterface $builder, array $options) |
||
129 | |||
130 | /** |
||
131 | * @return array |
||
132 | */ |
||
133 | public function getData() : array |
||
157 | |||
158 | /** |
||
159 | * @param QueryBuilder $queryBuilder |
||
160 | * @param ColumnInterface[] $columns |
||
161 | */ |
||
162 | protected function addSearch(QueryBuilder $queryBuilder, $columns) |
||
207 | |||
208 | /** |
||
209 | * @param array|\Traversable $rows |
||
210 | * @return array |
||
211 | */ |
||
212 | protected function parseData($rows) |
||
243 | |||
244 | /** |
||
245 | * @param QueryBuilder $queryBuilder |
||
246 | * @return Pagerfanta |
||
247 | */ |
||
248 | public function getPager(QueryBuilder $queryBuilder) |
||
262 | |||
263 | /** |
||
264 | * Get value of primary column |
||
265 | * |
||
266 | * @param mixed $object |
||
267 | * |
||
268 | * @return mixed |
||
269 | */ |
||
270 | public function getPrimaryValue($object) |
||
283 | |||
284 | /** |
||
285 | * @return null|string |
||
286 | */ |
||
287 | public function findPrimary() |
||
297 | |||
298 | /** |
||
299 | * @param OptionsResolver $resolver |
||
300 | */ |
||
301 | public function configureOptions(OptionsResolver $resolver) |
||
319 | |||
320 | public function getColumnDefinition() |
||
331 | |||
332 | /** |
||
333 | * @param string $name |
||
334 | * @return bool |
||
335 | */ |
||
336 | public function hasOption(string $name) |
||
340 | |||
341 | /** |
||
342 | * @param string $name |
||
343 | * @param mixed|null $default |
||
344 | * @return mixed |
||
345 | */ |
||
346 | public function getOption(string $name, $default = null) |
||
350 | |||
351 | /** |
||
352 | * @param string $name |
||
353 | * @return ColumnInterface |
||
354 | */ |
||
355 | View Code Duplication | public function get(string $name) : ColumnInterface |
|
363 | |||
364 | /** |
||
365 | * @param string $name |
||
366 | * @return $this |
||
367 | */ |
||
368 | public function remove(string $name) |
||
374 | |||
375 | /** |
||
376 | * @param string $name |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function has(string $name) |
||
383 | |||
384 | /** |
||
385 | * @return \Cwd\FancyGridBundle\Column\ColumnInterface[] |
||
386 | */ |
||
387 | public function all() |
||
391 | |||
392 | /** |
||
393 | * @param array<ColumnInterface> $children |
||
394 | * @return $this |
||
395 | */ |
||
396 | public function setChildren($children) |
||
402 | |||
403 | /** |
||
404 | * |
||
405 | * @return \ArrayIterator |
||
406 | */ |
||
407 | public function getIterator() |
||
411 | } |
||
412 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.