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

Select::getArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 0
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