| Conditions | 2 |
| Paths | 2 |
| Total Lines | 373 |
| Code Lines | 217 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 189 | public function buildConditionsProvider(): array |
||
| 190 | { |
||
| 191 | $conditions = [ |
||
| 192 | /* empty values */ |
||
| 193 | [['like', 'name', []], '0=1', []], |
||
| 194 | [['not like', 'name', []], '', []], |
||
| 195 | [['or like', 'name', []], '0=1', []], |
||
| 196 | [['or not like', 'name', []], '', []], |
||
| 197 | |||
| 198 | /* not */ |
||
| 199 | [['not', 'name'], 'NOT (name)', []], |
||
| 200 | [ |
||
| 201 | [ |
||
| 202 | 'not', |
||
| 203 | (new Query($this->db)) |
||
| 204 | ->select('exists') |
||
| 205 | ->from('some_table'), |
||
| 206 | ], |
||
| 207 | 'NOT ((SELECT [[exists]] FROM [[some_table]]))', [], |
||
| 208 | ], |
||
| 209 | |||
| 210 | /* and */ |
||
| 211 | [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []], |
||
| 212 | [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []], |
||
| 213 | [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]], |
||
| 214 | [ |
||
| 215 | [ |
||
| 216 | 'and', |
||
| 217 | ['expired' => false], |
||
| 218 | (new Query($this->db)) |
||
| 219 | ->select('count(*) > 1') |
||
| 220 | ->from('queue'), |
||
| 221 | ], |
||
| 222 | '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))', |
||
| 223 | [':qp0' => false], |
||
| 224 | ], |
||
| 225 | |||
| 226 | /* or */ |
||
| 227 | [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []], |
||
| 228 | [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []], |
||
| 229 | [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]], |
||
| 230 | |||
| 231 | /* between */ |
||
| 232 | [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 233 | [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 234 | [ |
||
| 235 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 236 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 237 | [], |
||
| 238 | ], |
||
| 239 | [ |
||
| 240 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 241 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 242 | [':qp0' => 123], |
||
| 243 | ], |
||
| 244 | [ |
||
| 245 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 246 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 247 | [], |
||
| 248 | ], |
||
| 249 | [ |
||
| 250 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 251 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 252 | [':qp0' => 123], |
||
| 253 | ], |
||
| 254 | [ |
||
| 255 | new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'), |
||
| 256 | ':qp0 BETWEEN [[create_time]] AND [[update_time]]', |
||
| 257 | [':qp0' => '2018-02-11'], |
||
| 258 | ], |
||
| 259 | [ |
||
| 260 | new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'), |
||
| 261 | ':qp0 NOT BETWEEN NOW() AND [[update_time]]', |
||
| 262 | [':qp0' => '2018-02-11'], |
||
| 263 | ], |
||
| 264 | [ |
||
| 265 | new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'), |
||
| 266 | 'NOW() BETWEEN [[create_time]] AND [[update_time]]', |
||
| 267 | [], |
||
| 268 | ], |
||
| 269 | [ |
||
| 270 | new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'), |
||
| 271 | 'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]', |
||
| 272 | [], |
||
| 273 | ], |
||
| 274 | [ |
||
| 275 | new BetweenColumnsCondition( |
||
| 276 | new Expression('NOW()'), |
||
| 277 | 'NOT BETWEEN', |
||
| 278 | (new Query($this->db)) |
||
| 279 | ->select('min_date') |
||
| 280 | ->from('some_table'), |
||
| 281 | 'max_date' |
||
| 282 | ), |
||
| 283 | 'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]', |
||
| 284 | [], |
||
| 285 | ], |
||
| 286 | |||
| 287 | /* in */ |
||
| 288 | [ |
||
| 289 | [ |
||
| 290 | 'in', |
||
| 291 | 'id', |
||
| 292 | [ |
||
| 293 | 1, |
||
| 294 | 2, |
||
| 295 | (new Query($this->db)) |
||
| 296 | ->select('three') |
||
| 297 | ->from('digits'), |
||
| 298 | ], |
||
| 299 | ], |
||
| 300 | '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))', |
||
| 301 | [':qp0' => 1, ':qp1' => 2], |
||
| 302 | ], |
||
| 303 | [ |
||
| 304 | ['not in', 'id', [1, 2, 3]], |
||
| 305 | '[[id]] NOT IN (:qp0, :qp1, :qp2)', |
||
| 306 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 307 | ], |
||
| 308 | [ |
||
| 309 | [ |
||
| 310 | 'in', |
||
| 311 | 'id', |
||
| 312 | (new Query($this->db)) |
||
| 313 | ->select('id') |
||
| 314 | ->from('users') |
||
| 315 | ->where(['active' => 1]), |
||
| 316 | ], |
||
| 317 | '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 318 | [':qp0' => 1], |
||
| 319 | ], |
||
| 320 | [ |
||
| 321 | [ |
||
| 322 | 'not in', |
||
| 323 | 'id', |
||
| 324 | (new Query($this->db)) |
||
| 325 | ->select('id') |
||
| 326 | ->from('users') |
||
| 327 | ->where(['active' => 1]), |
||
| 328 | ], |
||
| 329 | '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 330 | [':qp0' => 1], |
||
| 331 | ], |
||
| 332 | [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 333 | [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 334 | [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 335 | 'composite in' => [ |
||
| 336 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]], |
||
| 337 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
| 338 | [':qp0' => 1, ':qp1' => 'oy'], |
||
| 339 | ], |
||
| 340 | 'composite in (just one column)' => [ |
||
| 341 | ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]], |
||
| 342 | '[[id]] IN (:qp0, :qp1)', |
||
| 343 | [':qp0' => 1, ':qp1' => 2], |
||
| 344 | ], |
||
| 345 | 'composite in using array objects (just one column)' => [ |
||
| 346 | ['in', new TraversableObject(['id']), new TraversableObject([ |
||
| 347 | ['id' => 1, 'name' => 'Name1'], |
||
| 348 | ['id' => 2, 'name' => 'Name2'], |
||
| 349 | ])], |
||
| 350 | '[[id]] IN (:qp0, :qp1)', |
||
| 351 | [':qp0' => 1, ':qp1' => 2], |
||
| 352 | ], |
||
| 353 | |||
| 354 | /* in using array objects. */ |
||
| 355 | [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 356 | [ |
||
| 357 | ['in', 'id', new TraversableObject([1, 2, 3])], |
||
| 358 | '[[id]] IN (:qp0, :qp1, :qp2)', |
||
| 359 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 360 | ], |
||
| 361 | |||
| 362 | /* in using array objects containing null value */ |
||
| 363 | [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]], |
||
| 364 | [ |
||
| 365 | ['in', 'id', new TraversableObject([1, 2, null])], |
||
| 366 | '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2], |
||
| 367 | ], |
||
| 368 | |||
| 369 | /* not in using array object containing null value */ |
||
| 370 | [ |
||
| 371 | ['not in', 'id', new TraversableObject([1, null])], |
||
| 372 | '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1], |
||
| 373 | ], |
||
| 374 | [ |
||
| 375 | ['not in', 'id', new TraversableObject([1, 2, null])], |
||
| 376 | '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL', |
||
| 377 | [':qp0' => 1, ':qp1' => 2], |
||
| 378 | ], |
||
| 379 | |||
| 380 | /* in using array object containing only null value */ |
||
| 381 | [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []], |
||
| 382 | [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []], |
||
| 383 | 'composite in using array objects' => [ |
||
| 384 | ['in', new TraversableObject(['id', 'name']), new TraversableObject([ |
||
| 385 | ['id' => 1, 'name' => 'oy'], |
||
| 386 | ['id' => 2, 'name' => 'yo'], |
||
| 387 | ])], |
||
| 388 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 389 | [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'], |
||
| 390 | ], |
||
| 391 | |||
| 392 | /* in object conditions */ |
||
| 393 | [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 394 | [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 395 | [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 396 | [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 397 | [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 398 | [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 399 | |||
| 400 | /* exists */ |
||
| 401 | [ |
||
| 402 | [ |
||
| 403 | 'exists', |
||
| 404 | (new Query($this->db)) |
||
| 405 | ->select('id') |
||
| 406 | ->from('users') |
||
| 407 | ->where(['active' => 1]), |
||
| 408 | ], |
||
| 409 | 'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 410 | [':qp0' => 1], |
||
| 411 | ], |
||
| 412 | [ |
||
| 413 | [ |
||
| 414 | 'not exists', |
||
| 415 | (new Query($this->db)) |
||
| 416 | ->select('id') |
||
| 417 | ->from('users') |
||
| 418 | ->where(['active' => 1]), |
||
| 419 | ], |
||
| 420 | 'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1], |
||
| 421 | ], |
||
| 422 | |||
| 423 | /* simple conditions */ |
||
| 424 | [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']], |
||
| 425 | [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]], |
||
| 426 | [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']], |
||
| 427 | [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]], |
||
| 428 | [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']], |
||
| 429 | [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]], |
||
| 430 | [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']], |
||
| 431 | [ |
||
| 432 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')], |
||
| 433 | '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)', |
||
| 434 | [], |
||
| 435 | ], |
||
| 436 | [ |
||
| 437 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])], |
||
| 438 | '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)', |
||
| 439 | [':month' => 2], |
||
| 440 | ], |
||
| 441 | [ |
||
| 442 | [ |
||
| 443 | '=', |
||
| 444 | 'date', |
||
| 445 | (new Query($this->db)) |
||
| 446 | ->select('max(date)') |
||
| 447 | ->from('test') |
||
| 448 | ->where(['id' => 5]), |
||
| 449 | ], |
||
| 450 | '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)', |
||
| 451 | [':qp0' => 5], |
||
| 452 | ], |
||
| 453 | |||
| 454 | /* operand1 is Expression */ |
||
| 455 | [ |
||
| 456 | ['=', new Expression('date'), '2019-08-01'], |
||
| 457 | 'date = :qp0', |
||
| 458 | [':qp0' => '2019-08-01'], |
||
| 459 | ], |
||
| 460 | [ |
||
| 461 | [ |
||
| 462 | '=', |
||
| 463 | (new Query($this->db)) |
||
| 464 | ->select('COUNT(*)') |
||
| 465 | ->from('test') |
||
| 466 | ->where(['id' => 6]), |
||
| 467 | 0, |
||
| 468 | ], |
||
| 469 | '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1', |
||
| 470 | [':qp0' => 6, ':qp1' => 0], |
||
| 471 | ], |
||
| 472 | |||
| 473 | /* hash condition */ |
||
| 474 | [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 475 | [ |
||
| 476 | ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2], |
||
| 477 | '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)', |
||
| 478 | [':qp0' => 2], |
||
| 479 | ], |
||
| 480 | |||
| 481 | /* direct conditions */ |
||
| 482 | ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []], |
||
| 483 | [ |
||
| 484 | new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']), |
||
| 485 | 'a = CONCAT(col1, :param1)', |
||
| 486 | ['param1' => 'value1'], |
||
| 487 | ], |
||
| 488 | |||
| 489 | /* Expression with params as operand of 'not' */ |
||
| 490 | [ |
||
| 491 | ['not', new Expression('any_expression(:a)', [':a' => 1])], |
||
| 492 | 'NOT (any_expression(:a))', [':a' => 1], |
||
| 493 | ], |
||
| 494 | [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]], |
||
| 495 | ]; |
||
| 496 | |||
| 497 | $conditions = match ($this->db->getDriverName()) { |
||
| 498 | 'sqlsrv', 'sqlite' => array_merge($conditions, [ |
||
| 499 | [ |
||
| 500 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 501 | '(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))', |
||
| 502 | [':qp0' => 1, |
||
| 503 | ':qp1' => 'foo', |
||
| 504 | ':qp2' => 2, |
||
| 505 | ':qp3' => 'bar',], |
||
| 506 | ], |
||
| 507 | [ |
||
| 508 | ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 509 | '(([[id]] != :qp0 OR [[name]] != :qp1) AND ([[id]] != :qp2 OR [[name]] != :qp3))', |
||
| 510 | [':qp0' => 1, |
||
| 511 | ':qp1' => 'foo', |
||
| 512 | ':qp2' => 2, |
||
| 513 | ':qp3' => 'bar',], |
||
| 514 | ], |
||
| 515 | //[['in', ['id', 'name'], (new Query())->select(['id', 'name'])->from('users')->where(['active' => 1])], 'EXISTS (SELECT 1 FROM (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0) AS a WHERE a.[[id]] = [[id AND a.]]name[[ = ]]name`)', [':qp0' => 1] ], |
||
| 516 | //[ ['not in', ['id', 'name'], (new Query())->select(['id', 'name'])->from('users')->where(['active' => 1])], 'NOT EXISTS (SELECT 1 FROM (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0) AS a WHERE a.[[id]] = [[id]] AND a.[[name = ]]name`)', [':qp0' => 1] ], |
||
| 517 | ]), |
||
| 518 | default => array_merge($conditions, [ |
||
| 519 | [ |
||
| 520 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 521 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 522 | [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'], |
||
| 523 | ], |
||
| 524 | [ |
||
| 525 | ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 526 | '([[id]], [[name]]) NOT IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 527 | [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'], |
||
| 528 | ], |
||
| 529 | [ |
||
| 530 | [ |
||
| 531 | 'in', |
||
| 532 | ['id', 'name'], |
||
| 533 | (new Query($this->db)) |
||
| 534 | ->select(['id', 'name']) |
||
| 535 | ->from('users') |
||
| 536 | ->where(['active' => 1]), |
||
| 537 | ], |
||
| 538 | '([[id]], [[name]]) IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 539 | [':qp0' => 1], |
||
| 540 | ], |
||
| 541 | [ |
||
| 542 | [ |
||
| 543 | 'not in', |
||
| 544 | ['id', 'name'], |
||
| 545 | (new Query($this->db)) |
||
| 546 | ->select(['id', 'name']) |
||
| 547 | ->from('users') |
||
| 548 | ->where(['active' => 1]), |
||
| 549 | ], |
||
| 550 | '([[id]], [[name]]) NOT IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 551 | [':qp0' => 1], |
||
| 552 | ], |
||
| 553 | ]), |
||
| 554 | }; |
||
| 555 | |||
| 556 | /* adjust dbms specific escaping */ |
||
| 557 | foreach ($conditions as $i => $condition) { |
||
| 558 | $conditions[$i][1] = DbHelper::replaceQuotes($condition[1], $this->db->getDriverName()); |
||
| 559 | } |
||
| 560 | |||
| 561 | return $conditions; |
||
| 562 | } |
||
| 1075 |
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