Passed
Push — master ( 946461...3f32b7 )
by Rougin
03:35
created

Result::inserted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Windstorm\Doctrine;
4
5
use Doctrine\DBAL\Connection;
6
use Rougin\Windstorm\QueryInterface;
7
use Rougin\Windstorm\ResultInterface;
8
9
/**
10
 * Result
11
 *
12
 * @package Windstorm
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class Result implements ResultInterface
16
{
17
    /**
18
     * @var integer
19
     */
20
    protected $affected = 0;
21
22
    /**
23
     * @var \Doctrine\DBAL\Connection
24
     */
25
    protected $connection;
26
27
    /**
28
     * @var integer
29
     */
30
    protected $fetch = \PDO::FETCH_ASSOC;
31
32
    /**
33
     * @var \Doctrine\DBAL\Driver\Statement
34
     */
35
    protected $result;
36
37
    /**
38
     * Initializes the result instance.
39
     *
40
     * @param \Doctrine\DBAL\Connection $connection
41
     */
42 42
    public function __construct(Connection $connection)
43
    {
44 42
        $this->connection = $connection;
45 42
    }
46
47
    /**
48
     * Returns a number of affected rows.
49
     *
50
     * @return integer
51
     */
52 12
    public function affected()
53
    {
54 12
        return $this->affected;
55
    }
56
57
    /**
58
     * Returns a result from a query instance.
59
     *
60
     * @param  \Rougin\Windstorm\QueryInterface $query
61
     * @return self
62
     */
63 39
    public function execute(QueryInterface $query)
64
    {
65 39
        if ($query->type() !== QueryInterface::TYPE_SELECT)
66 26
        {
67 12
            $this->affected = $this->response($query);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->response($query) can also be of type Doctrine\DBAL\Driver\Statement. However, the property $affected is declared as type integer. 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...
68
69 12
            return $this;
70
        }
71
72 27
        $this->result = $this->response($query);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->response($query) can also be of type integer. However, the property $result is declared as type Doctrine\DBAL\Driver\Statement. 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...
73
74 27
        return $this;
75
    }
76
77
    /**
78
     * Returns the first row from result.
79
     *
80
     * @return mixed
81
     */
82 6
    public function first()
83
    {
84 6
        return $this->result->fetch($this->fetch);
85
    }
86
87
    /**
88
     * Returns the last inserted ID.
89
     *
90
     * @return integer
91
     */
92
    public function inserted()
93
    {
94
        return $this->connection->lastInsertId();
95
    }
96
97
    /**
98
     * Returns all items from the result.
99
     *
100
     * @return mixed
101
     */
102 21
    public function items()
103
    {
104 21
        return $this->result->fetchAll($this->fetch);
105
    }
106
107
    /**
108
     * Executes the query from the connection instance.
109
     *
110
     * @param  \Rougin\Windstorm\QueryInterface $query
111
     * @return \Doctrine\DBAL\Driver\Statement|integer
112
     */
113 39
    protected function response(QueryInterface $query)
114
    {
115 39
        $bindings = $query->bindings();
116
117 39
        $sql = (string) $query->sql();
118
119 39
        if ($query->type() === QueryInterface::TYPE_SELECT)
120 26
        {
121 27
            return $this->connection->executeQuery($sql, $bindings);
122
        }
123
124 12
        return $this->connection->executeUpdate($sql, $bindings);
125
    }
126
}
127