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:
| 1 | <?php |
||
| 10 | class LiveJobsGridSource extends EntityGridSource |
||
| 11 | { |
||
| 12 | protected $jobManager; |
||
| 13 | protected $running = false; |
||
| 14 | |||
| 15 | public function getId() |
||
| 16 | { |
||
| 17 | return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.orm'; |
||
| 18 | } |
||
| 19 | |||
| 20 | public function __construct(JobManager $jobManager) |
||
| 21 | { |
||
| 22 | $this->jobManager = $jobManager; |
||
| 23 | /** @var EntityManager $entityManager */ |
||
| 24 | $entityManager = $jobManager->getObjectManager(); |
||
| 25 | parent::__construct($entityManager, $jobManager->getJobClass()); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function getColumns() |
||
| 29 | { |
||
| 30 | if ($columns = parent::getColumns()) { |
||
| 31 | return $columns; |
||
| 32 | } |
||
| 33 | $this->autoDiscoverColumns(); |
||
| 34 | |||
| 35 | return parent::getColumns(); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param bool $flag |
||
| 40 | */ |
||
| 41 | public function setRunning($flag) |
||
| 45 | |||
| 46 | public function isRunning() |
||
| 50 | |||
| 51 | protected function getRunningQueryBuilder() |
||
| 64 | |||
| 65 | View Code Duplication | protected function getQueryBuilder() |
|
| 66 | { |
||
| 67 | if ($this->isRunning()) { |
||
| 68 | return $this->getRunningQueryBuilder(); |
||
| 69 | } |
||
| 70 | |||
| 71 | $queryBuilder = $this->jobManager->getJobQueryBuilder(); |
||
| 72 | $queryBuilder->add('select', 'j'); |
||
| 73 | $queryBuilder->setFirstResult($this->offset) |
||
| 74 | ->setMaxResults($this->limit); |
||
| 75 | |||
| 76 | return $queryBuilder; |
||
| 77 | } |
||
| 78 | |||
| 79 | View Code Duplication | public function autoDiscoverColumns() |
|
| 90 | |||
| 91 | public function getDefaultSort() |
||
| 95 | |||
| 96 | public function getCount() |
||
| 97 | { |
||
| 98 | $qb = $this->getQueryBuilder(); |
||
| 99 | $qb->add('select', 'count(j)') |
||
| 106 | } |
||
| 107 |
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: