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 |
||
| 189 | { |
||
| 190 | return new IndexBy($field, $context); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param int $count |
||
| 195 | * |
||
| 196 | * @return Limit |
||
| 197 | */ |
||
| 198 | public static function limit(int $count): Limit |
||
| 199 | { |
||
| 200 | return new Limit($count); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param int $count |
||
| 205 | * |
||
| 206 | * @return Offset |
||
| 207 | */ |
||
| 208 | public static function offset(int $count): Offset |
||
| 209 | { |
||
| 210 | return new Offset($count); |
||
| 211 | } |
||
| 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 |
||
| 220 | { |
||
| 221 | return new Slice($sliceSize, $sliceNumber); |
||
| 222 | } |
||
| 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 |
||
| 232 | { |
||
| 233 | return new OrderBy($field, $order, $context); |
||
| 234 | } |
||
| 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 |
||
| 243 | { |
||
| 244 | return new GroupBy($field, $context); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return Distinct |
||
| 249 | */ |
||
| 250 | public static function distinct(): Distinct |
||
| 251 | { |
||
| 252 | return new Distinct(); |
||
| 253 | } |
||
| 254 | |||
| 255 | // Selection |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Selection|string ...$fields |
||
| 259 | * |
||
| 260 | * @return Select |
||
| 261 | */ |
||
| 262 | public static function select(...$fields): Select |
||
| 263 | { |
||
| 264 | return new Select(...$fields); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param Selection|string ...$fields |
||
| 269 | * |
||
| 270 | * @return AddSelect |
||
| 271 | */ |
||
| 272 | public static function addSelect(...$fields): AddSelect |
||
| 273 | { |
||
| 274 | return new AddSelect(...$fields); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $class |
||
| 279 | * @param Operand|mixed ...$arguments |
||
| 280 | * |
||
| 281 | * @phpstan-param class-string $class |
||
| 282 | * |
||
| 283 | * @return SelectNew |
||
| 284 | */ |
||
| 285 | public static function selectNew(string $class, ...$arguments): SelectNew |
||
| 286 | { |
||
| 287 | return new SelectNew($class, ...$arguments); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $dqlAliasInContext |
||
| 292 | * |
||
| 293 | * @return SelectEntity |
||
| 294 | */ |
||
| 295 | public static function selectEntity(string $dqlAliasInContext): SelectEntity |
||
| 296 | { |
||
| 297 | return new SelectEntity($dqlAliasInContext); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param Filter|Operand|string $expression |
||
| 302 | * @param string $newAlias |
||
| 303 | * |
||
| 304 | * @return SelectAs |
||
| 305 | */ |
||
| 306 | public static function selectAs($expression, string $newAlias): SelectAs |
||
| 307 | { |
||
| 308 | return new SelectAs($expression, $newAlias); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param Filter|Operand|string $expression |
||
| 313 | * @param string $newAlias |
||
| 314 | * |
||
| 315 | * @return SelectHiddenAs |
||
| 316 | */ |
||
| 317 | public static function selectHiddenAs($expression, string $newAlias): SelectHiddenAs |
||
| 318 | { |
||
| 319 | return new SelectHiddenAs($expression, $newAlias); |
||
| 320 | } |
||
| 321 | |||
| 322 | // Result modifier |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return AsArray |
||
| 326 | */ |
||
| 327 | public static function asArray(): AsArray |
||
| 328 | { |
||
| 329 | return new AsArray(); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return AsSingleScalar |
||
| 334 | */ |
||
| 335 | public static function asSingleScalar(): AsSingleScalar |
||
| 336 | { |
||
| 337 | return new AsSingleScalar(); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return AsScalar |
||
| 342 | */ |
||
| 343 | public static function asScalar(): AsScalar |
||
| 344 | { |
||
| 345 | return new AsScalar(); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 350 | * |
||
| 351 | * @return Cache |
||
| 352 | */ |
||
| 353 | public static function cache(int $cacheLifetime): Cache |
||
| 354 | { |
||
| 355 | return new Cache($cacheLifetime); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param int $roundSeconds How may seconds to round time |
||
| 360 | * |
||
| 361 | * @return RoundDateTime |
||
| 362 | */ |
||
| 363 | public static function roundDateTimeParams(int $roundSeconds): RoundDateTime |
||
| 364 | { |
||
| 365 | return new RoundDateTime($roundSeconds); |
||
| 366 | } |
||
| 367 | |||
| 368 | // Filters |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param Operand|string $field |
||
| 372 | * @param string|null $context |
||
| 373 | * |
||
| 374 | * @return IsNull |
||
| 375 | */ |
||
| 376 | public static function isNull($field, ?string $context = null): IsNull |
||
| 377 | { |
||
| 378 | return new IsNull($field, $context); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param Operand|string $field |
||
| 383 | * @param string|null $context |
||
| 384 | * |
||
| 385 | * @return IsNotNull |
||
| 386 | */ |
||
| 387 | public static function isNotNull($field, ?string $context = null): IsNotNull |
||
| 388 | { |
||
| 389 | return new IsNotNull($field, $context); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Make sure the $field has a value equals to $value. |
||
| 394 | * |
||
| 395 | * @param Operand|string $field |
||
| 396 | * @param Operand|mixed $value |
||
| 397 | * @param string|null $context |
||
| 398 | * |
||
| 399 | * @return In |
||
| 400 | */ |
||
| 401 | public static function in($field, $value, ?string $context = null): In |
||
| 402 | { |
||
| 403 | return new In($field, $value, $context); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param Operand|string $field |
||
| 408 | * @param Operand|mixed $value |
||
| 409 | * @param string|null $context |
||
| 410 | * |
||
| 411 | * @return Not |
||
| 412 | */ |
||
| 413 | public static function notIn($field, $value, ?string $context = null): Not |
||
| 414 | { |
||
| 415 | return new Not(new In($field, $value, $context)); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param Operand|string $field |
||
| 420 | * @param Operand|mixed $value |
||
| 421 | * @param string|null $context |
||
| 422 | * |
||
| 423 | * @return Equals |
||
| 424 | */ |
||
| 425 | public static function eq($field, $value, ?string $context = null): Equals |
||
| 426 | { |
||
| 427 | return new Equals($field, $value, $context); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param Operand|string $field |
||
| 432 | * @param Operand|mixed $value |
||
| 433 | * @param string|null $context |
||
| 434 | * |
||
| 435 | * @return NotEquals |
||
| 436 | */ |
||
| 437 | public static function neq($field, $value, ?string $context = null): NotEquals |
||
| 438 | { |
||
| 439 | return new NotEquals($field, $value, $context); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param Operand|string $field |
||
| 444 | * @param Operand|mixed $value |
||
| 445 | * @param string|null $context |
||
| 446 | * |
||
| 447 | * @return LessThan |
||
| 448 | */ |
||
| 449 | public static function lt($field, $value, ?string $context = null): LessThan |
||
| 450 | { |
||
| 451 | return new LessThan($field, $value, $context); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param Operand|string $field |
||
| 456 | * @param Operand|mixed $value |
||
| 457 | * @param string|null $context |
||
| 458 | * |
||
| 459 | * @return LessOrEqualThan |
||
| 460 | */ |
||
| 461 | public static function lte($field, $value, ?string $context = null): LessOrEqualThan |
||
| 462 | { |
||
| 463 | return new LessOrEqualThan($field, $value, $context); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param Operand|string $field |
||
| 468 | * @param Operand|mixed $value |
||
| 469 | * @param string|null $context |
||
| 470 | * |
||
| 471 | * @return GreaterThan |
||
| 472 | */ |
||
| 473 | public static function gt($field, $value, ?string $context = null): GreaterThan |
||
| 474 | { |
||
| 475 | return new GreaterThan($field, $value, $context); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param Operand|string $field |
||
| 480 | * @param Operand|mixed $value |
||
| 481 | * @param string|null $context |
||
| 482 | * |
||
| 483 | * @return GreaterOrEqualThan |
||
| 484 | */ |
||
| 485 | public static function gte($field, $value, ?string $context = null): GreaterOrEqualThan |
||
| 486 | { |
||
| 487 | return new GreaterOrEqualThan($field, $value, $context); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param Operand|string $field |
||
| 492 | * @param LikePattern|string $value |
||
| 493 | * @param string $format |
||
| 494 | * @param string|null $context |
||
| 495 | * |
||
| 496 | * @return Like |
||
| 497 | */ |
||
| 498 | public static function like($field, $value, string $format = Like::CONTAINS, ?string $context = null): Like |
||
| 499 | { |
||
| 500 | return new Like($field, $value, $format, $context); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param string $value |
||
| 505 | * @param string|null $context |
||
| 506 | * |
||
| 507 | * @return InstanceOfX |
||
| 508 | */ |
||
| 509 | public static function instanceOfX($value, ?string $context = null): InstanceOfX |
||
| 510 | { |
||
| 511 | return new InstanceOfX($value, $context); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param Operand|mixed $value |
||
| 516 | * @param Operand|string $field |
||
| 517 | * @param string|null $context |
||
| 518 | * |
||
| 519 | * @return MemberOfX |
||
| 520 | */ |
||
| 521 | public static function memberOfX($value, $field, ?string $context = null): MemberOfX |
||
| 522 | { |
||
| 523 | return new MemberOfX($value, $field, $context); |
||
| 524 | } |
||
| 525 | |||
| 526 | // Specifications |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param Filter|QueryModifier $spec |
||
| 530 | * |
||
| 531 | * @return CountOf |
||
| 532 | */ |
||
| 533 | public static function countOf($spec): CountOf |
||
| 534 | { |
||
| 535 | return new CountOf($spec); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param Filter $spec |
||
| 540 | * |
||
| 541 | * @return Having |
||
| 542 | */ |
||
| 543 | public static function having(Filter $spec): Having |
||
| 544 | { |
||
| 545 | return new Having($spec); |
||
| 546 | } |
||
| 547 | |||
| 548 | // Operands |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param string $fieldName |
||
| 552 | * @param string|null $context |
||
| 553 | * |
||
| 554 | * @return Field |
||
| 555 | */ |
||
| 556 | public static function field(string $fieldName, ?string $context = null): Field |
||
| 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): Value |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param mixed[] $values |
||
| 574 | * @param int|string|null $valueType |
||
| 575 | * |
||
| 576 | * @return Values |
||
| 577 | */ |
||
| 578 | public static function values(array $values, $valueType = null): Values |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @param string $value |
||
| 585 | * @param string $format |
||
| 586 | * |
||
| 587 | * @return LikePattern |
||
| 588 | */ |
||
| 589 | public static function likePattern(string $value, string $format = LikePattern::CONTAINS): LikePattern |
||
| 593 | |||
| 594 | // Arithmetic operands |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @param Operand|string $field |
||
| 598 | * @param Operand|mixed $value |
||
| 599 | * |
||
| 600 | * @return Addition |
||
| 601 | */ |
||
| 602 | public static function add($field, $value): Addition |
||
| 603 | { |
||
| 604 | return new Addition($field, $value); |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param Operand|string $field |
||
| 609 | * @param Operand|mixed $value |
||
| 610 | * |
||
| 611 | * @return Subtraction |
||
| 612 | */ |
||
| 613 | public static function sub($field, $value): Subtraction |
||
| 614 | { |
||
| 615 | return new Subtraction($field, $value); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @param Operand|string $field |
||
| 620 | * @param Operand|mixed $value |
||
| 621 | * |
||
| 622 | * @return Multiplication |
||
| 623 | */ |
||
| 624 | public static function mul($field, $value): Multiplication |
||
| 625 | { |
||
| 626 | return new Multiplication($field, $value); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @param Operand|string $field |
||
| 631 | * @param Operand|mixed $value |
||
| 632 | * |
||
| 633 | * @return Division |
||
| 634 | */ |
||
| 635 | public static function div($field, $value): Division |
||
| 639 | |||
| 640 | // Platform functions |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @param Operand|string $field |
||
| 644 | * @param bool $distinct |
||
| 645 | * |
||
| 646 | * @return Count |
||
| 647 | */ |
||
| 648 | public static function COUNT($field, bool $distinct = false): Count |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @param Operand|string $field |
||
| 655 | * @param bool $distinct |
||
| 656 | * |
||
| 657 | * @return Avg |
||
| 658 | */ |
||
| 659 | public static function AVG($field, bool $distinct = false): Avg |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param Operand|string $field |
||
| 666 | * @param bool $distinct |
||
| 667 | * |
||
| 668 | * @return Min |
||
| 669 | */ |
||
| 670 | public static function MIN($field, bool $distinct = false): Min |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param Operand|string $field |
||
| 677 | * @param bool $distinct |
||
| 678 | * |
||
| 679 | * @return Max |
||
| 680 | */ |
||
| 681 | public static function MAX($field, bool $distinct = false): Max |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @param Operand|string $field |
||
| 688 | * @param bool $distinct |
||
| 689 | * |
||
| 690 | * @return Sum |
||
| 691 | */ |
||
| 692 | public static function SUM($field, bool $distinct = false): Sum |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Add the number of days to 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 DateAdd |
||
| 705 | */ |
||
| 706 | public static function DATE_ADD($date, $value, string $unit): DateAdd |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Substract the number of days from a given date. (Supported units are YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND). |
||
| 713 | * |
||
| 714 | * @param \DateTimeInterface|string|Operand $date |
||
| 715 | * @param int|Operand $value |
||
| 716 | * @param string $unit |
||
| 717 | * |
||
| 718 | * @return DateSub |
||
| 719 | */ |
||
| 720 | public static function DATE_SUB($date, $value, string $unit): DateSub |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Trim the string by the given trim char, defaults to whitespaces. |
||
| 727 | * |
||
| 728 | * @param Operand|string $string |
||
| 729 | * @param string $mode |
||
| 730 | * @param string $characters |
||
| 731 | */ |
||
| 732 | public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Call DQL function. |
||
| 739 | * |
||
| 740 | * Usage: |
||
| 741 | * Spec::fun('CURRENT_DATE') |
||
| 742 | * Spec::fun('DATE_DIFF', $date1, $date2) |
||
| 743 | * |
||
| 744 | * @param string $functionName |
||
| 745 | * @param mixed ...$arguments |
||
| 746 | * |
||
| 747 | * @return PlatformFunction |
||
| 748 | */ |
||
| 749 | public static function fun(string $functionName, ...$arguments): PlatformFunction |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @param string $alias |
||
| 756 | * |
||
| 757 | * @return Alias |
||
| 758 | */ |
||
| 759 | public static function alias(string $alias): Alias |
||
| 763 | } |
||
| 764 |