Paginator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 28
c 1
b 0
f 0
dl 0
loc 76
ccs 24
cts 24
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCount() 0 18 3
A getData() 0 11 2
1
<?php
2
3
namespace Povs\ListerBundle\Service;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Povs\ListerBundle\Exception\ListQueryException;
7
use Throwable;
8
9
/**
10
 * @author Povilas Margaiatis <[email protected]>
11
 */
12
class Paginator
13
{
14
    /**
15
     * @var QueryBuilder
16
     */
17
    private $queryBuilder;
18
19
    /**
20
     * @var string
21
     */
22
    private $alias;
23
24
    /**
25
     * @var string
26
     */
27
    private $identifier;
28
29
    /**
30
     * @var int|null
31
     */
32
    private $count;
33
34
    /**
35
     * ListPaginator constructor.
36
     *
37
     * @param QueryBuilder       $queryBuilder
38
     * @param string             $alias       query base entity alias
39
     * @param string             $identifier query base entity identifier
40
     */
41 5
    public function __construct(QueryBuilder $queryBuilder, string $alias, string $identifier)
42
    {
43 5
        $this->queryBuilder = $queryBuilder;
44 5
        $this->alias = $alias;
45 5
        $this->identifier = $identifier;
46
    }
47
48
    /**
49
     * @return int
50
     */
51 3
    public function getCount(): int
52
    {
53 3
        if (null === $this->count) {
54 2
            $queryBuilder = clone $this->queryBuilder;
55
56
            try {
57 2
                $this->count = $queryBuilder->select(sprintf('COUNT(DISTINCT %s.%s)', $this->alias, $this->identifier))
0 ignored issues
show
Documentation Bug introduced by
It seems like $queryBuilder->select(sp...getSingleScalarResult() can also be of type boolean or string. However, the property $count is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58 2
                    ->distinct(false)
59 2
                    ->resetDQLPart('orderBy')
60 2
                    ->resetDQLPart('groupBy')
61 2
                    ->getQuery()
62 2
                    ->getSingleScalarResult();
63 1
            } catch (Throwable $e) {
64 1
                throw ListQueryException::invalidQueryConfiguration($e->getMessage(), $queryBuilder->getDQL());
65
            }
66
        }
67
68 2
        return $this->count;
69
    }
70
71
    /**
72
     * @param int $offset
73
     * @param int $length
74
     *
75
     * @return array
76
     */
77 2
    public function getData(int $offset, int $length): array
78
    {
79 2
        $queryBuilder = clone $this->queryBuilder;
80
81
        try {
82 2
            return $queryBuilder->setFirstResult($offset)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $queryBuilder->se...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
83 2
                ->setMaxResults($length)
84 2
                ->getQuery()
85 2
                ->getResult();
86 1
        } catch (Throwable $e) {
87 1
            throw ListQueryException::invalidQueryConfiguration($e->getMessage(), $queryBuilder->getDQL());
88
        }
89
    }
90
}
91