Completed
Pull Request — master (#30)
by Matthew
13:27 queued 10:46
created

LiveJobsGridSource   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 24.74 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 2
cbo 5
dl 24
loc 97
ccs 26
cts 50
cp 0.52
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setRunning() 0 4 1
A isRunning() 0 4 1
A autoDiscoverColumns() 11 11 4
A getDefaultSort() 0 4 1
A getId() 0 4 2
A __construct() 0 7 1
A getColumns() 0 9 2
A getRunningQueryBuilder() 0 13 1
A getQueryBuilder() 13 13 2
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 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)
42
    {
43 9
        $this->running = $flag;
44 9
    }
45
46 2
    public function isRunning()
47
    {
48 2
        return $this->running;
49
    }
50
51
    protected function getRunningQueryBuilder()
52
    {
53
        /** @var EntityManager $entityManager */
54
        $entityManager = $this->jobManager->getObjectManager();
55
        $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...
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()
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...
66
    {
67
        if ($this->isRunning()) {
68
            return $this->getRunningQueryBuilder();
69
        }
70
71
        $queryBuilder = $this->jobManager->getJobQueryBuilder();
72
        $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...
73
        $queryBuilder->setFirstResult($this->offset)
74
                        ->setMaxResults($this->limit);
75
76
        return $queryBuilder;
77
    }
78
79 2 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...
80
    {
81 2
        parent::autoDiscoverColumns();
82 2
        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...
83 2
            foreach ($this->columns as $column) {
84 2
                if ($column instanceof GridColumn) {
85 2
                    $column->setOption('sortable', false);
86
                }
87
            }
88
        }
89 2
    }
90
91 2
    public function getDefaultSort()
92
    {
93 2
        return null;
94
    }
95
96
    public function getCount()
97
    {
98
        $qb = $this->getQueryBuilder();
99
        $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...
100
            ->setFirstResult(null)
101
            ->setMaxResults(null);
102
103
        return $qb->getQuery()
104
            ->getSingleScalarResult();
105
    }
106
}
107