Completed
Push — master ( 45b64e...037c01 )
by Oscar
01:38
created

Select::formatRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query;
5
6
use Closure;
7
use PDO;
8
use SimpleCrud\Table;
9
use SimpleCrud\Row;
10
11
final class Select implements QueryInterface
12
{
13
    use Traits\Common;
14
    use Traits\HasRelatedWith;
15
    use Traits\HasPagination;
16
    use Traits\HasJoinRelation;
17
18
    private $one;
19
    private $allowedMethods = [
20
        'from',
21
        'columns',
22
        'join',
23
        'catJoin',
24
        'groupBy',
25
        'having',
26
        'orHaving',
27
        'orderBy',
28
        'catHaving',
29
        'where',
30
        'orWhere',
31
        'catWhere',
32
        'limit',
33
        'offset',
34
        'distinct',
35
        'forUpdate',
36
        'setFlag',
37
    ];
38
39
    public function __construct(Table $table)
40
    {
41
        $this->table = $table;
42
43
        $this->query = $table->getDatabase()
44
            ->select()
45
            ->from((string) $table);
46
47
        foreach ($table->getFields() as $field) {
48
            $field->select($this->query);
49
        }
50
    }
51
52
    public function one(): self
53
    {
54
        $this->one = true;
55
        $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...
56
57
        return $this;
58
    }
59
60
    public function run()
61
    {
62
        $statement = $this->__invoke();
63
        $statement->setFetchMode(PDO::FETCH_ASSOC);
64
65
        $dataFields = [];
0 ignored issues
show
Unused Code introduced by
$dataFields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
67
        if ($this->one) {
68
            $data = $statement->fetch();
69
70
            return $data ? $this->createRow($data) : null;
71
        }
72
73
        $rows = array_map(Closure::fromCallable([$this, 'createRow']), $statement->fetchAll());
74
75
        return $this->table->createCollection($rows);
76
    }
77
78
    private function createRow(array $data): Row
79
    {
80
        $values = [];
81
        $extraData = [];
82
        $fields = $this->table->getFields();
83
84
        foreach ($data as $name => $value) {
85
            if (isset($fields[$name])) {
86
                $values[$name] = $fields[$name]->format($value);
87
            } else {
88
                $extraData[$name] = $value;
89
            }
90
        }
91
92
        return $this->table->create($values)->setData($extraData);
93
    }
94
}
95