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 |
||
| 99 | class Spec |
||
| 100 | { |
||
| 101 | /** |
||
| 102 | * @param string $functionName |
||
| 103 | * @param mixed[] $arguments |
||
| 104 | * |
||
| 105 | * @return PlatformFunction |
||
| 106 | */ |
||
| 107 | public static function __callStatic(string $functionName, array $arguments = []): PlatformFunction |
||
| 108 | { |
||
| 109 | return self::fun($functionName, ...$arguments); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Logic |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param Filter|QueryModifier ...$specs |
||
| 116 | * |
||
| 117 | * @return AndX |
||
| 118 | */ |
||
| 119 | public static function andX(...$specs): AndX |
||
| 120 | { |
||
| 121 | return new AndX(...$specs); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Filter|QueryModifier ...$specs |
||
| 126 | * |
||
| 127 | * @return OrX |
||
| 128 | */ |
||
| 129 | public static function orX(...$specs): OrX |
||
| 130 | { |
||
| 131 | return new OrX(...$specs); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param Filter $spec |
||
| 136 | * |
||
| 137 | * @return Not |
||
| 138 | */ |
||
| 139 | public static function not(Filter $spec): Not |
||
| 140 | { |
||
| 141 | return new Not($spec); |
||
| 142 | } |
||
| 143 | |||
| 144 | // Query modifier |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $field |
||
| 148 | * @param string $newAlias |
||
| 149 | * @param string|null $context |
||
| 150 | * |
||
| 151 | * @return Join |
||
| 152 | */ |
||
| 153 | public static function join(string $field, string $newAlias, ?string $context = null): Join |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $field |
||
| 160 | * @param string $newAlias |
||
| 161 | * @param string|null $context |
||
| 162 | * |
||
| 163 | * @return LeftJoin |
||
| 164 | */ |
||
| 165 | public static function leftJoin(string $field, string $newAlias, ?string $context = null): LeftJoin |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $field |
||
| 172 | * @param string $newAlias |
||
| 173 | * @param string|null $context |
||
| 174 | * |
||
| 175 | * @return InnerJoin |
||
| 176 | */ |
||
| 177 | public static function innerJoin(string $field, string $newAlias, ?string $context = null): InnerJoin |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param Field|string $field |
||
| 184 | * @param string|null $context |
||
| 185 | * |
||
| 186 | * @return IndexBy |
||
| 187 | */ |
||
| 188 | public static function indexBy($field, ?string $context = null): IndexBy |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param int $count |
||
| 195 | * |
||
| 196 | * @return Limit |
||
| 197 | */ |
||
| 198 | public static function limit(int $count): Limit |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param int $count |
||
| 205 | * |
||
| 206 | * @return Offset |
||
| 207 | */ |
||
| 208 | public static function offset(int $count): Offset |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param int $sliceSize |
||
| 215 | * @param int $sliceNumber |
||
| 216 | * |
||
| 217 | * @return Slice |
||
| 218 | */ |
||
| 219 | public static function slice(int $sliceSize, int $sliceNumber = 0): Slice |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param Field|Alias|string $field |
||
| 226 | * @param string $order |
||
| 227 | * @param string|null $context |
||
| 228 | * |
||
| 229 | * @return OrderBy |
||
| 230 | */ |
||
| 231 | public static function orderBy($field, string $order = 'ASC', ?string $context = null): OrderBy |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param Field|Alias|string $field |
||
| 238 | * @param string|null $context |
||
| 239 | * |
||
| 240 | * @return GroupBy |
||
| 241 | */ |
||
| 242 | public static function groupBy($field, ?string $context = null): GroupBy |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return Distinct |
||
| 249 | */ |
||
| 250 | public static function distinct(): Distinct |
||
| 254 | |||
| 255 | // Selection |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Selection|string ...$fields |
||
| 259 | * |
||
| 260 | * @return Select |
||
| 261 | */ |
||
| 262 | public static function select(...$fields): Select |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param Selection|string ...$fields |
||
| 269 | * |
||
| 270 | * @return AddSelect |
||
| 271 | */ |
||
| 272 | public static function addSelect(...$fields): AddSelect |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $context |
||
| 279 | * |
||
| 280 | * @return SelectEntity |
||
| 281 | */ |
||
| 282 | public static function selectEntity(string $context): SelectEntity |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param Filter|Operand|string $expression |
||
| 289 | * @param string $alias |
||
| 290 | * |
||
| 291 | * @return SelectAs |
||
| 292 | */ |
||
| 293 | public static function selectAs($expression, string $alias): SelectAs |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param Filter|Operand|string $expression |
||
| 300 | * @param string $alias |
||
| 301 | * |
||
| 302 | * @return SelectHiddenAs |
||
| 303 | */ |
||
| 304 | public static function selectHiddenAs($expression, string $alias): SelectHiddenAs |
||
| 308 | |||
| 309 | // Result modifier |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return AsArray |
||
| 313 | */ |
||
| 314 | public static function asArray(): AsArray |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return AsSingleScalar |
||
| 321 | */ |
||
| 322 | public static function asSingleScalar(): AsSingleScalar |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return AsScalar |
||
| 329 | */ |
||
| 330 | public static function asScalar(): AsScalar |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 337 | * |
||
| 338 | * @return Cache |
||
| 339 | */ |
||
| 340 | public static function cache(int $cacheLifetime): Cache |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param int $roundSeconds How may seconds to round time |
||
| 347 | * |
||
| 348 | * @return RoundDateTime |
||
| 349 | */ |
||
| 350 | public static function roundDateTimeParams(int $roundSeconds): RoundDateTime |
||
| 354 | |||
| 355 | // Filters |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param Operand|string $field |
||
| 359 | * @param string|null $context |
||
| 360 | * |
||
| 361 | * @return IsNull |
||
| 362 | */ |
||
| 363 | public static function isNull($field, ?string $context = null): IsNull |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param Operand|string $field |
||
| 370 | * @param string|null $context |
||
| 371 | * |
||
| 372 | * @return IsNotNull |
||
| 373 | */ |
||
| 374 | public static function isNotNull($field, ?string $context = null): IsNotNull |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Make sure the $field has a value equals to $value. |
||
| 381 | * |
||
| 382 | * @param Operand|string $field |
||
| 383 | * @param Operand|mixed $value |
||
| 384 | * @param string|null $context |
||
| 385 | * |
||
| 386 | * @return In |
||
| 387 | */ |
||
| 388 | public static function in($field, $value, ?string $context = null): In |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param Operand|string $field |
||
| 395 | * @param Operand|mixed $value |
||
| 396 | * @param string|null $context |
||
| 397 | * |
||
| 398 | * @return Not |
||
| 399 | */ |
||
| 400 | public static function notIn($field, $value, ?string $context = null): Not |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param Operand|string $field |
||
| 407 | * @param Operand|mixed $value |
||
| 408 | * @param string|null $context |
||
| 409 | * |
||
| 410 | * @return Equals |
||
| 411 | */ |
||
| 412 | public static function eq($field, $value, ?string $context = null): Equals |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param Operand|string $field |
||
| 419 | * @param Operand|mixed $value |
||
| 420 | * @param string|null $context |
||
| 421 | * |
||
| 422 | * @return NotEquals |
||
| 423 | */ |
||
| 424 | public static function neq($field, $value, ?string $context = null): NotEquals |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param Operand|string $field |
||
| 431 | * @param Operand|mixed $value |
||
| 432 | * @param string|null $context |
||
| 433 | * |
||
| 434 | * @return LessThan |
||
| 435 | */ |
||
| 436 | public static function lt($field, $value, ?string $context = null): LessThan |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param Operand|string $field |
||
| 443 | * @param Operand|mixed $value |
||
| 444 | * @param string|null $context |
||
| 445 | * |
||
| 446 | * @return LessOrEqualThan |
||
| 447 | */ |
||
| 448 | public static function lte($field, $value, ?string $context = null): LessOrEqualThan |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param Operand|string $field |
||
| 455 | * @param Operand|mixed $value |
||
| 456 | * @param string|null $context |
||
| 457 | * |
||
| 458 | * @return GreaterThan |
||
| 459 | */ |
||
| 460 | public static function gt($field, $value, ?string $context = null): GreaterThan |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param Operand|string $field |
||
| 467 | * @param Operand|mixed $value |
||
| 468 | * @param string|null $context |
||
| 469 | * |
||
| 470 | * @return GreaterOrEqualThan |
||
| 471 | */ |
||
| 472 | public static function gte($field, $value, ?string $context = null): GreaterOrEqualThan |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param Operand|string $field |
||
| 479 | * @param LikePattern|string $value |
||
| 480 | * @param string $format |
||
| 481 | * @param string|null $context |
||
| 482 | * |
||
| 483 | * @return Like |
||
| 484 | */ |
||
| 485 | public static function like($field, $value, string $format = Like::CONTAINS, ?string $context = null): Like |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $value |
||
| 492 | * @param string|null $context |
||
| 493 | * |
||
| 494 | * @return InstanceOfX |
||
| 495 | */ |
||
| 496 | public static function instanceOfX($value, ?string $context = null): InstanceOfX |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param Operand|mixed $value |
||
| 503 | * @param Operand|string $field |
||
| 504 | * @param string|null $context |
||
| 505 | * |
||
| 506 | * @return MemberOfX |
||
| 507 | */ |
||
| 508 | public static function memberOfX($value, $field, ?string $context = null): MemberOfX |
||
| 512 | |||
| 513 | // Specifications |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param Filter|QueryModifier $spec |
||
| 517 | * |
||
| 518 | * @return CountOf |
||
| 519 | */ |
||
| 520 | public static function countOf($spec): CountOf |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param Filter $spec |
||
| 527 | * |
||
| 528 | * @return Having |
||
| 529 | */ |
||
| 530 | public static function having(Filter $spec): Having |
||
| 534 | |||
| 535 | // Operands |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param string $fieldName |
||
| 539 | * @param string|null $context |
||
| 540 | * |
||
| 541 | * @return Field |
||
| 542 | */ |
||
| 543 | public static function field(string $fieldName, ?string $context = null): Field |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param mixed $value |
||
| 550 | * @param int|string|null $valueType |
||
| 551 | * |
||
| 552 | * @return Value |
||
| 553 | */ |
||
| 554 | public static function value($value, $valueType = null): Value |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param mixed[] $values |
||
| 561 | * @param int|string|null $valueType |
||
| 562 | * |
||
| 563 | * @return Values |
||
| 564 | */ |
||
| 565 | public static function values(array $values, $valueType = null): Values |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param string $value |
||
| 572 | * @param string $format |
||
| 573 | * |
||
| 574 | * @return LikePattern |
||
| 575 | */ |
||
| 576 | public static function likePattern(string $value, string $format = LikePattern::CONTAINS): LikePattern |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param Operand|string $field |
||
| 583 | * |
||
| 584 | * @return CountDistinct |
||
| 585 | */ |
||
| 586 | public static function countDistinct($field): CountDistinct |
||
| 590 | |||
| 591 | // Arithmetic operands |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @param Operand|string $field |
||
| 595 | * @param Operand|mixed $value |
||
| 596 | * |
||
| 597 | * @return Addition |
||
| 598 | */ |
||
| 599 | public static function add($field, $value): Addition |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param Operand|string $field |
||
| 606 | * @param Operand|mixed $value |
||
| 607 | * |
||
| 608 | * @return Subtraction |
||
| 609 | */ |
||
| 610 | public static function sub($field, $value): Subtraction |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param Operand|string $field |
||
| 617 | * @param Operand|mixed $value |
||
| 618 | * |
||
| 619 | * @return Multiplication |
||
| 620 | */ |
||
| 621 | public static function mul($field, $value): Multiplication |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param Operand|string $field |
||
| 628 | * @param Operand|mixed $value |
||
| 629 | * |
||
| 630 | * @return Division |
||
| 631 | */ |
||
| 632 | public static function div($field, $value): Division |
||
| 636 | |||
| 637 | // Platform functions |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Trim the string by the given trim char, defaults to whitespaces. |
||
| 641 | * |
||
| 642 | * @param Operand|string $string |
||
| 643 | * @param string $mode |
||
| 644 | * @param string $characters |
||
| 645 | */ |
||
| 646 | public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Add the number of days to a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND). |
||
| 653 | * |
||
| 654 | * @param \DateTimeInterface|string|Operand $date |
||
| 655 | * @param int|Operand $value |
||
| 656 | * @param string $unit |
||
| 657 | * |
||
| 658 | * @return DateAdd |
||
| 659 | */ |
||
| 660 | public static function DATE_ADD($date, $value, string $unit): DateAdd |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Substract the number of days from a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND). |
||
| 667 | * |
||
| 668 | * @param \DateTimeInterface|string|Operand $date |
||
| 669 | * @param int|Operand $value |
||
| 670 | * @param string $unit |
||
| 671 | * |
||
| 672 | * @return DateSub |
||
| 673 | */ |
||
| 674 | public static function DATE_SUB($date, $value, string $unit): DateSub |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Call DQL function. |
||
| 681 | * |
||
| 682 | * Usage: |
||
| 683 | * Spec::fun('CURRENT_DATE') |
||
| 684 | * Spec::fun('DATE_DIFF', $date1, $date2) |
||
| 685 | * |
||
| 686 | * @param string $functionName |
||
| 687 | * @param mixed ...$arguments |
||
| 688 | * |
||
| 689 | * @return PlatformFunction |
||
| 690 | */ |
||
| 691 | public static function fun(string $functionName, ...$arguments): PlatformFunction |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @param string $alias |
||
| 698 | * |
||
| 699 | * @return Alias |
||
| 700 | */ |
||
| 701 | public static function alias(string $alias): Alias |
||
| 705 | } |
||
| 706 |