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

Select::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query;
5
6
use PDO;
7
use SimpleCrud\Events\CreateSelectQuery;
8
use SimpleCrud\Table;
9
10
final class Select implements QueryInterface
11
{
12
    use Traits\Common;
13
    use Traits\HasRelatedWith;
14
    use Traits\HasPagination;
15
    use Traits\HasJoinRelation;
16
17
    private $one;
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)
38
    {
39
        $this->table = $table;
40
41
        $fields = array_map(
42
            function ($field) {
43
                return (string) $field;
44
            },
45
            array_values($table->getFields())
46
        );
47
48
        $this->query = $table->getDatabase()
49
            ->select()
50
            ->from((string) $table)
51
            ->columns(...$fields);
52
53
        $eventDispatcher = $table->getEventDispatcher();
54
55
        if ($eventDispatcher) {
56
            $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...
57
        }
58
    }
59
60
    public function one(): self
61
    {
62
        $this->one = true;
63
        $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...
64
65
        return $this;
66
    }
67
68
    public function run()
69
    {
70
        $statement = $this->__invoke();
71
        $statement->setFetchMode(PDO::FETCH_ASSOC);
72
73
        if ($this->one) {
74
            $data = $statement->fetch();
75
76
            return $data ? $this->table->create($data, true) : null;
77
        }
78
79
        return $this->table->createCollection($statement->fetchAll(), true);
80
    }
81
}
82