| Total Complexity | 66 |
| Total Lines | 414 |
| 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 | * Entity instance |
||
| 26 | * |
||
| 27 | * @var Entity |
||
| 28 | */ |
||
| 29 | private $entity; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns the entity |
||
| 33 | * |
||
| 34 | * @return Entity |
||
| 35 | */ |
||
| 36 | public function getEntity() |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * |
||
| 44 | * @param Entity $entity |
||
| 45 | * @param boolean $auto_connect |
||
| 46 | */ |
||
| 47 | public function __construct(Entity $entity, $auto_connect = true) |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Select statement |
||
| 55 | * |
||
| 56 | * @param array $where |
||
| 57 | * |
||
| 58 | * @return array With all results |
||
| 59 | */ |
||
| 60 | public function select(Array $where = []) |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Insert statement |
||
| 140 | * |
||
| 141 | * @param array $data |
||
| 142 | * |
||
| 143 | * @throws RuntimeException from internal execute() |
||
| 144 | * @throws LogicException |
||
| 145 | * |
||
| 146 | * @return resource|object |
||
| 147 | */ |
||
| 148 | public function insert(Array $data) |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Update statement |
||
| 214 | * |
||
| 215 | * @param array $set |
||
| 216 | * @param array $where |
||
| 217 | * |
||
| 218 | * @throws RuntimeException from internal execute() |
||
| 219 | * @throws LogicException |
||
| 220 | * @throws SecurityException |
||
| 221 | * |
||
| 222 | * @return resource|object |
||
| 223 | */ |
||
| 224 | public function update(Array $set, Array $where) |
||
| 225 | { |
||
| 226 | $parsed_set = []; |
||
| 227 | |||
| 228 | if (!count($set)) |
||
| 229 | throw new \LogicException("You cannot update rows without SET clause"); |
||
| 230 | |||
| 231 | if (!count($where)) |
||
| 232 | throw new SecurityException("You cannot update rows without WHERE clause!"); |
||
|
|
|||
| 233 | |||
| 234 | $bind_values = []; |
||
| 235 | |||
| 236 | $driver = $this->getDriver()->getDriverName(); |
||
| 237 | |||
| 238 | $k = 0; |
||
| 239 | |||
| 240 | foreach ($set as $key => $value) |
||
| 241 | { |
||
| 242 | $k++; |
||
| 243 | |||
| 244 | if (is_null($value)) |
||
| 245 | $parsed_set[] = "$key = NULL"; |
||
| 246 | elseif ($value instanceof SQLFunction) |
||
| 247 | $parsed_set[] = "$key = " . $value->getStatement(); |
||
| 248 | elseif (is_array($value)) |
||
| 249 | { |
||
| 250 | $parsed_in = []; |
||
| 251 | |||
| 252 | foreach ($value as $in_value) |
||
| 253 | { |
||
| 254 | switch ($driver) |
||
| 255 | { |
||
| 256 | case 'Oci8': |
||
| 257 | |||
| 258 | # [POSSIBLE BUG] - To Future revision (What about non-string values ?) |
||
| 259 | if (is_string($in_value)) |
||
| 260 | $parsed_in[] = ":$k"; |
||
| 261 | |||
| 262 | $bind_values[":$k"] = $in_value; |
||
| 263 | break; |
||
| 264 | |||
| 265 | case 'Mysqli' || 'Sqlsrv': |
||
| 266 | $parsed_in[] = "?"; |
||
| 267 | $bind_values[] = $in_value; |
||
| 268 | break; |
||
| 269 | } |
||
| 270 | |||
| 271 | $k++; |
||
| 272 | } |
||
| 273 | |||
| 274 | $parsed_set[] = "$key IN (" . implode(", ", $parsed_in) . ")"; |
||
| 275 | } |
||
| 276 | else |
||
| 277 | { |
||
| 278 | switch ($driver) |
||
| 279 | { |
||
| 280 | case 'Oci8': |
||
| 281 | $parsed_set[] = "$key = :$k"; |
||
| 282 | $bind_values[":$k"] = $value; |
||
| 283 | break; |
||
| 284 | |||
| 285 | case 'Mysqli' || 'Sqlsrv': |
||
| 286 | $parsed_set[] = "$key = ?"; |
||
| 287 | $bind_values[] = $value; |
||
| 288 | break; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | $parsed_set = implode(",\r\n\t", $parsed_set); |
||
| 294 | |||
| 295 | $parsed_where = []; |
||
| 296 | |||
| 297 | foreach ($where as $key => $value) |
||
| 298 | { |
||
| 299 | $k++; |
||
| 300 | |||
| 301 | if (is_null($value)) |
||
| 302 | $parsed_where[] = "$key IS NULL"; |
||
| 303 | elseif ($value instanceof SQLFunction) |
||
| 304 | $parsed_where[] = "$key = " . $value->getStatement(); |
||
| 305 | elseif (is_array($value)) |
||
| 306 | { |
||
| 307 | $parsed_in = []; |
||
| 308 | |||
| 309 | foreach ($value as $in_value) |
||
| 310 | { |
||
| 311 | switch ($driver) |
||
| 312 | { |
||
| 313 | case 'Oci8': |
||
| 314 | $parsed_in[] = ":$k"; |
||
| 315 | $bind_values[":$k"] = $in_value; |
||
| 316 | break; |
||
| 317 | |||
| 318 | case 'Mysqli' || 'Sqlsrv': |
||
| 319 | $parsed_in[] = "?"; |
||
| 320 | $bind_values[] = $in_value; |
||
| 321 | break; |
||
| 322 | } |
||
| 323 | |||
| 324 | $k++; |
||
| 325 | } |
||
| 326 | |||
| 327 | $parsed_where[] = "$key IN (" . implode(", ", $parsed_in) . ")"; |
||
| 328 | } |
||
| 329 | else |
||
| 330 | { |
||
| 331 | switch ($driver) |
||
| 332 | { |
||
| 333 | case 'Oci8': |
||
| 334 | $parsed_where[] = "$key = :$k"; |
||
| 335 | $bind_values[":$k"] = $value; |
||
| 336 | break; |
||
| 337 | |||
| 338 | case 'Mysqli' || 'Sqlsrv': |
||
| 339 | $parsed_where[] = "$key = ?"; |
||
| 340 | $bind_values[] = $value; |
||
| 341 | break; |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | $parsed_where = implode(" AND\r\n\t", $parsed_where); |
||
| 347 | |||
| 348 | $table = $this->entity->getTableName(); |
||
| 349 | |||
| 350 | $sql = "UPDATE {$table} \r\nSET \r\n\t$parsed_set \r\nWHERE \r\n\t$parsed_where"; |
||
| 351 | |||
| 352 | return $this->getDriver()->getDb()->execute($sql, $bind_values); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Delete statement |
||
| 357 | * |
||
| 358 | * @param array $where |
||
| 359 | * |
||
| 360 | * @throws RuntimeException from internal execute() |
||
| 361 | * @throws SecurityException |
||
| 362 | * |
||
| 363 | * @return resource|object |
||
| 364 | */ |
||
| 365 | public function delete(Array $where) |
||
| 436 | } |
||
| 437 | } |
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