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) |
||
201 | |||
202 | /** |
||
203 | * @param array|\Traversable $rows |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function parseData($rows) |
||
237 | |||
238 | /** |
||
239 | * @param QueryBuilder $queryBuilder |
||
240 | * @return Pagerfanta |
||
241 | */ |
||
242 | public function getPager(QueryBuilder $queryBuilder) |
||
256 | |||
257 | /** |
||
258 | * Get value of primary column |
||
259 | * |
||
260 | * @param mixed $object |
||
261 | * |
||
262 | * @return mixed |
||
263 | */ |
||
264 | public function getPrimaryValue($object) |
||
277 | |||
278 | /** |
||
279 | * @return null|string |
||
280 | */ |
||
281 | public function findPrimary() |
||
291 | |||
292 | /** |
||
293 | * @param OptionsResolver $resolver |
||
294 | */ |
||
295 | public function configureOptions(OptionsResolver $resolver) |
||
313 | |||
314 | public function getColumnDefinition() |
||
325 | |||
326 | /** |
||
327 | * @param string $name |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function hasOption(string $name) |
||
334 | |||
335 | /** |
||
336 | * @param string $name |
||
337 | * @param mixed|null $default |
||
338 | * @return mixed |
||
339 | */ |
||
340 | public function getOption(string $name, $default = null) |
||
344 | |||
345 | /** |
||
346 | * @param string $name |
||
347 | * @return ColumnInterface |
||
348 | */ |
||
349 | View Code Duplication | public function get(string $name) : ColumnInterface |
|
357 | |||
358 | /** |
||
359 | * @param string $name |
||
360 | * @return $this |
||
361 | */ |
||
362 | public function remove(string $name) |
||
368 | |||
369 | /** |
||
370 | * @param string $name |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function has(string $name) |
||
377 | |||
378 | /** |
||
379 | * @return \Cwd\FancyGridBundle\Column\ColumnInterface[] |
||
380 | */ |
||
381 | public function all() |
||
385 | |||
386 | /** |
||
387 | * @param array<ColumnInterface> $children |
||
388 | * @return $this |
||
389 | */ |
||
390 | public function setChildren($children) |
||
396 | |||
397 | /** |
||
398 | * |
||
399 | * @return \ArrayIterator |
||
400 | */ |
||
401 | public function getIterator() |
||
405 | } |
||
406 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.