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 | 39 | public function __construct(Connection $connection) |
|
43 | { |
||
44 | 39 | $this->connection = $connection; |
|
45 | 39 | } |
|
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 | 36 | public function execute(QueryInterface $query) |
|
64 | { |
||
65 | 36 | if ($query->type() !== QueryInterface::TYPE_SELECT) |
|
66 | 24 | { |
|
67 | 12 | $this->affected = $this->response($query); |
|
0 ignored issues
–
show
|
|||
68 | |||
69 | 12 | return $this; |
|
70 | } |
||
71 | |||
72 | 24 | $this->result = $this->response($query); |
|
0 ignored issues
–
show
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 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 | 24 | 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 | 18 | public function items() |
|
103 | { |
||
104 | 18 | 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 | 36 | protected function response(QueryInterface $query) |
|
114 | { |
||
115 | 36 | $bindings = $query->bindings(); |
|
116 | |||
117 | 36 | $sql = (string) $query->sql(); |
|
118 | |||
119 | 36 | if ($query->type() === QueryInterface::TYPE_SELECT) |
|
120 | 24 | { |
|
121 | 24 | return $this->connection->executeQuery($sql, $bindings); |
|
122 | } |
||
123 | |||
124 | 12 | return $this->connection->executeUpdate($sql, $bindings); |
|
125 | } |
||
126 | } |
||
127 |
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 theid
property of an instance of theAccount
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.