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 |
||
| 29 | abstract class AbstractGrid implements GridInterface, \IteratorAggregate |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $options; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $children = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ObjectManager |
||
| 43 | */ |
||
| 44 | protected $objectManager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var TranslatorInterface |
||
| 48 | */ |
||
| 49 | protected $translator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Symfony\Component\PropertyAccess\PropertyAccessor |
||
| 53 | */ |
||
| 54 | protected $accessor; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var null|string |
||
| 58 | */ |
||
| 59 | protected $primary = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \Twig_Environment |
||
| 63 | */ |
||
| 64 | protected $twig; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * AbstractGrid constructor. |
||
| 68 | * @param array $options |
||
| 69 | */ |
||
| 70 | public function __construct(TranslatorInterface $translator, array $options = array()) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param \Twig_Environment $twig |
||
| 82 | */ |
||
| 83 | public function setTwig(\Twig_Environment $twig) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param ObjectManager $objectManager |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function setObjectManager($objectManager) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * generate gridid |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getId() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getOptions() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function buildGrid(GridBuilderInterface $builder, array $options) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function getData() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param \Doctrine\DBAL\Query\QueryBuilder $queryBuilder |
||
| 161 | * @param ColumnInterface[] $columns |
||
| 162 | */ |
||
| 163 | protected function addSearch($queryBuilder, $columns) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param array|\Traversable $rows |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | protected function parseData($rows) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param QueryBuilder $queryBuilder |
||
| 218 | * @return Pagerfanta |
||
| 219 | */ |
||
| 220 | public function getPager(QueryBuilder $queryBuilder) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get value of primary column |
||
| 237 | * |
||
| 238 | * @param mixed $object |
||
| 239 | * |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | public function getPrimaryValue($object) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return null|string |
||
| 258 | */ |
||
| 259 | public function findPrimary() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param OptionsResolver $resolver |
||
| 272 | */ |
||
| 273 | public function configureOptions(OptionsResolver $resolver) |
||
| 291 | |||
| 292 | public function getColumnDefinition() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $name |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | public function hasOption($name) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $name |
||
| 315 | * @param string|null $default |
||
| 316 | * @return misc |
||
| 317 | */ |
||
| 318 | public function getOption($name, $default = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $name |
||
| 325 | * @return ColumnInterface |
||
| 326 | */ |
||
| 327 | View Code Duplication | public function get($name) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $name |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function remove($name) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $name |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function has($name) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return \Cwd\FancyGridBundle\Column\ColumnInterface[] |
||
| 358 | */ |
||
| 359 | public function all() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param array<ColumnInterface> $children |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function setChildren($children) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * |
||
| 377 | * @return \ArrayIterator |
||
| 378 | */ |
||
| 379 | public function getIterator() |
||
| 383 | } |
||
| 384 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: