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 \Doctrine\DBAL\Query\QueryBuilder $queryBuilder |
||
160 | * @param ColumnInterface[] $columns |
||
161 | */ |
||
162 | protected function addSearch($queryBuilder, $columns) |
||
201 | |||
202 | /** |
||
203 | * @param array|\Traversable $rows |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function parseData($rows) |
||
229 | |||
230 | /** |
||
231 | * @param QueryBuilder $queryBuilder |
||
232 | * @return Pagerfanta |
||
233 | */ |
||
234 | public function getPager(QueryBuilder $queryBuilder) |
||
248 | |||
249 | /** |
||
250 | * Get value of primary column |
||
251 | * |
||
252 | * @param mixed $object |
||
253 | * |
||
254 | * @return mixed |
||
255 | */ |
||
256 | public function getPrimaryValue($object) |
||
269 | |||
270 | /** |
||
271 | * @return null|string |
||
272 | */ |
||
273 | public function findPrimary() |
||
283 | |||
284 | /** |
||
285 | * @param OptionsResolver $resolver |
||
286 | */ |
||
287 | public function configureOptions(OptionsResolver $resolver) |
||
305 | |||
306 | public function getColumnDefinition() |
||
317 | |||
318 | /** |
||
319 | * @param string $name |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function hasOption(string $name) |
||
326 | |||
327 | /** |
||
328 | * @param string $name |
||
329 | * @param string|null $default |
||
330 | * @return misc |
||
331 | */ |
||
332 | public function getOption(string $name, $default = null) |
||
336 | |||
337 | /** |
||
338 | * @param string $name |
||
339 | * @return ColumnInterface |
||
340 | */ |
||
341 | View Code Duplication | public function get(string $name) : ColumnInterface |
|
349 | |||
350 | /** |
||
351 | * @param string $name |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function remove(string $name) |
||
360 | |||
361 | /** |
||
362 | * @param string $name |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function has(string $name) |
||
369 | |||
370 | /** |
||
371 | * @return \Cwd\FancyGridBundle\Column\ColumnInterface[] |
||
372 | */ |
||
373 | public function all() |
||
377 | |||
378 | /** |
||
379 | * @param array<ColumnInterface> $children |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function setChildren($children) |
||
388 | |||
389 | /** |
||
390 | * |
||
391 | * @return \ArrayIterator |
||
392 | */ |
||
393 | public function getIterator() |
||
397 | } |
||
398 |
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: