Completed
Push — master ( 6b0411...a61b3b )
by Bas
04:04
created

Builder::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 4
nc 2
nop 4
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query;
4
5
use Illuminate\Database\ConnectionInterface;
6
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
7
use InvalidArgumentException;
8
use LaravelFreelancerNL\Aranguent\Query\Grammars\Grammar;
9
use LaravelFreelancerNL\Aranguent\Query\Processors\Processor;
10
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException;
11
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
12
13
class Builder extends IlluminateQueryBuilder
14
{
15
    /**
16
     * @var QueryBuilder
17
     */
18
    public $aqb;
19
20
    /**
21
     * @override
22
     * Create a new query builder instance.
23
     *
24
     * @param ConnectionInterface $connection
25
     * @param Grammar $grammar
26
     * @param Processor $processor
27
     * @param QueryBuilder|null $aqb
28
     */
29
    public function __construct(ConnectionInterface $connection,
30
                                Grammar $grammar = null,
31
                                Processor $processor = null,
32
                                QueryBuilder $aqb = null)
33
    {
34
        $this->connection = $connection;
35
        $this->grammar = $grammar ?: $connection->getQueryGrammar();
0 ignored issues
show
Documentation Bug introduced by
It seems like $grammar ?: $connection->getQueryGrammar() can also be of type LaravelFreelancerNL\Aran...\Query\Grammars\Grammar. However, the property $grammar is declared as type Illuminate\Database\Query\Grammars\Grammar. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
        $this->processor = $processor ?: $connection->getPostProcessor();
37
        if (!$aqb instanceof QueryBuilder) {
38
            $aqb = new QueryBuilder();
39
        }
40
        $this->aqb = $aqb;
41
    }
42
43
    /**
44
     * Run the query as a "select" statement against the connection.
45
     *
46
     * @return array
47
     */
48
    protected function runSelect()
49
    {
50
        return $this->connection->select($this->grammar->compileSelect($this));
51
    }
52
53
    /**
54
     * Get the SQL representation of the query.
55
     *
56
     * @return string
57
     */
58
    public function toSql()
59
    {
60
        return $this->grammar->compileSelect($this)->query;
0 ignored issues
show
Bug introduced by
The property query does not exist on string.
Loading history...
61
    }
62
63
    /**
64
     * Insert a new record and get the value of the primary key.
65
     *
66
     * @param  array  $values
67
     * @param  string|null  $sequence
68
     * @return int
69
     */
70
    public function insertGetId(array $values, $sequence = null)
71
    {
72
        $qb = $this->grammar->compileInsertGetId($this, $values, $sequence);
73
74
        $result = $this->getConnection()->execute($qb);
0 ignored issues
show
Bug introduced by
The method execute() does not exist on Illuminate\Database\ConnectionInterface. It seems like you code against a sub-type of Illuminate\Database\ConnectionInterface such as LaravelFreelancerNL\Aranguent\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $result = $this->getConnection()->/** @scrutinizer ignore-call */ execute($qb);
Loading history...
75
76
        return $result;
77
    }
78
79
    /**
80
     * Update a record in the database.
81
     *
82
     * @param  array  $values
83
     * @return int
84
     */
85
    public function update(array $values)
86
    {
87
        $aqb = $this->grammar->compileUpdate($this, $values);
88
89
        return $this->connection->update($aqb);
90
    }
91
92
    /**
93
     * Delete a record from the database.
94
     *
95
     * @param  mixed  $_key
96
     * @return int
97
     */
98
    public function delete($_key = null)
99
    {
100
        // If an ID is passed to the method, we will set the where clause to check the
101
        // ID to let developers to simply and quickly remove a single row from this
102
        // database without manually specifying the "where" clauses on the query.
103
        if (! is_null($_key)) {
104
            $this->where($this->from.'._key', '=', $_key);
105
        }
106
        $aqb = $this->grammar->compileDelete($this, $_key);
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Database\Quer...rammar::compileDelete() has too many arguments starting with $_key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        /** @scrutinizer ignore-call */ 
107
        $aqb = $this->grammar->compileDelete($this, $_key);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
107
var_dump($aqb->query);
0 ignored issues
show
Bug introduced by
The property query does not exist on string.
Loading history...
Security Debugging Code introduced by
var_dump($aqb->query) looks like debug code. Are you sure you do not want to remove it?
Loading history...
108
        return $this->connection->delete($aqb);
109
    }
110
111
112
}
113