-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMysqliQueryReflector.php
More file actions
177 lines (147 loc) · 5.13 KB
/
MysqliQueryReflector.php
File metadata and controls
177 lines (147 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace staabm\PHPStanDba\QueryReflection;
use mysqli;
use mysqli_result;
use mysqli_sql_exception;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use staabm\PHPStanDba\Error;
use staabm\PHPStanDba\TypeMapping\MysqliTypeMapper;
final class MysqliQueryReflector implements QueryReflector, RecordingReflector
{
private const MYSQL_SYNTAX_ERROR_CODE = 1064;
private const MYSQL_UNKNOWN_COLUMN_IN_FIELDLIST = 1054;
public const MYSQL_UNKNOWN_TABLE = 1146;
private const MYSQL_INCORRECT_TABLE = 1103;
/**
* @api
*/
public const NAME = 'mysqli';
private const MYSQL_ERROR_CODES = [
self::MYSQL_SYNTAX_ERROR_CODE,
self::MYSQL_UNKNOWN_COLUMN_IN_FIELDLIST,
self::MYSQL_UNKNOWN_TABLE,
self::MYSQL_INCORRECT_TABLE,
];
private const MAX_CACHE_SIZE = 50;
/** @var array<string, mysqli_sql_exception|list<object>|null> */
private $cache = [];
/**
* @var mysqli
*/
private $db;
/**
* @var MysqliTypeMapper
*/
private $typeMapper;
public function __construct(mysqli $mysqli)
{
$this->db = $mysqli;
// set a sane default.. atm this should not have any impact
$this->db->set_charset('utf8');
// enable exception throwing on php <8.1
mysqli_report(\MYSQLI_REPORT_ERROR | \MYSQLI_REPORT_STRICT);
$this->db->autocommit(false);
}
public function validateQueryString(string $queryString): ?Error
{
$result = $this->simulateQuery($queryString);
if (!$result instanceof mysqli_sql_exception) {
return null;
}
$e = $result;
if (\in_array($e->getCode(), self::MYSQL_ERROR_CODES, true)) {
if (
self::MYSQL_SYNTAX_ERROR_CODE === $e->getCode()
&& QueryReflection::getRuntimeConfiguration()->isDebugEnabled()
) {
return Error::forSyntaxError($e, $e->getCode(), $queryString);
}
return Error::forException($e, $e->getCode());
}
return null;
}
/**
* @param self::FETCH_TYPE* $fetchType
*/
public function getResultType(string $queryString, int $fetchType): ?Type
{
$result = $this->simulateQuery($queryString);
if (!\is_array($result)) {
return null;
}
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$i = 0;
foreach ($result as $val) {
if (
!property_exists($val, 'name')
|| !property_exists($val, 'type')
|| !property_exists($val, 'flags')
|| !property_exists($val, 'length')
) {
throw new ShouldNotHappenException();
}
if (self::FETCH_TYPE_ASSOC === $fetchType || self::FETCH_TYPE_BOTH === $fetchType) {
$arrayBuilder->setOffsetValueType(
new ConstantStringType($val->name),
$this->typeMapper->mapToPHPStanType($val->type, $val->flags, $val->length)
);
}
if (self::FETCH_TYPE_NUMERIC === $fetchType || self::FETCH_TYPE_BOTH === $fetchType) {
$arrayBuilder->setOffsetValueType(
new ConstantIntegerType($i),
$this->typeMapper->mapToPHPStanType($val->type, $val->flags, $val->length)
);
}
++$i;
}
return $arrayBuilder->getArray();
}
public function setupDbaApi(?DbaApi $dbaApi): void
{
$this->typeMapper = new MysqliTypeMapper($dbaApi);
}
/**
* @return mysqli_sql_exception|list<object>|null
*/
private function simulateQuery(string $queryString)
{
if (\array_key_exists($queryString, $this->cache)) {
return $this->cache[$queryString];
}
if (\count($this->cache) > self::MAX_CACHE_SIZE) {
// make room for the next element by randomly removing a existing one
array_shift($this->cache);
}
$simulatedQuery = QuerySimulation::simulate($queryString);
if (null === $simulatedQuery) {
return $this->cache[$queryString] = null;
}
if (QueryReflection::getRuntimeConfiguration()->isAnalyzingWriteQueries()) {
$this->db->begin_transaction();
} else {
$this->db->begin_transaction(\MYSQLI_TRANS_START_READ_ONLY);
}
try {
$result = $this->db->query($simulatedQuery);
if (!$result instanceof mysqli_result) {
return $this->cache[$queryString] = null;
}
$resultInfo = $result->fetch_fields();
$result->close();
return $this->cache[$queryString] = $resultInfo;
} catch (mysqli_sql_exception $e) {
return $this->cache[$queryString] = $e;
} finally {
$this->db->rollback();
}
}
public function getDatasource()
{
return $this->db;
}
}