Completed
Push — master ( 5fe2ea...bba9f0 )
by Matthew
13:23
created

LiveJobsGridSource   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 25.53 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 5
dl 24
loc 94
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 2
A __construct() 0 7 1
A getColumns() 0 9 2
A setRunning() 0 4 1
A isRunning() 0 4 1
A getRunningQueryBuilder() 0 13 1
A getQueryBuilder() 13 13 2
A autoDiscoverColumns() 11 11 4
A getDefaultSort() 0 4 1
A getCount() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Dtc\QueueBundle\ORM;
4
5
use Doctrine\ORM\EntityManager;
6
use Dtc\GridBundle\Grid\Column\GridColumn;
7
use Dtc\GridBundle\Grid\Source\EntityGridSource;
8
use Dtc\QueueBundle\Model\BaseJob;
9
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
    public function setRunning($flag)
39
    {
40
        $this->running = $flag;
41
    }
42
43
    public function isRunning()
44
    {
45
        return $this->running;
46
    }
47
48
    protected function getRunningQueryBuilder()
49
    {
50
        /** @var EntityManager $entityManager */
51
        $entityManager = $this->jobManager->getObjectManager();
52
        $queryBuilder = $entityManager->createQueryBuilder()->add('select', 'j');
0 ignored issues
show
Documentation introduced by
'j' is of type string, but the function expects a object<Doctrine\ORM\Query\Expr\Base>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
        $queryBuilder->from($this->jobManager->getJobClass(), 'j');
54
        $queryBuilder->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_RUNNING);
55
        $queryBuilder->orderBy('j.startedAt', 'DESC');
56
        $queryBuilder->setFirstResult($this->offset)
57
                     ->setMaxResults($this->limit);
58
59
        return $queryBuilder;
60
    }
61
62 View Code Duplication
    protected function getQueryBuilder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        if ($this->isRunning()) {
65
            return $this->getRunningQueryBuilder();
66
        }
67
68
        $queryBuilder = $this->jobManager->getJobQueryBuilder();
69
        $queryBuilder->add('select', 'j');
0 ignored issues
show
Documentation introduced by
'j' is of type string, but the function expects a object<Doctrine\ORM\Query\Expr\Base>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
        $queryBuilder->setFirstResult($this->offset)
71
                     ->setMaxResults($this->limit);
72
73
        return $queryBuilder;
74
    }
75
76 View Code Duplication
    public function autoDiscoverColumns()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        parent::autoDiscoverColumns();
79
        if ($this->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
80
            foreach ($this->columns as $column) {
81
                if ($column instanceof GridColumn) {
82
                    $column->setOption('sortable', false);
83
                }
84
            }
85
        }
86
    }
87
88
    public function getDefaultSort()
89
    {
90
        return null;
91
    }
92
93
    public function getCount()
94
    {
95
        $qb = $this->getQueryBuilder();
96
        $qb->add('select', 'count(j)')
0 ignored issues
show
Documentation introduced by
'count(j)' is of type string, but the function expects a object<Doctrine\ORM\Query\Expr\Base>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
            ->setFirstResult(null)
98
            ->setMaxResults(null);
99
100
        return $qb->getQuery()
101
            ->getSingleScalarResult();
102
    }
103
}
104