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 |
||
| 85 | class Spec |
||
| 86 | { |
||
| 87 | /** |
||
| 88 | * @param string $name |
||
| 89 | * @param array $arguments |
||
| 90 | * |
||
| 91 | * @return PlatformFunction |
||
| 92 | */ |
||
| 93 | public static function __callStatic($name, array $arguments = []) |
||
| 97 | |||
| 98 | /* |
||
| 99 | * Logic |
||
| 100 | */ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return AndX |
||
| 104 | */ |
||
| 105 | public static function andX() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return OrX |
||
| 115 | */ |
||
| 116 | public static function orX() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Filter $spec |
||
| 126 | * |
||
| 127 | * @return Not |
||
| 128 | */ |
||
| 129 | public static function not(Filter $spec) |
||
| 133 | |||
| 134 | /* |
||
| 135 | * Query modifier |
||
| 136 | */ |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $field |
||
| 140 | * @param string $newAlias |
||
| 141 | * @param string $dqlAlias |
||
| 142 | * |
||
| 143 | * @return Join |
||
| 144 | */ |
||
| 145 | public static function join($field, $newAlias, $dqlAlias = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $field |
||
| 152 | * @param string $newAlias |
||
| 153 | * @param string $dqlAlias |
||
| 154 | * |
||
| 155 | * @return LeftJoin |
||
| 156 | */ |
||
| 157 | public static function leftJoin($field, $newAlias, $dqlAlias = null) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $field |
||
| 164 | * @param string $newAlias |
||
| 165 | * @param string $dqlAlias |
||
| 166 | * |
||
| 167 | * @return InnerJoin |
||
| 168 | */ |
||
| 169 | public static function innerJoin($field, $newAlias, $dqlAlias = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param int $count |
||
| 176 | * |
||
| 177 | * @return Limit |
||
| 178 | */ |
||
| 179 | public static function limit($count) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param int $count |
||
| 186 | * |
||
| 187 | * @return Offset |
||
| 188 | */ |
||
| 189 | public static function offset($count) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param int $sliceSize |
||
| 196 | * @param int $sliceNumber |
||
| 197 | * |
||
| 198 | * @return Slice |
||
| 199 | */ |
||
| 200 | public static function slice($sliceSize, $sliceNumber = 0) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $field |
||
| 207 | * @param string $order |
||
| 208 | * @param string|null $dqlAlias |
||
| 209 | * |
||
| 210 | * @return OrderBy |
||
| 211 | */ |
||
| 212 | public static function orderBy($field, $order = 'ASC', $dqlAlias = null) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $field |
||
| 219 | * @param string $dqlAlias |
||
| 220 | * |
||
| 221 | * @return GroupBy |
||
| 222 | */ |
||
| 223 | public static function groupBy($field, $dqlAlias = null) |
||
| 227 | |||
| 228 | /* |
||
| 229 | * Selection |
||
| 230 | */ |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param mixed $field |
||
| 234 | * |
||
| 235 | * @return Select |
||
| 236 | */ |
||
| 237 | public static function select($field) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param mixed $field |
||
| 244 | * |
||
| 245 | * @return AddSelect |
||
| 246 | */ |
||
| 247 | public static function addSelect($field) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $dqlAlias |
||
| 254 | * |
||
| 255 | * @return SelectEntity |
||
| 256 | */ |
||
| 257 | public static function selectEntity($dqlAlias) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param Filter|Operand|string $expression |
||
| 264 | * @param string $alias |
||
| 265 | * |
||
| 266 | * @return SelectAs |
||
| 267 | */ |
||
| 268 | public static function selectAs($expression, $alias) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Filter|Operand|string $expression |
||
| 275 | * @param string $alias |
||
| 276 | * |
||
| 277 | * @return SelectHiddenAs |
||
| 278 | */ |
||
| 279 | public static function selectHiddenAs($expression, $alias) |
||
| 283 | |||
| 284 | /* |
||
| 285 | * Result modifier |
||
| 286 | */ |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return AsArray |
||
| 290 | */ |
||
| 291 | public static function asArray() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return AsSingleScalar |
||
| 298 | */ |
||
| 299 | public static function asSingleScalar() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return AsScalar |
||
| 306 | */ |
||
| 307 | public static function asScalar() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 314 | * |
||
| 315 | * @return Cache |
||
| 316 | */ |
||
| 317 | public static function cache($cacheLifetime) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param int $roundSeconds How may seconds to round time |
||
| 324 | * |
||
| 325 | * @return RoundDateTime |
||
| 326 | */ |
||
| 327 | public static function roundDateTimeParams($roundSeconds) |
||
| 331 | |||
| 332 | /* |
||
| 333 | * Filters |
||
| 334 | */ |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param Operand|string $field |
||
| 338 | * @param string|null $dqlAlias |
||
| 339 | * |
||
| 340 | * @return IsNull |
||
| 341 | */ |
||
| 342 | public static function isNull($field, $dqlAlias = null) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param Operand|string $field |
||
| 349 | * @param string|null $dqlAlias |
||
| 350 | * |
||
| 351 | * @return IsNotNull |
||
| 352 | */ |
||
| 353 | public static function isNotNull($field, $dqlAlias = null) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Make sure the $field has a value equals to $value. |
||
| 360 | * |
||
| 361 | * @param string $field |
||
| 362 | * @param mixed $value |
||
| 363 | * @param string $dqlAlias |
||
| 364 | * |
||
| 365 | * @return In |
||
| 366 | */ |
||
| 367 | public static function in($field, $value, $dqlAlias = null) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $field |
||
| 374 | * @param mixed $value |
||
| 375 | * @param string $dqlAlias |
||
| 376 | * |
||
| 377 | * @return Not |
||
| 378 | */ |
||
| 379 | public static function notIn($field, $value, $dqlAlias = null) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param Operand|string $field |
||
| 386 | * @param Operand|mixed $value |
||
| 387 | * @param string|null $dqlAlias |
||
| 388 | * |
||
| 389 | * @return Comparison |
||
| 390 | */ |
||
| 391 | public static function eq($field, $value, $dqlAlias = null) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param Operand|string $field |
||
| 398 | * @param Operand|mixed $value |
||
| 399 | * @param string|null $dqlAlias |
||
| 400 | * |
||
| 401 | * @return Comparison |
||
| 402 | */ |
||
| 403 | public static function neq($field, $value, $dqlAlias = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param Operand|string $field |
||
| 410 | * @param Operand|mixed $value |
||
| 411 | * @param string|null $dqlAlias |
||
| 412 | * |
||
| 413 | * @return Comparison |
||
| 414 | */ |
||
| 415 | public static function lt($field, $value, $dqlAlias = null) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param Operand|string $field |
||
| 422 | * @param Operand|mixed $value |
||
| 423 | * @param string|null $dqlAlias |
||
| 424 | * |
||
| 425 | * @return Comparison |
||
| 426 | */ |
||
| 427 | public static function lte($field, $value, $dqlAlias = null) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param Operand|string $field |
||
| 434 | * @param Operand|mixed $value |
||
| 435 | * @param string|null $dqlAlias |
||
| 436 | * |
||
| 437 | * @return Comparison |
||
| 438 | */ |
||
| 439 | public static function gt($field, $value, $dqlAlias = null) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param Operand|string $field |
||
| 446 | * @param Operand|mixed $value |
||
| 447 | * @param string|null $dqlAlias |
||
| 448 | * |
||
| 449 | * @return Comparison |
||
| 450 | */ |
||
| 451 | public static function gte($field, $value, $dqlAlias = null) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param Operand|string $field |
||
| 458 | * @param string $value |
||
| 459 | * @param string $format |
||
| 460 | * @param string|null $dqlAlias |
||
| 461 | * |
||
| 462 | * @return Like |
||
| 463 | */ |
||
| 464 | public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $value |
||
| 471 | * @param string|null $dqlAlias |
||
| 472 | * |
||
| 473 | * @return InstanceOfX |
||
| 474 | */ |
||
| 475 | public static function instanceOfX($value, $dqlAlias = null) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param string $value |
||
| 482 | * @param string|null $dqlAlias |
||
| 483 | * |
||
| 484 | * @return MemberOfX |
||
| 485 | */ |
||
| 486 | public static function memberOfX($value, $dqlAlias = null) |
||
| 490 | |||
| 491 | /* |
||
| 492 | * Specifications |
||
| 493 | */ |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param Filter|QueryModifier $spec |
||
| 497 | * |
||
| 498 | * @return CountOf |
||
| 499 | */ |
||
| 500 | public static function countOf($spec) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param Filter|string $spec |
||
| 507 | * |
||
| 508 | * @return Having |
||
| 509 | */ |
||
| 510 | public static function having($spec) |
||
| 514 | |||
| 515 | /* |
||
| 516 | * Operands |
||
| 517 | */ |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $fieldName |
||
| 521 | * |
||
| 522 | * @return Field |
||
| 523 | */ |
||
| 524 | public static function field($fieldName) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param mixed $value |
||
| 531 | * @param int|string|null $valueType |
||
| 532 | * |
||
| 533 | * @return Value |
||
| 534 | */ |
||
| 535 | public static function value($value, $valueType = null) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param array $values |
||
| 542 | * @param int|string|null $valueType |
||
| 543 | * |
||
| 544 | * @return Values |
||
| 545 | */ |
||
| 546 | public static function values($values, $valueType = null) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $value |
||
| 553 | * @param string $format |
||
| 554 | * |
||
| 555 | * @return LikePattern |
||
| 556 | */ |
||
| 557 | public static function likePattern($value, $format = LikePattern::CONTAINS) |
||
| 561 | |||
| 562 | /* |
||
| 563 | * Arithmetic operands |
||
| 564 | */ |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param Operand|string $field |
||
| 568 | * @param Operand|mixed $value |
||
| 569 | * |
||
| 570 | * @return Addition |
||
| 571 | */ |
||
| 572 | public static function add($field, $value) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param Operand|string $field |
||
| 579 | * @param Operand|mixed $value |
||
| 580 | * |
||
| 581 | * @return Subtraction |
||
| 582 | */ |
||
| 583 | public static function sub($field, $value) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param Operand|string $field |
||
| 590 | * @param Operand|mixed $value |
||
| 591 | * |
||
| 592 | * @return Multiplication |
||
| 593 | */ |
||
| 594 | public static function mul($field, $value) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param Operand|string $field |
||
| 601 | * @param Operand|mixed $value |
||
| 602 | * |
||
| 603 | * @return Division |
||
| 604 | */ |
||
| 605 | public static function div($field, $value) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param Operand|string $field |
||
| 612 | * @param Operand|mixed $value |
||
| 613 | * |
||
| 614 | * @return Modulo |
||
| 615 | */ |
||
| 616 | public static function mod($field, $value) |
||
| 620 | |||
| 621 | /* |
||
| 622 | * Bitwise operands |
||
| 623 | */ |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @param Operand|string $field |
||
| 627 | * @param Operand|mixed $value |
||
| 628 | * |
||
| 629 | * @return BitAnd |
||
| 630 | */ |
||
| 631 | public static function bAnd($field, $value) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @param Operand|string $field |
||
| 638 | * @param Operand|mixed $value |
||
| 639 | * |
||
| 640 | * @return BitOr |
||
| 641 | */ |
||
| 642 | public static function bOr($field, $value) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @param Operand|string $field |
||
| 649 | * @param Operand|mixed $value |
||
| 650 | * |
||
| 651 | * @return BitXor |
||
| 652 | */ |
||
| 653 | public static function bXor($field, $value) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param Operand|string $field |
||
| 660 | * @param Operand|mixed $value |
||
| 661 | * |
||
| 662 | * @return BitLeftShift |
||
| 663 | */ |
||
| 664 | public static function bLs($field, $value) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param Operand|string $field |
||
| 671 | * @param Operand|mixed $value |
||
| 672 | * |
||
| 673 | * @return BitRightShift |
||
| 674 | */ |
||
| 675 | public static function bRs($field, $value) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @param Operand|string $field |
||
| 682 | * |
||
| 683 | * @return BitNot |
||
| 684 | */ |
||
| 685 | public static function bNot($field) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * @param string $functionName |
||
| 692 | * @param mixed $arguments |
||
| 693 | * |
||
| 694 | * @return PlatformFunction |
||
| 695 | */ |
||
| 696 | public static function fun($functionName, $arguments = []) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @param string $alias |
||
| 705 | * |
||
| 706 | * @return Alias |
||
| 707 | */ |
||
| 708 | public static function alias($alias) |
||
| 712 | } |
||
| 713 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.