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 |
||
| 98 | class Spec |
||
| 99 | { |
||
| 100 | /** |
||
| 101 | * @param string $functionName |
||
| 102 | * @param mixed[] $arguments |
||
| 103 | * |
||
| 104 | * @return PlatformFunction |
||
| 105 | */ |
||
| 106 | public static function __callStatic(string $functionName, array $arguments = []): PlatformFunction |
||
| 107 | { |
||
| 108 | return self::fun($functionName, ...$arguments); |
||
| 109 | } |
||
| 110 | |||
| 111 | // Logic |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param Filter|QueryModifier ...$specs |
||
| 115 | * |
||
| 116 | * @return AndX |
||
| 117 | */ |
||
| 118 | public static function andX(...$specs): AndX |
||
| 119 | { |
||
| 120 | return new AndX(...$specs); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Filter|QueryModifier ...$specs |
||
| 125 | * |
||
| 126 | * @return OrX |
||
| 127 | */ |
||
| 128 | public static function orX(...$specs): OrX |
||
| 129 | { |
||
| 130 | return new OrX(...$specs); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Filter $spec |
||
| 135 | * |
||
| 136 | * @return Not |
||
| 137 | */ |
||
| 138 | public static function not(Filter $spec): Not |
||
| 142 | |||
| 143 | // Query modifier |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $field |
||
| 147 | * @param string $newAlias |
||
| 148 | * @param string|null $context |
||
| 149 | * |
||
| 150 | * @return Join |
||
| 151 | */ |
||
| 152 | public static function join(string $field, string $newAlias, ?string $context = null): Join |
||
| 153 | { |
||
| 154 | return new Join($field, $newAlias, $context); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $field |
||
| 159 | * @param string $newAlias |
||
| 160 | * @param string|null $context |
||
| 161 | * |
||
| 162 | * @return LeftJoin |
||
| 163 | */ |
||
| 164 | public static function leftJoin(string $field, string $newAlias, ?string $context = null): LeftJoin |
||
| 165 | { |
||
| 166 | return new LeftJoin($field, $newAlias, $context); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $field |
||
| 171 | * @param string $newAlias |
||
| 172 | * @param string|null $context |
||
| 173 | * |
||
| 174 | * @return InnerJoin |
||
| 175 | */ |
||
| 176 | public static function innerJoin(string $field, string $newAlias, ?string $context = null): InnerJoin |
||
| 177 | { |
||
| 178 | return new InnerJoin($field, $newAlias, $context); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param Field|string $field |
||
| 183 | * @param string|null $context |
||
| 184 | * |
||
| 185 | * @return IndexBy |
||
| 186 | */ |
||
| 187 | public static function indexBy($field, ?string $context = null): IndexBy |
||
| 188 | { |
||
| 189 | return new IndexBy($field, $context); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $count |
||
| 194 | * |
||
| 195 | * @return Limit |
||
| 196 | */ |
||
| 197 | public static function limit(int $count): Limit |
||
| 198 | { |
||
| 199 | return new Limit($count); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $count |
||
| 204 | * |
||
| 205 | * @return Offset |
||
| 206 | */ |
||
| 207 | public static function offset(int $count): Offset |
||
| 208 | { |
||
| 209 | return new Offset($count); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param int $sliceSize |
||
| 214 | * @param int $sliceNumber |
||
| 215 | * |
||
| 216 | * @return Slice |
||
| 217 | */ |
||
| 218 | public static function slice(int $sliceSize, int $sliceNumber = 0): Slice |
||
| 219 | { |
||
| 220 | return new Slice($sliceSize, $sliceNumber); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Field|Alias|string $field |
||
| 225 | * @param string $order |
||
| 226 | * @param string|null $context |
||
| 227 | * |
||
| 228 | * @return OrderBy |
||
| 229 | */ |
||
| 230 | public static function orderBy($field, string $order = 'ASC', ?string $context = null): OrderBy |
||
| 231 | { |
||
| 232 | return new OrderBy($field, $order, $context); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param Field|Alias|string $field |
||
| 237 | * @param string|null $context |
||
| 238 | * |
||
| 239 | * @return GroupBy |
||
| 240 | */ |
||
| 241 | public static function groupBy($field, ?string $context = null): GroupBy |
||
| 242 | { |
||
| 243 | return new GroupBy($field, $context); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return Distinct |
||
| 248 | */ |
||
| 249 | public static function distinct(): Distinct |
||
| 250 | { |
||
| 251 | return new Distinct(); |
||
| 252 | } |
||
| 253 | |||
| 254 | // Selection |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param Selection|string ...$fields |
||
| 258 | * |
||
| 259 | * @return Select |
||
| 260 | */ |
||
| 261 | public static function select(...$fields): Select |
||
| 262 | { |
||
| 263 | return new Select(...$fields); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param Selection|string ...$fields |
||
| 268 | * |
||
| 269 | * @return AddSelect |
||
| 270 | */ |
||
| 271 | public static function addSelect(...$fields): AddSelect |
||
| 272 | { |
||
| 273 | return new AddSelect(...$fields); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $context |
||
| 278 | * |
||
| 279 | * @return SelectEntity |
||
| 280 | */ |
||
| 281 | public static function selectEntity(string $context): SelectEntity |
||
| 282 | { |
||
| 283 | return new SelectEntity($context); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param Filter|Operand|string $expression |
||
| 288 | * @param string $alias |
||
| 289 | * |
||
| 290 | * @return SelectAs |
||
| 291 | */ |
||
| 292 | public static function selectAs($expression, string $alias): SelectAs |
||
| 293 | { |
||
| 294 | return new SelectAs($expression, $alias); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param Filter|Operand|string $expression |
||
| 299 | * @param string $alias |
||
| 300 | * |
||
| 301 | * @return SelectHiddenAs |
||
| 302 | */ |
||
| 303 | public static function selectHiddenAs($expression, string $alias): SelectHiddenAs |
||
| 304 | { |
||
| 305 | return new SelectHiddenAs($expression, $alias); |
||
| 306 | } |
||
| 307 | |||
| 308 | // Result modifier |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return AsArray |
||
| 312 | */ |
||
| 313 | public static function asArray(): AsArray |
||
| 314 | { |
||
| 315 | return new AsArray(); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return AsSingleScalar |
||
| 320 | */ |
||
| 321 | public static function asSingleScalar(): AsSingleScalar |
||
| 322 | { |
||
| 323 | return new AsSingleScalar(); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return AsScalar |
||
| 328 | */ |
||
| 329 | public static function asScalar(): AsScalar |
||
| 330 | { |
||
| 331 | return new AsScalar(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 336 | * |
||
| 337 | * @return Cache |
||
| 338 | */ |
||
| 339 | public static function cache(int $cacheLifetime): Cache |
||
| 340 | { |
||
| 341 | return new Cache($cacheLifetime); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param int $roundSeconds How may seconds to round time |
||
| 346 | * |
||
| 347 | * @return RoundDateTime |
||
| 348 | */ |
||
| 349 | public static function roundDateTimeParams(int $roundSeconds): RoundDateTime |
||
| 350 | { |
||
| 351 | return new RoundDateTime($roundSeconds); |
||
| 352 | } |
||
| 353 | |||
| 354 | // Filters |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param Operand|string $field |
||
| 358 | * @param string|null $context |
||
| 359 | * |
||
| 360 | * @return IsNull |
||
| 361 | */ |
||
| 362 | public static function isNull($field, ?string $context = null): IsNull |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param Operand|string $field |
||
| 369 | * @param string|null $context |
||
| 370 | * |
||
| 371 | * @return IsNotNull |
||
| 372 | */ |
||
| 373 | public static function isNotNull($field, ?string $context = null): IsNotNull |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Make sure the $field has a value equals to $value. |
||
| 380 | * |
||
| 381 | * @param Operand|string $field |
||
| 382 | * @param Operand|mixed $value |
||
| 383 | * @param string|null $context |
||
| 384 | * |
||
| 385 | * @return In |
||
| 386 | */ |
||
| 387 | public static function in($field, $value, ?string $context = null): In |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Operand|string $field |
||
| 394 | * @param Operand|mixed $value |
||
| 395 | * @param string|null $context |
||
| 396 | * |
||
| 397 | * @return Not |
||
| 398 | */ |
||
| 399 | public static function notIn($field, $value, ?string $context = null): Not |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param Operand|string $field |
||
| 406 | * @param Operand|mixed $value |
||
| 407 | * @param string|null $context |
||
| 408 | * |
||
| 409 | * @return Equals |
||
| 410 | */ |
||
| 411 | public static function eq($field, $value, ?string $context = null): Equals |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param Operand|string $field |
||
| 418 | * @param Operand|mixed $value |
||
| 419 | * @param string|null $context |
||
| 420 | * |
||
| 421 | * @return NotEquals |
||
| 422 | */ |
||
| 423 | public static function neq($field, $value, ?string $context = null): NotEquals |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param Operand|string $field |
||
| 430 | * @param Operand|mixed $value |
||
| 431 | * @param string|null $context |
||
| 432 | * |
||
| 433 | * @return LessThan |
||
| 434 | */ |
||
| 435 | public static function lt($field, $value, ?string $context = null): LessThan |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param Operand|string $field |
||
| 442 | * @param Operand|mixed $value |
||
| 443 | * @param string|null $context |
||
| 444 | * |
||
| 445 | * @return LessOrEqualThan |
||
| 446 | */ |
||
| 447 | public static function lte($field, $value, ?string $context = null): LessOrEqualThan |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param Operand|string $field |
||
| 454 | * @param Operand|mixed $value |
||
| 455 | * @param string|null $context |
||
| 456 | * |
||
| 457 | * @return GreaterThan |
||
| 458 | */ |
||
| 459 | public static function gt($field, $value, ?string $context = null): GreaterThan |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param Operand|string $field |
||
| 466 | * @param Operand|mixed $value |
||
| 467 | * @param string|null $context |
||
| 468 | * |
||
| 469 | * @return GreaterOrEqualThan |
||
| 470 | */ |
||
| 471 | public static function gte($field, $value, ?string $context = null): GreaterOrEqualThan |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param Operand|string $field |
||
| 478 | * @param LikePattern|string $value |
||
| 479 | * @param string $format |
||
| 480 | * @param string|null $context |
||
| 481 | * |
||
| 482 | * @return Like |
||
| 483 | */ |
||
| 484 | public static function like($field, $value, string $format = Like::CONTAINS, ?string $context = null): Like |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $value |
||
| 491 | * @param string|null $context |
||
| 492 | * |
||
| 493 | * @return InstanceOfX |
||
| 494 | */ |
||
| 495 | public static function instanceOfX($value, ?string $context = null): InstanceOfX |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param Operand|mixed $value |
||
| 502 | * @param Operand|string $field |
||
| 503 | * @param string|null $context |
||
| 504 | * |
||
| 505 | * @return MemberOfX |
||
| 506 | */ |
||
| 507 | public static function memberOfX($value, $field, ?string $context = null): MemberOfX |
||
| 511 | |||
| 512 | // Specifications |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param Filter|QueryModifier $spec |
||
| 516 | * |
||
| 517 | * @return CountOf |
||
| 518 | */ |
||
| 519 | public static function countOf($spec): CountOf |
||
| 520 | { |
||
| 521 | return new CountOf($spec); |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param Filter $spec |
||
| 526 | * |
||
| 527 | * @return Having |
||
| 528 | */ |
||
| 529 | public static function having(Filter $spec): Having |
||
| 530 | { |
||
| 531 | return new Having($spec); |
||
| 532 | } |
||
| 533 | |||
| 534 | // Operands |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $fieldName |
||
| 538 | * @param string|null $context |
||
| 539 | * |
||
| 540 | * @return Field |
||
| 541 | */ |
||
| 542 | public static function field(string $fieldName, ?string $context = null): Field |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param mixed $value |
||
| 549 | * @param int|string|null $valueType |
||
| 550 | * |
||
| 551 | * @return Value |
||
| 552 | */ |
||
| 553 | public static function value($value, $valueType = null): Value |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @param mixed[] $values |
||
| 560 | * @param int|string|null $valueType |
||
| 561 | * |
||
| 562 | * @return Values |
||
| 563 | */ |
||
| 564 | public static function values(array $values, $valueType = null): Values |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param string $value |
||
| 571 | * @param string $format |
||
| 572 | * |
||
| 573 | * @return LikePattern |
||
| 574 | */ |
||
| 575 | public static function likePattern(string $value, string $format = LikePattern::CONTAINS): LikePattern |
||
| 579 | |||
| 580 | // Arithmetic operands |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @param Operand|string $field |
||
| 584 | * @param Operand|mixed $value |
||
| 585 | * |
||
| 586 | * @return Addition |
||
| 587 | */ |
||
| 588 | public static function add($field, $value): Addition |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @param Operand|string $field |
||
| 595 | * @param Operand|mixed $value |
||
| 596 | * |
||
| 597 | * @return Subtraction |
||
| 598 | */ |
||
| 599 | public static function sub($field, $value): Subtraction |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param Operand|string $field |
||
| 606 | * @param Operand|mixed $value |
||
| 607 | * |
||
| 608 | * @return Multiplication |
||
| 609 | */ |
||
| 610 | public static function mul($field, $value): Multiplication |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param Operand|string $field |
||
| 617 | * @param Operand|mixed $value |
||
| 618 | * |
||
| 619 | * @return Division |
||
| 620 | */ |
||
| 621 | public static function div($field, $value): Division |
||
| 625 | |||
| 626 | // Platform functions |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param Operand|string $field |
||
| 630 | * @param bool $distinct |
||
| 631 | * |
||
| 632 | * @return Count |
||
| 633 | */ |
||
| 634 | public static function COUNT($field, bool $distinct = false): Count |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @param Operand|string $field |
||
| 641 | * @param bool $distinct |
||
| 642 | * |
||
| 643 | * @return Avg |
||
| 644 | */ |
||
| 645 | public static function AVG($field, bool $distinct = false): Avg |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param Operand|string $field |
||
| 652 | * @param bool $distinct |
||
| 653 | * |
||
| 654 | * @return Min |
||
| 655 | */ |
||
| 656 | public static function MIN($field, bool $distinct = false): Min |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param Operand|string $field |
||
| 663 | * @param bool $distinct |
||
| 664 | * |
||
| 665 | * @return Max |
||
| 666 | */ |
||
| 667 | public static function MAX($field, bool $distinct = false): Max |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param Operand|string $field |
||
| 674 | * @param bool $distinct |
||
| 675 | * |
||
| 676 | * @return Sum |
||
| 677 | */ |
||
| 678 | public static function SUM($field, bool $distinct = false): Sum |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Add the number of days to a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND). |
||
| 685 | * |
||
| 686 | * @param \DateTimeInterface|string|Operand $date |
||
| 687 | * @param int|Operand $value |
||
| 688 | * @param string $unit |
||
| 689 | * |
||
| 690 | * @return DateAdd |
||
| 691 | */ |
||
| 692 | public static function DATE_ADD($date, $value, string $unit): DateAdd |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Substract the number of days from a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND). |
||
| 699 | * |
||
| 700 | * @param \DateTimeInterface|string|Operand $date |
||
| 701 | * @param int|Operand $value |
||
| 702 | * @param string $unit |
||
| 703 | * |
||
| 704 | * @return DateSub |
||
| 705 | */ |
||
| 706 | public static function DATE_SUB($date, $value, string $unit): DateSub |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Trim the string by the given trim char, defaults to whitespaces. |
||
| 713 | * |
||
| 714 | * @param Operand|string $string |
||
| 715 | * @param string $mode |
||
| 716 | * @param string $characters |
||
| 717 | */ |
||
| 718 | public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Call DQL function. |
||
| 725 | * |
||
| 726 | * Usage: |
||
| 727 | * Spec::fun('CURRENT_DATE') |
||
| 728 | * Spec::fun('DATE_DIFF', $date1, $date2) |
||
| 729 | * |
||
| 730 | * @param string $functionName |
||
| 731 | * @param mixed ...$arguments |
||
| 732 | * |
||
| 733 | * @return PlatformFunction |
||
| 734 | */ |
||
| 735 | public static function fun(string $functionName, ...$arguments): PlatformFunction |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @param string $alias |
||
| 742 | * |
||
| 743 | * @return Alias |
||
| 744 | */ |
||
| 745 | public static function alias(string $alias): Alias |
||
| 749 | } |
||
| 750 |