This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Neurony\QueryCache\Database; |
||
4 | |||
5 | use Exception; |
||
6 | use Illuminate\Database\ConnectionInterface; |
||
7 | use Illuminate\Database\Query\Builder as QueryBuilder; |
||
8 | use Illuminate\Database\Query\Grammars\Grammar; |
||
9 | use Illuminate\Database\Query\Processors\Processor; |
||
10 | |||
11 | class QueryCacheBuilder extends QueryBuilder |
||
12 | { |
||
13 | /** |
||
14 | * The cache tag value. |
||
15 | * The value comes from the Neurony\QueryCache\Traits\IsCacheable. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $cacheTag; |
||
20 | |||
21 | /** |
||
22 | * The cache type value. |
||
23 | * Can have one of the values present in the QueryCache class -> TYPE_CACHE constants. |
||
24 | * The value comes from the Neurony\QueryCache\IsCacheable. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $cacheType; |
||
29 | |||
30 | /** |
||
31 | * Create a new query builder instance. |
||
32 | * |
||
33 | * @param ConnectionInterface $connection |
||
34 | * @param Grammar|null $grammar |
||
35 | * @param Processor|null $processor |
||
36 | * @param string|null $cacheTag |
||
37 | * @param int|null $cacheType |
||
38 | */ |
||
39 | public function __construct( |
||
40 | ConnectionInterface $connection, |
||
41 | Grammar $grammar = null, |
||
42 | Processor $processor = null, |
||
43 | $cacheTag = null, $cacheType = null |
||
44 | ) { |
||
45 | parent::__construct($connection, $grammar, $processor); |
||
46 | |||
47 | $this->cacheType = $cacheType; |
||
0 ignored issues
–
show
|
|||
48 | $this->cacheTag = $cacheTag; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Returns a unique string that can identify this query. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getQueryCacheKey(): string |
||
57 | { |
||
58 | return json_encode([ |
||
59 | $this->toSql() => $this->getBindings(), |
||
60 | ]); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Flush the query cache based on the model's cache tag. |
||
65 | * |
||
66 | * @return void |
||
67 | * @throws Exception |
||
68 | */ |
||
69 | public function flushQueryCache(): void |
||
70 | { |
||
71 | cache()->store( |
||
72 | app('cache.query')->getAllQueryCacheStore() |
||
73 | )->tags($this->cacheTag)->flush(); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Insert a new record into the database. |
||
78 | * |
||
79 | * @param array $values |
||
80 | * @return bool |
||
81 | * @throws Exception |
||
82 | */ |
||
83 | public function insert(array $values): bool |
||
84 | { |
||
85 | $this->flushQueryCache(); |
||
86 | |||
87 | return parent::insert($values); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Update a record in the database. |
||
92 | * |
||
93 | * @param array $values |
||
94 | * @return int |
||
95 | * @throws Exception |
||
96 | */ |
||
97 | public function update(array $values): int |
||
98 | { |
||
99 | $this->flushQueryCache(); |
||
100 | |||
101 | return parent::update($values); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Delete a record from the database. |
||
106 | * |
||
107 | * @param int|null $id |
||
108 | * @return int|null |
||
109 | * @throws Exception |
||
110 | */ |
||
111 | public function delete($id = null): ?int |
||
112 | { |
||
113 | $this->flushQueryCache(); |
||
114 | |||
115 | return parent::delete($id); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Run a truncate statement on the table. |
||
120 | * |
||
121 | * @return void |
||
122 | * @throws Exception |
||
123 | */ |
||
124 | public function truncate(): void |
||
125 | { |
||
126 | $this->flushQueryCache(); |
||
127 | |||
128 | parent::truncate(); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Run the query as a "select" statement against the connection. |
||
133 | * |
||
134 | * @return array |
||
135 | * @throws Exception |
||
136 | */ |
||
137 | protected function runSelect(): array |
||
138 | { |
||
139 | switch ($this->cacheType) { |
||
140 | case app('cache.query')->cacheAllQueriesForeverType(): |
||
141 | return $this->runSelectWithAllQueriesCached(); |
||
142 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive. ![]() |
|||
143 | case app('cache.query')->cacheOnlyDuplicateQueriesOnceType(): |
||
144 | return $this->runSelectWithDuplicateQueriesCached(); |
||
145 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive. ![]() |
|||
146 | default: |
||
147 | return parent::runSelect(); |
||
148 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The break statement is not necessary if it is preceded for example by a return statement: switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive. ![]() |
|||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Run the query as a "select" statement against the connection. |
||
154 | * Also while fetching the results, cache all queries. |
||
155 | * |
||
156 | * @return mixed |
||
157 | * @throws Exception |
||
158 | */ |
||
159 | protected function runSelectWithAllQueriesCached() |
||
160 | { |
||
161 | return cache()->store( |
||
162 | app('cache.query')->getAllQueryCacheStore() |
||
163 | )->tags($this->cacheTag)->rememberForever($this->getQueryCacheKey(), function () { |
||
164 | return parent::runSelect(); |
||
165 | }); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Run the query as a "select" statement against the connection. |
||
170 | * Also while fetching the results, cache only duplicate queries for the current request. |
||
171 | * |
||
172 | * @return mixed |
||
173 | * @throws Exception |
||
174 | */ |
||
175 | protected function runSelectWithDuplicateQueriesCached() |
||
176 | { |
||
177 | return cache()->store( |
||
178 | app('cache.query')->getDuplicateQueryCacheStore() |
||
179 | )->tags($this->cacheTag)->remember($this->getQueryCacheKey(), 1, function () { |
||
180 | return parent::runSelect(); |
||
181 | }); |
||
182 | } |
||
183 | } |
||
184 |
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.