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 | 2 | public function getId() |
|
| 16 | { |
||
| 17 | 2 | return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.orm'; |
|
| 18 | } |
||
| 19 | |||
| 20 | 9 | public function __construct(JobManager $jobManager) |
|
| 21 | { |
||
| 22 | 9 | $this->jobManager = $jobManager; |
|
| 23 | /** @var EntityManager $entityManager */ |
||
| 24 | 9 | $entityManager = $jobManager->getObjectManager(); |
|
| 25 | 9 | parent::__construct($entityManager, $jobManager->getJobClass()); |
|
| 26 | 9 | } |
|
| 27 | |||
| 28 | 2 | public function getColumns() |
|
| 29 | { |
||
| 30 | 2 | if ($columns = parent::getColumns()) { |
|
| 31 | 2 | return $columns; |
|
| 32 | } |
||
| 33 | 2 | $this->autoDiscoverColumns(); |
|
| 34 | |||
| 35 | 2 | return parent::getColumns(); |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param bool $flag |
||
| 40 | */ |
||
| 41 | 9 | public function setRunning($flag) |
|
| 45 | |||
| 46 | 2 | public function isRunning() |
|
| 50 | |||
| 51 | protected function getRunningQueryBuilder() |
||
| 52 | { |
||
| 53 | /** @var EntityManager $entityManager */ |
||
| 54 | $entityManager = $this->jobManager->getObjectManager(); |
||
| 55 | $queryBuilder = $entityManager->createQueryBuilder()->add('select', 'j'); |
||
|
|
|||
| 56 | $queryBuilder->from($this->jobManager->getJobClass(), 'j'); |
||
| 57 | $queryBuilder->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_RUNNING); |
||
| 58 | $queryBuilder->orderBy('j.startedAt', 'DESC'); |
||
| 59 | $queryBuilder->setFirstResult($this->offset) |
||
| 60 | ->setMaxResults($this->limit); |
||
| 61 | |||
| 62 | return $queryBuilder; |
||
| 63 | } |
||
| 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 | 2 | View Code Duplication | public function autoDiscoverColumns() |
| 90 | |||
| 91 | 2 | public function getDefaultSort() |
|
| 95 | |||
| 96 | public function getCount() |
||
| 97 | { |
||
| 98 | $qb = $this->getQueryBuilder(); |
||
| 99 | $qb->add('select', 'count(j)') |
||
| 100 | ->setFirstResult(null) |
||
| 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: