Completed
Branch 7-dev (bf2895)
by Oscar
03:53
created

Aggregation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 9
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A run() 0 9 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query\Traits;
5
6
use PDO;
7
use SimpleCrud\Events\CreateSelectQuery;
8
use SimpleCrud\Table;
9
10
trait Aggregation
11
{
12
    use Common;
13
    use HasRelatedWith;
14
    use HasPagination;
15
    use HasJoinRelation;
16
17
    private $field;
18
    private $allowedMethods = [
19
        'from',
20
        'join',
21
        'catJoin',
22
        'groupBy',
23
        'having',
24
        'orHaving',
25
        'orderBy',
26
        'catHaving',
27
        'where',
28
        'orWhere',
29
        'catWhere',
30
        'limit',
31
        'offset',
32
        'distinct',
33
        'forUpdate',
34
        'setFlag',
35
    ];
36
37
    public function __construct(Table $table, string $field = 'id')
38
    {
39
        $this->table = $table;
40
        $this->field = $field;
41
42
        $this->query = $table->getDatabase()
43
            ->select()
44
            ->from((string) $table)
45
            ->columns(sprintf('%s(%s)', self::AGGREGATION_FUNCTION, $field));
46
47
        $eventDispatcher = $table->getEventDispatcher();
48
49
        if ($eventDispatcher) {
50
            $eventDispatcher->dispatch(new CreateSelectQuery($this));
0 ignored issues
show
Documentation introduced by
new \SimpleCrud\Events\CreateSelectQuery($this) is of type object<SimpleCrud\Events\CreateSelectQuery>, but the function expects a object<Psr\EventDispatcher\object>.

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...
51
        }
52
    }
53
54
    public function run()
55
    {
56
        $statement = $this->__invoke();
57
        $statement->setFetchMode(PDO::FETCH_NUM);
58
59
        $field = $this->table->{$this->field};
60
61
        return $field->format($statement->fetchColumn());
62
    }
63
}
64