Completed
Push — master ( ac7343...32cfc5 )
by Oscar
01:27
created

Select   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A one() 0 7 1
A run() 0 12 3
A getArray() 0 11 3
A createRow() 0 9 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Queries;
5
6
use Closure;
7
use PDO;
8
use SimpleCrud\Row;
9
use SimpleCrud\Table;
10
11
class Select extends Query
12
{
13
    use Traits\HasRelatedWith;
14
    use Traits\HasPagination;
15
    use Traits\HasJoinRelation;
16
17
    private $one;
18
    protected const ALLOWED_METHODS = [
19
        'from',
20
        'columns',
21
        'join',
22
        'catJoin',
23
        'groupBy',
24
        'having',
25
        'orHaving',
26
        'orderBy',
27
        'catHaving',
28
        'where',
29
        'whereEquals',
30
        'orWhere',
31
        'catWhere',
32
        'limit',
33
        'offset',
34
        'distinct',
35
        'forUpdate',
36
        'setFlag',
37
        'bindValue',
38
    ];
39
40
    public function __construct(Table $table)
41
    {
42
        $this->table = $table;
43
44
        $this->query = $table->getDatabase()
45
            ->select()
46
            ->from((string) $table);
47
48
        foreach ($table->getFields() as $field) {
49
            $field->select($this->query);
50
        }
51
    }
52
53
    public function one(): self
54
    {
55
        $this->one = true;
56
        $this->query->limit(1);
0 ignored issues
show
Bug introduced by
The method limit does only exist in Atlas\Query\Delete and A... and Atlas\Query\Update, but not in Atlas\Query\Insert.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
58
        return $this;
59
    }
60
61
    public function run()
62
    {
63
        $data = $this->getArray();
64
65
        if ($this->one) {
66
            return $data ? $this->createRow($data) : null;
67
        }
68
69
        $rows = array_map(Closure::fromCallable([$this, 'createRow']), $data);
70
71
        return $this->table->createCollection($rows);
72
    }
73
74
    public function getArray(): ?array
75
    {
76
        $statement = $this->__invoke();
77
        $statement->setFetchMode(PDO::FETCH_ASSOC);
78
79
        if ($this->one) {
80
            return $statement->fetch() ?: null;
81
        }
82
83
        return $statement->fetchAll();
84
    }
85
86
    private function createRow(array $data): Row
87
    {
88
        $data = $this->table->format($data);
89
        $fields = $this->table->getFields();
90
        $extraData = array_diff_key($data, $fields);
91
        $values = array_intersect_key($data, $fields);
92
93
        return $this->table->create($values)->setData($extraData);
94
    }
95
}
96