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 |
||
| 100 | class Spec |
||
| 101 | { |
||
| 102 | /** |
||
| 103 | * @param string $functionName |
||
| 104 | * @param mixed[] $arguments |
||
| 105 | * |
||
| 106 | * @return PlatformFunction |
||
| 107 | */ |
||
| 108 | public static function __callStatic($functionName, array $arguments = []) |
||
| 109 | { |
||
| 110 | return self::fun($functionName, ...$arguments); |
||
| 111 | } |
||
| 112 | |||
| 113 | // Logic |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param Filter|QueryModifier ...$specs |
||
| 117 | * |
||
| 118 | * @return AndX |
||
| 119 | */ |
||
| 120 | public static function andX(...$specs) |
||
| 121 | { |
||
| 122 | return new AndX(...$specs); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param Filter|QueryModifier ...$specs |
||
| 127 | * |
||
| 128 | * @return OrX |
||
| 129 | */ |
||
| 130 | public static function orX(...$specs) |
||
| 131 | { |
||
| 132 | return new OrX(...$specs); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param Filter $spec |
||
| 137 | * |
||
| 138 | * @return Not |
||
| 139 | */ |
||
| 140 | public static function not(Filter $spec) |
||
| 141 | { |
||
| 142 | return new Not($spec); |
||
| 143 | } |
||
| 144 | |||
| 145 | // Query modifier |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $field |
||
| 149 | * @param string $newAlias |
||
| 150 | * @param string|null $dqlAlias |
||
| 151 | * |
||
| 152 | * @return Join |
||
| 153 | */ |
||
| 154 | public static function join($field, $newAlias, $dqlAlias = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $field |
||
| 161 | * @param string $newAlias |
||
| 162 | * @param string|null $dqlAlias |
||
| 163 | * |
||
| 164 | * @return LeftJoin |
||
| 165 | */ |
||
| 166 | public static function leftJoin($field, $newAlias, $dqlAlias = null) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $field |
||
| 173 | * @param string $newAlias |
||
| 174 | * @param string|null $dqlAlias |
||
| 175 | * |
||
| 176 | * @return InnerJoin |
||
| 177 | */ |
||
| 178 | public static function innerJoin($field, $newAlias, $dqlAlias = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param Field|string $field |
||
| 185 | * @param string|null $dqlAlias |
||
| 186 | * |
||
| 187 | * @return IndexBy |
||
| 188 | */ |
||
| 189 | public static function indexBy($field, $dqlAlias = null) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param int $count |
||
| 196 | * |
||
| 197 | * @return Limit |
||
| 198 | */ |
||
| 199 | public static function limit($count) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param int $count |
||
| 206 | * |
||
| 207 | * @return Offset |
||
| 208 | */ |
||
| 209 | public static function offset($count) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param int $sliceSize |
||
| 216 | * @param int $sliceNumber |
||
| 217 | * |
||
| 218 | * @return Slice |
||
| 219 | */ |
||
| 220 | public static function slice($sliceSize, $sliceNumber = 0) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param Field|Alias|string $field |
||
| 227 | * @param string $order |
||
| 228 | * @param string|null $dqlAlias |
||
| 229 | * |
||
| 230 | * @return OrderBy |
||
| 231 | */ |
||
| 232 | public static function orderBy($field, $order = 'ASC', $dqlAlias = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param Field|Alias|string $field |
||
| 239 | * @param string|null $dqlAlias |
||
| 240 | * |
||
| 241 | * @return GroupBy |
||
| 242 | */ |
||
| 243 | public static function groupBy($field, $dqlAlias = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return Distinct |
||
| 250 | */ |
||
| 251 | public static function distinct() |
||
| 252 | { |
||
| 253 | return new Distinct(); |
||
| 254 | } |
||
| 255 | |||
| 256 | // Selection |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param Selection|string ...$fields |
||
| 260 | * |
||
| 261 | * @return Select |
||
| 262 | */ |
||
| 263 | public static function select(...$fields) |
||
| 264 | { |
||
| 265 | return new Select(...$fields); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param Selection|string ...$fields |
||
| 270 | * |
||
| 271 | * @return AddSelect |
||
| 272 | */ |
||
| 273 | public static function addSelect(...$fields) |
||
| 274 | { |
||
| 275 | return new AddSelect(...$fields); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $dqlAlias |
||
| 280 | * |
||
| 281 | * @return SelectEntity |
||
| 282 | */ |
||
| 283 | public static function selectEntity($dqlAlias) |
||
| 284 | { |
||
| 285 | return new SelectEntity($dqlAlias); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param Filter|Operand|string $expression |
||
| 290 | * @param string $alias |
||
| 291 | * |
||
| 292 | * @return SelectAs |
||
| 293 | */ |
||
| 294 | public static function selectAs($expression, $alias) |
||
| 295 | { |
||
| 296 | return new SelectAs($expression, $alias); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param Filter|Operand|string $expression |
||
| 301 | * @param string $alias |
||
| 302 | * |
||
| 303 | * @return SelectHiddenAs |
||
| 304 | */ |
||
| 305 | public static function selectHiddenAs($expression, $alias) |
||
| 306 | { |
||
| 307 | return new SelectHiddenAs($expression, $alias); |
||
| 308 | } |
||
| 309 | |||
| 310 | // Result modifier |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return AsArray |
||
| 314 | */ |
||
| 315 | public static function asArray() |
||
| 316 | { |
||
| 317 | return new AsArray(); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return AsSingleScalar |
||
| 322 | */ |
||
| 323 | public static function asSingleScalar() |
||
| 324 | { |
||
| 325 | return new AsSingleScalar(); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return AsScalar |
||
| 330 | */ |
||
| 331 | public static function asScalar() |
||
| 332 | { |
||
| 333 | return new AsScalar(); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 338 | * |
||
| 339 | * @return Cache |
||
| 340 | */ |
||
| 341 | public static function cache($cacheLifetime) |
||
| 342 | { |
||
| 343 | return new Cache($cacheLifetime); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param int $roundSeconds How may seconds to round time |
||
| 348 | * |
||
| 349 | * @return RoundDateTime |
||
| 350 | */ |
||
| 351 | public static function roundDateTimeParams($roundSeconds) |
||
| 352 | { |
||
| 353 | return new RoundDateTime($roundSeconds); |
||
| 354 | } |
||
| 355 | |||
| 356 | // Filters |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param Operand|string $field |
||
| 360 | * @param string|null $dqlAlias |
||
| 361 | * |
||
| 362 | * @return IsNull |
||
| 363 | */ |
||
| 364 | public static function isNull($field, $dqlAlias = null) |
||
| 365 | { |
||
| 366 | return new IsNull($field, $dqlAlias); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param Operand|string $field |
||
| 371 | * @param string|null $dqlAlias |
||
| 372 | * |
||
| 373 | * @return IsNotNull |
||
| 374 | */ |
||
| 375 | public static function isNotNull($field, $dqlAlias = null) |
||
| 376 | { |
||
| 377 | return new IsNotNull($field, $dqlAlias); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Make sure the $field has a value equals to $value. |
||
| 382 | * |
||
| 383 | * @param Operand|string $field |
||
| 384 | * @param Operand|mixed $value |
||
| 385 | * @param string|null $dqlAlias |
||
| 386 | * |
||
| 387 | * @return In |
||
| 388 | */ |
||
| 389 | public static function in($field, $value, $dqlAlias = null) |
||
| 390 | { |
||
| 391 | return new In($field, $value, $dqlAlias); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param Operand|string $field |
||
| 396 | * @param Operand|mixed $value |
||
| 397 | * @param string|null $dqlAlias |
||
| 398 | * |
||
| 399 | * @return Not |
||
| 400 | */ |
||
| 401 | public static function notIn($field, $value, $dqlAlias = null) |
||
| 402 | { |
||
| 403 | return new Not(new In($field, $value, $dqlAlias)); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param Operand|string $field |
||
| 408 | * @param Operand|mixed $value |
||
| 409 | * @param string|null $dqlAlias |
||
| 410 | * |
||
| 411 | * @return Equals |
||
| 412 | */ |
||
| 413 | public static function eq($field, $value, $dqlAlias = null) |
||
| 414 | { |
||
| 415 | return new Equals($field, $value, $dqlAlias); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param Operand|string $field |
||
| 420 | * @param Operand|mixed $value |
||
| 421 | * @param string|null $dqlAlias |
||
| 422 | * |
||
| 423 | * @return NotEquals |
||
| 424 | */ |
||
| 425 | public static function neq($field, $value, $dqlAlias = null) |
||
| 426 | { |
||
| 427 | return new NotEquals($field, $value, $dqlAlias); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param Operand|string $field |
||
| 432 | * @param Operand|mixed $value |
||
| 433 | * @param string|null $dqlAlias |
||
| 434 | * |
||
| 435 | * @return LessThan |
||
| 436 | */ |
||
| 437 | public static function lt($field, $value, $dqlAlias = null) |
||
| 438 | { |
||
| 439 | return new LessThan($field, $value, $dqlAlias); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param Operand|string $field |
||
| 444 | * @param Operand|mixed $value |
||
| 445 | * @param string|null $dqlAlias |
||
| 446 | * |
||
| 447 | * @return LessOrEqualThan |
||
| 448 | */ |
||
| 449 | public static function lte($field, $value, $dqlAlias = null) |
||
| 450 | { |
||
| 451 | return new LessOrEqualThan($field, $value, $dqlAlias); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param Operand|string $field |
||
| 456 | * @param Operand|mixed $value |
||
| 457 | * @param string|null $dqlAlias |
||
| 458 | * |
||
| 459 | * @return GreaterThan |
||
| 460 | */ |
||
| 461 | public static function gt($field, $value, $dqlAlias = null) |
||
| 462 | { |
||
| 463 | return new GreaterThan($field, $value, $dqlAlias); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param Operand|string $field |
||
| 468 | * @param Operand|mixed $value |
||
| 469 | * @param string|null $dqlAlias |
||
| 470 | * |
||
| 471 | * @return GreaterOrEqualThan |
||
| 472 | */ |
||
| 473 | public static function gte($field, $value, $dqlAlias = null) |
||
| 474 | { |
||
| 475 | return new GreaterOrEqualThan($field, $value, $dqlAlias); |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param Operand|string $field |
||
| 480 | * @param LikePattern|string $value |
||
| 481 | * @param string $format |
||
| 482 | * @param string|null $dqlAlias |
||
| 483 | * |
||
| 484 | * @return Like |
||
| 485 | */ |
||
| 486 | public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null) |
||
| 487 | { |
||
| 488 | return new Like($field, $value, $format, $dqlAlias); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $value |
||
| 493 | * @param string|null $dqlAlias |
||
| 494 | * |
||
| 495 | * @return InstanceOfX |
||
| 496 | */ |
||
| 497 | public static function instanceOfX($value, $dqlAlias = null) |
||
| 498 | { |
||
| 499 | return new InstanceOfX($value, $dqlAlias); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param Operand|string $value |
||
| 504 | * @param Operand|string $field |
||
| 505 | * @param string|null $dqlAlias |
||
| 506 | * |
||
| 507 | * @return MemberOfX |
||
| 508 | */ |
||
| 509 | public static function memberOfX($value, $field, $dqlAlias = null) |
||
| 510 | { |
||
| 511 | return new MemberOfX($value, $field, $dqlAlias); |
||
| 512 | } |
||
| 513 | |||
| 514 | // Specifications |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param Filter|QueryModifier $spec |
||
| 518 | * |
||
| 519 | * @return CountOf |
||
| 520 | */ |
||
| 521 | public static function countOf($spec) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @param Filter $spec |
||
| 528 | * |
||
| 529 | * @return Having |
||
| 530 | */ |
||
| 531 | public static function having(Filter $spec) |
||
| 535 | |||
| 536 | // Operands |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $fieldName |
||
| 540 | * @param string|null $dqlAlias |
||
| 541 | * |
||
| 542 | * @return Field |
||
| 543 | */ |
||
| 544 | public static function field($fieldName, $dqlAlias = null) |
||
| 545 | { |
||
| 546 | return new Field($fieldName, $dqlAlias); |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param mixed $value |
||
| 551 | * @param int|string|null $valueType |
||
| 552 | * |
||
| 553 | * @return Value |
||
| 554 | */ |
||
| 555 | public static function value($value, $valueType = null) |
||
| 556 | { |
||
| 557 | return new Value($value, $valueType); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @param array $values |
||
| 562 | * @param int|string|null $valueType |
||
| 563 | * |
||
| 564 | * @return Values |
||
| 565 | */ |
||
| 566 | public static function values($values, $valueType = null) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param string $value |
||
| 573 | * @param string $format |
||
| 574 | * |
||
| 575 | * @return LikePattern |
||
| 576 | */ |
||
| 577 | public static function likePattern($value, $format = LikePattern::CONTAINS) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @param Operand|string $field |
||
| 584 | * |
||
| 585 | * @return CountDistinct |
||
| 586 | */ |
||
| 587 | public static function countDistinct($field) |
||
| 591 | |||
| 592 | // Arithmetic operands |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @param Operand|string $field |
||
| 596 | * @param Operand|mixed $value |
||
| 597 | * |
||
| 598 | * @return Addition |
||
| 599 | */ |
||
| 600 | public static function add($field, $value) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param Operand|string $field |
||
| 607 | * @param Operand|mixed $value |
||
| 608 | * |
||
| 609 | * @return Subtraction |
||
| 610 | */ |
||
| 611 | public static function sub($field, $value) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param Operand|string $field |
||
| 618 | * @param Operand|mixed $value |
||
| 619 | * |
||
| 620 | * @return Multiplication |
||
| 621 | */ |
||
| 622 | public static function mul($field, $value) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param Operand|string $field |
||
| 629 | * @param Operand|mixed $value |
||
| 630 | * |
||
| 631 | * @return Division |
||
| 632 | */ |
||
| 633 | public static function div($field, $value) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @param Operand|string $field |
||
| 640 | * @param Operand|mixed $value |
||
| 641 | * |
||
| 642 | * @return Modulo |
||
| 643 | */ |
||
| 644 | public static function mod($field, $value) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Call DQL function. |
||
| 651 | * |
||
| 652 | * Usage: |
||
| 653 | * Spec::fun('CURRENT_DATE') |
||
| 654 | * Spec::fun('DATE_DIFF', $date1, $date2) |
||
| 655 | * |
||
| 656 | * @param string $functionName |
||
| 657 | * @param mixed ...$arguments |
||
| 658 | * |
||
| 659 | * @return PlatformFunction |
||
| 660 | */ |
||
| 661 | public static function fun($functionName, ...$arguments) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param string $alias |
||
| 668 | * |
||
| 669 | * @return Alias |
||
| 670 | */ |
||
| 671 | public static function alias($alias) |
||
| 675 | } |
||
| 676 |