Complex classes like Spec 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Spec, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 88 | class Spec |
||
| 89 | { |
||
| 90 | /** |
||
| 91 | * @param string $name |
||
| 92 | * @param array $arguments |
||
| 93 | * |
||
| 94 | * @return PlatformFunction |
||
| 95 | */ |
||
| 96 | public static function __callStatic($name, array $arguments = []) |
||
| 108 | |||
| 109 | /* |
||
| 110 | * Logic |
||
| 111 | */ |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return AndX |
||
| 115 | */ |
||
| 116 | public static function andX() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return OrX |
||
| 126 | */ |
||
| 127 | public static function orX() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param Filter $spec |
||
| 137 | * |
||
| 138 | * @return Not |
||
| 139 | */ |
||
| 140 | public static function not(Filter $spec) |
||
| 144 | |||
| 145 | /* |
||
| 146 | * Query modifier |
||
| 147 | */ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $field |
||
| 151 | * @param string $newAlias |
||
| 152 | * @param string $dqlAlias |
||
| 153 | * |
||
| 154 | * @return Join |
||
| 155 | */ |
||
| 156 | public static function join($field, $newAlias, $dqlAlias = null) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $field |
||
| 163 | * @param string $newAlias |
||
| 164 | * @param string $dqlAlias |
||
| 165 | * |
||
| 166 | * @return LeftJoin |
||
| 167 | */ |
||
| 168 | public static function leftJoin($field, $newAlias, $dqlAlias = null) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $field |
||
| 175 | * @param string $newAlias |
||
| 176 | * @param string $dqlAlias |
||
| 177 | * |
||
| 178 | * @return InnerJoin |
||
| 179 | */ |
||
| 180 | public static function innerJoin($field, $newAlias, $dqlAlias = null) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $field |
||
| 187 | * @param string $dqlAlias |
||
| 188 | * |
||
| 189 | * @return IndexBy |
||
| 190 | */ |
||
| 191 | public static function indexBy($field, $dqlAlias = null) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param int $count |
||
| 198 | * |
||
| 199 | * @return Limit |
||
| 200 | */ |
||
| 201 | public static function limit($count) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param int $count |
||
| 208 | * |
||
| 209 | * @return Offset |
||
| 210 | */ |
||
| 211 | public static function offset($count) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param int $sliceSize |
||
| 218 | * @param int $sliceNumber |
||
| 219 | * |
||
| 220 | * @return Slice |
||
| 221 | */ |
||
| 222 | public static function slice($sliceSize, $sliceNumber = 0) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $field |
||
| 229 | * @param string $order |
||
| 230 | * @param string|null $dqlAlias |
||
| 231 | * |
||
| 232 | * @return OrderBy |
||
| 233 | */ |
||
| 234 | public static function orderBy($field, $order = 'ASC', $dqlAlias = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $field |
||
| 241 | * @param string $dqlAlias |
||
| 242 | * |
||
| 243 | * @return GroupBy |
||
| 244 | */ |
||
| 245 | public static function groupBy($field, $dqlAlias = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return Distinct |
||
| 252 | */ |
||
| 253 | public static function distinct() |
||
| 257 | |||
| 258 | /* |
||
| 259 | * Selection |
||
| 260 | */ |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param mixed $field |
||
| 264 | * |
||
| 265 | * @return Select |
||
| 266 | */ |
||
| 267 | public static function select($field) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param mixed $field |
||
| 274 | * |
||
| 275 | * @return AddSelect |
||
| 276 | */ |
||
| 277 | public static function addSelect($field) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $dqlAlias |
||
| 284 | * |
||
| 285 | * @return SelectEntity |
||
| 286 | */ |
||
| 287 | public static function selectEntity($dqlAlias) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param Filter|Operand|string $expression |
||
| 294 | * @param string $alias |
||
| 295 | * |
||
| 296 | * @return SelectAs |
||
| 297 | */ |
||
| 298 | public static function selectAs($expression, $alias) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param Filter|Operand|string $expression |
||
| 305 | * @param string $alias |
||
| 306 | * |
||
| 307 | * @return SelectHiddenAs |
||
| 308 | */ |
||
| 309 | public static function selectHiddenAs($expression, $alias) |
||
| 313 | |||
| 314 | /* |
||
| 315 | * Result modifier |
||
| 316 | */ |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return AsArray |
||
| 320 | */ |
||
| 321 | public static function asArray() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return AsSingleScalar |
||
| 328 | */ |
||
| 329 | public static function asSingleScalar() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return AsScalar |
||
| 336 | */ |
||
| 337 | public static function asScalar() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 344 | * |
||
| 345 | * @return Cache |
||
| 346 | */ |
||
| 347 | public static function cache($cacheLifetime) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param int $roundSeconds How may seconds to round time |
||
| 354 | * |
||
| 355 | * @return RoundDateTime |
||
| 356 | */ |
||
| 357 | public static function roundDateTimeParams($roundSeconds) |
||
| 361 | |||
| 362 | /* |
||
| 363 | * Filters |
||
| 364 | */ |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param Operand|string $field |
||
| 368 | * @param string|null $dqlAlias |
||
| 369 | * |
||
| 370 | * @return IsNull |
||
| 371 | */ |
||
| 372 | public static function isNull($field, $dqlAlias = null) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param Operand|string $field |
||
| 379 | * @param string|null $dqlAlias |
||
| 380 | * |
||
| 381 | * @return IsNotNull |
||
| 382 | */ |
||
| 383 | public static function isNotNull($field, $dqlAlias = null) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Make sure the $field has a value equals to $value. |
||
| 390 | * |
||
| 391 | * @param string $field |
||
| 392 | * @param mixed $value |
||
| 393 | * @param string $dqlAlias |
||
| 394 | * |
||
| 395 | * @return In |
||
| 396 | */ |
||
| 397 | public static function in($field, $value, $dqlAlias = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param string $field |
||
| 404 | * @param mixed $value |
||
| 405 | * @param string $dqlAlias |
||
| 406 | * |
||
| 407 | * @return Not |
||
| 408 | */ |
||
| 409 | public static function notIn($field, $value, $dqlAlias = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param Operand|string $field |
||
| 416 | * @param Operand|mixed $value |
||
| 417 | * @param string|null $dqlAlias |
||
| 418 | * |
||
| 419 | * @return Comparison |
||
| 420 | */ |
||
| 421 | public static function eq($field, $value, $dqlAlias = null) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param Operand|string $field |
||
| 428 | * @param Operand|mixed $value |
||
| 429 | * @param string|null $dqlAlias |
||
| 430 | * |
||
| 431 | * @return Comparison |
||
| 432 | */ |
||
| 433 | public static function neq($field, $value, $dqlAlias = null) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param Operand|string $field |
||
| 440 | * @param Operand|mixed $value |
||
| 441 | * @param string|null $dqlAlias |
||
| 442 | * |
||
| 443 | * @return Comparison |
||
| 444 | */ |
||
| 445 | public static function lt($field, $value, $dqlAlias = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param Operand|string $field |
||
| 452 | * @param Operand|mixed $value |
||
| 453 | * @param string|null $dqlAlias |
||
| 454 | * |
||
| 455 | * @return Comparison |
||
| 456 | */ |
||
| 457 | public static function lte($field, $value, $dqlAlias = null) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param Operand|string $field |
||
| 464 | * @param Operand|mixed $value |
||
| 465 | * @param string|null $dqlAlias |
||
| 466 | * |
||
| 467 | * @return Comparison |
||
| 468 | */ |
||
| 469 | public static function gt($field, $value, $dqlAlias = null) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param Operand|string $field |
||
| 476 | * @param Operand|mixed $value |
||
| 477 | * @param string|null $dqlAlias |
||
| 478 | * |
||
| 479 | * @return Comparison |
||
| 480 | */ |
||
| 481 | public static function gte($field, $value, $dqlAlias = null) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param Operand|string $field |
||
| 488 | * @param string $value |
||
| 489 | * @param string $format |
||
| 490 | * @param string|null $dqlAlias |
||
| 491 | * |
||
| 492 | * @return Like |
||
| 493 | */ |
||
| 494 | public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param string $value |
||
| 501 | * @param string|null $dqlAlias |
||
| 502 | * |
||
| 503 | * @return InstanceOfX |
||
| 504 | */ |
||
| 505 | public static function instanceOfX($value, $dqlAlias = null) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param Operand|string $value |
||
| 512 | * @param Operand|string $field |
||
| 513 | * @param string|null $dqlAlias |
||
| 514 | * |
||
| 515 | * @return MemberOfX |
||
| 516 | */ |
||
| 517 | public static function memberOfX($value, $field, $dqlAlias = null) |
||
| 521 | |||
| 522 | /* |
||
| 523 | * Specifications |
||
| 524 | */ |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param Filter|QueryModifier $spec |
||
| 528 | * |
||
| 529 | * @return CountOf |
||
| 530 | */ |
||
| 531 | public static function countOf($spec) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param Filter $spec |
||
| 538 | * |
||
| 539 | * @return Having |
||
| 540 | */ |
||
| 541 | public static function having($spec) |
||
| 545 | |||
| 546 | /* |
||
| 547 | * Operands |
||
| 548 | */ |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param string $fieldName |
||
| 552 | * @param string|null $dqlAlias |
||
| 553 | * |
||
| 554 | * @return Field |
||
| 555 | */ |
||
| 556 | public static function field($fieldName, $dqlAlias = null) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param mixed $value |
||
| 563 | * @param int|string|null $valueType |
||
| 564 | * |
||
| 565 | * @return Value |
||
| 566 | */ |
||
| 567 | public static function value($value, $valueType = null) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param array $values |
||
| 574 | * @param int|string|null $valueType |
||
| 575 | * |
||
| 576 | * @return Values |
||
| 577 | */ |
||
| 578 | public static function values($values, $valueType = null) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @param string $value |
||
| 585 | * @param string $format |
||
| 586 | * |
||
| 587 | * @return LikePattern |
||
| 588 | */ |
||
| 589 | public static function likePattern($value, $format = LikePattern::CONTAINS) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @param Operand|string $field |
||
| 596 | * |
||
| 597 | * @return CountDistinct |
||
| 598 | */ |
||
| 599 | public static function countDistinct($field) |
||
| 603 | |||
| 604 | /* |
||
| 605 | * Arithmetic operands |
||
| 606 | */ |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param Operand|string $field |
||
| 610 | * @param Operand|mixed $value |
||
| 611 | * |
||
| 612 | * @return Addition |
||
| 613 | */ |
||
| 614 | public static function add($field, $value) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @param Operand|string $field |
||
| 621 | * @param Operand|mixed $value |
||
| 622 | * |
||
| 623 | * @return Subtraction |
||
| 624 | */ |
||
| 625 | public static function sub($field, $value) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @param Operand|string $field |
||
| 632 | * @param Operand|mixed $value |
||
| 633 | * |
||
| 634 | * @return Multiplication |
||
| 635 | */ |
||
| 636 | public static function mul($field, $value) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @param Operand|string $field |
||
| 643 | * @param Operand|mixed $value |
||
| 644 | * |
||
| 645 | * @return Division |
||
| 646 | */ |
||
| 647 | public static function div($field, $value) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @param Operand|string $field |
||
| 654 | * @param Operand|mixed $value |
||
| 655 | * |
||
| 656 | * @return Modulo |
||
| 657 | */ |
||
| 658 | public static function mod($field, $value) |
||
| 662 | |||
| 663 | /* |
||
| 664 | * Bitwise operands |
||
| 665 | */ |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param Operand|string $field |
||
| 669 | * @param Operand|mixed $value |
||
| 670 | * |
||
| 671 | * @return BitAnd |
||
| 672 | */ |
||
| 673 | public static function bAnd($field, $value) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @param Operand|string $field |
||
| 680 | * @param Operand|mixed $value |
||
| 681 | * |
||
| 682 | * @return BitOr |
||
| 683 | */ |
||
| 684 | public static function bOr($field, $value) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * @param Operand|string $field |
||
| 691 | * @param Operand|mixed $value |
||
| 692 | * |
||
| 693 | * @return BitXor |
||
| 694 | */ |
||
| 695 | public static function bXor($field, $value) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @param Operand|string $field |
||
| 702 | * @param Operand|mixed $value |
||
| 703 | * |
||
| 704 | * @return BitLeftShift |
||
| 705 | */ |
||
| 706 | public static function bLs($field, $value) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @param Operand|string $field |
||
| 713 | * @param Operand|mixed $value |
||
| 714 | * |
||
| 715 | * @return BitRightShift |
||
| 716 | */ |
||
| 717 | public static function bRs($field, $value) |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @param Operand|string $field |
||
| 724 | * |
||
| 725 | * @return BitNot |
||
| 726 | */ |
||
| 727 | public static function bNot($field) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Call DQL function. |
||
| 734 | * |
||
| 735 | * Usage: |
||
| 736 | * Spec::fun('CURRENT_DATE') |
||
| 737 | * Spec::fun('DATE_DIFF', $date1, $date2) |
||
| 738 | * Spec::fun('DATE_DIFF', [$date1, $date2]) |
||
| 739 | * |
||
| 740 | * @param string $functionName |
||
| 741 | * @param mixed $arguments |
||
| 742 | * |
||
| 743 | * @return PlatformFunction |
||
| 744 | */ |
||
| 745 | public static function fun($functionName, $arguments = []) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * @param string $alias |
||
| 759 | * |
||
| 760 | * @return Alias |
||
| 761 | */ |
||
| 762 | public static function alias($alias) |
||
| 766 | } |
||
| 767 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.