| Total Complexity | 66 |
| Total Lines | 431 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TableGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableGateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class TableGateway extends AbstractTableGateway implements TableGatewayInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The text with the last query executed |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $lastQuery; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * An array with the last binded values |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $lastValues; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Returns the lastQuery |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function getLastQuery() |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns the lastValues |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public function getLastValues() |
||
| 54 | { |
||
| 55 | return $this->lastValues; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Select statement |
||
| 60 | * |
||
| 61 | * @param array $where |
||
| 62 | * |
||
| 63 | * @return array With all results |
||
| 64 | */ |
||
| 65 | public function select(Array $where = []) |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Insert statement |
||
| 148 | * |
||
| 149 | * @param array $data |
||
| 150 | * |
||
| 151 | * @throws RuntimeException from internal execute() |
||
| 152 | * @throws LogicException |
||
| 153 | * |
||
| 154 | * @return resource|object |
||
| 155 | */ |
||
| 156 | public function insert(Array $data) |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Update statement |
||
| 225 | * |
||
| 226 | * @param array $set |
||
| 227 | * @param array $where |
||
| 228 | * |
||
| 229 | * @throws RuntimeException from internal execute() |
||
| 230 | * @throws LogicException |
||
| 231 | * @throws SecurityException |
||
| 232 | * |
||
| 233 | * @return resource|object |
||
| 234 | */ |
||
| 235 | public function update(Array $set, Array $where) |
||
| 236 | { |
||
| 237 | $parsed_set = []; |
||
| 238 | |||
| 239 | if (!count($set)) |
||
| 240 | throw new \LogicException("You cannot update rows without SET clause"); |
||
| 241 | |||
| 242 | if (!count($where)) |
||
| 243 | throw new SecurityException("You cannot update rows without WHERE clause!"); |
||
|
|
|||
| 244 | |||
| 245 | $bind_values = []; |
||
| 246 | |||
| 247 | $driver = $this->getDb()->getDriverName(); |
||
| 248 | |||
| 249 | $k = 0; |
||
| 250 | |||
| 251 | foreach ($set as $key => $value) |
||
| 252 | { |
||
| 253 | $k++; |
||
| 254 | |||
| 255 | if (is_null($value)) |
||
| 256 | $parsed_set[] = "$key = NULL"; |
||
| 257 | elseif ($value instanceof SQLFunction) |
||
| 258 | $parsed_set[] = "$key = " . $value->getStatement(); |
||
| 259 | elseif (is_array($value)) |
||
| 260 | { |
||
| 261 | $parsed_in = []; |
||
| 262 | |||
| 263 | foreach ($value as $in_value) |
||
| 264 | { |
||
| 265 | switch ($driver) |
||
| 266 | { |
||
| 267 | case 'Oci8': |
||
| 268 | |||
| 269 | # [POSSIBLE BUG] - To Future revision (What about non-string values ?) |
||
| 270 | if (is_string($in_value)) |
||
| 271 | $parsed_in[] = ":$k"; |
||
| 272 | |||
| 273 | $bind_values[":$k"] = $in_value; |
||
| 274 | break; |
||
| 275 | |||
| 276 | case 'Mysqli' || 'Sqlsrv': |
||
| 277 | $parsed_in[] = "?"; |
||
| 278 | $bind_values[] = $in_value; |
||
| 279 | break; |
||
| 280 | } |
||
| 281 | |||
| 282 | $k++; |
||
| 283 | } |
||
| 284 | |||
| 285 | $parsed_set[] = "$key IN (" . implode(", ", $parsed_in) . ")"; |
||
| 286 | } |
||
| 287 | else |
||
| 288 | { |
||
| 289 | switch ($driver) |
||
| 290 | { |
||
| 291 | case 'Oci8': |
||
| 292 | $parsed_set[] = "$key = :$k"; |
||
| 293 | $bind_values[":$k"] = $value; |
||
| 294 | break; |
||
| 295 | |||
| 296 | case 'Mysqli' || 'Sqlsrv': |
||
| 297 | $parsed_set[] = "$key = ?"; |
||
| 298 | $bind_values[] = $value; |
||
| 299 | break; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | $parsed_set = implode(",\r\n\t", $parsed_set); |
||
| 305 | |||
| 306 | $parsed_where = []; |
||
| 307 | |||
| 308 | foreach ($where as $key => $value) |
||
| 309 | { |
||
| 310 | $k++; |
||
| 311 | |||
| 312 | if (is_null($value)) |
||
| 313 | $parsed_where[] = "$key IS NULL"; |
||
| 314 | elseif ($value instanceof SQLFunction) |
||
| 315 | $parsed_where[] = "$key = " . $value->getStatement(); |
||
| 316 | elseif (is_array($value)) |
||
| 317 | { |
||
| 318 | $parsed_in = []; |
||
| 319 | |||
| 320 | foreach ($value as $in_value) |
||
| 321 | { |
||
| 322 | switch ($driver) |
||
| 323 | { |
||
| 324 | case 'Oci8': |
||
| 325 | $parsed_in[] = ":$k"; |
||
| 326 | $bind_values[":$k"] = $in_value; |
||
| 327 | break; |
||
| 328 | |||
| 329 | case 'Mysqli' || 'Sqlsrv': |
||
| 330 | $parsed_in[] = "?"; |
||
| 331 | $bind_values[] = $in_value; |
||
| 332 | break; |
||
| 333 | } |
||
| 334 | |||
| 335 | $k++; |
||
| 336 | } |
||
| 337 | |||
| 338 | $parsed_where[] = "$key IN (" . implode(", ", $parsed_in) . ")"; |
||
| 339 | } |
||
| 340 | else |
||
| 341 | { |
||
| 342 | switch ($driver) |
||
| 343 | { |
||
| 344 | case 'Oci8': |
||
| 345 | $parsed_where[] = "$key = :$k"; |
||
| 346 | $bind_values[":$k"] = $value; |
||
| 347 | break; |
||
| 348 | |||
| 349 | case 'Mysqli' || 'Sqlsrv': |
||
| 350 | $parsed_where[] = "$key = ?"; |
||
| 351 | $bind_values[] = $value; |
||
| 352 | break; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | $parsed_where = implode(" AND\r\n\t", $parsed_where); |
||
| 358 | |||
| 359 | $table = $this->entity->getTableName(); |
||
| 360 | |||
| 361 | $sql = "UPDATE {$table} \r\nSET \r\n\t$parsed_set \r\nWHERE \r\n\t$parsed_where"; |
||
| 362 | |||
| 363 | $this->lastQuery = $sql; |
||
| 364 | $this->lastValues = $bind_values; |
||
| 365 | |||
| 366 | return $this->getDb()->execute($sql, $bind_values); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Delete statement |
||
| 371 | * |
||
| 372 | * @param array $where |
||
| 373 | * |
||
| 374 | * @throws RuntimeException from internal execute() |
||
| 375 | * @throws SecurityException |
||
| 376 | * |
||
| 377 | * @return resource|object |
||
| 378 | */ |
||
| 379 | public function delete(Array $where) |
||
| 453 | } |
||
| 454 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths