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 | public function getObjectManager() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * generate gridid |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getId() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getOptions() : array |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | public function buildGrid(GridBuilderInterface $builder, array $options) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function getData() : array |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param QueryBuilder $queryBuilder |
||
| 165 | * @param ColumnInterface[] $columns |
||
| 166 | */ |
||
| 167 | protected function addSearch(QueryBuilder $queryBuilder, $columns) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param array|\Traversable $rows |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | protected function parseData($rows) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param QueryBuilder $queryBuilder |
||
| 255 | * @return Pagerfanta |
||
| 256 | */ |
||
| 257 | public function getPager(QueryBuilder $queryBuilder) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get value of primary column |
||
| 274 | * |
||
| 275 | * @param mixed $object |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | public function getPrimaryValue($object) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return null|string |
||
| 295 | */ |
||
| 296 | public function findPrimary() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param OptionsResolver $resolver |
||
| 309 | */ |
||
| 310 | public function configureOptions(OptionsResolver $resolver) |
||
| 328 | |||
| 329 | public function getColumnDefinition() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $name |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | public function hasOption(string $name) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $name |
||
| 352 | * @param mixed|null $default |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | public function getOption(string $name, $default = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string $name |
||
| 362 | * @return ColumnInterface |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function get(string $name) : ColumnInterface |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $name |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function remove(string $name) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $name |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function has(string $name) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return \Cwd\FancyGridBundle\Column\ColumnInterface[] |
||
| 395 | */ |
||
| 396 | public function all() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param array<ColumnInterface> $children |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function setChildren($children) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * |
||
| 414 | * @return \ArrayIterator |
||
| 415 | */ |
||
| 416 | public function getIterator() |
||
| 420 | } |
||
| 421 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.