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 |
||
| 83 | class Spec |
||
| 84 | { |
||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * @param array $arguments |
||
| 88 | * |
||
| 89 | * @return PlatformFunction |
||
| 90 | */ |
||
| 91 | public static function __callStatic($name, array $arguments = []) |
||
| 95 | |||
| 96 | /* |
||
| 97 | * Logic |
||
| 98 | */ |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return AndX |
||
| 102 | */ |
||
| 103 | public static function andX() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return OrX |
||
| 113 | */ |
||
| 114 | public static function orX() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Filter $spec |
||
| 124 | * |
||
| 125 | * @return Not |
||
| 126 | */ |
||
| 127 | public static function not(Filter $spec) |
||
| 131 | |||
| 132 | /* |
||
| 133 | * Query modifier |
||
| 134 | */ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $field |
||
| 138 | * @param string $newAlias |
||
| 139 | * @param string $dqlAlias |
||
| 140 | * |
||
| 141 | * @return Join |
||
| 142 | */ |
||
| 143 | public static function join($field, $newAlias, $dqlAlias = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $field |
||
| 150 | * @param string $newAlias |
||
| 151 | * @param string $dqlAlias |
||
| 152 | * |
||
| 153 | * @return LeftJoin |
||
| 154 | */ |
||
| 155 | public static function leftJoin($field, $newAlias, $dqlAlias = null) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $field |
||
| 162 | * @param string $newAlias |
||
| 163 | * @param string $dqlAlias |
||
| 164 | * |
||
| 165 | * @return InnerJoin |
||
| 166 | */ |
||
| 167 | public static function innerJoin($field, $newAlias, $dqlAlias = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $count |
||
| 174 | * |
||
| 175 | * @return Limit |
||
| 176 | */ |
||
| 177 | public static function limit($count) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param int $count |
||
| 184 | * |
||
| 185 | * @return Offset |
||
| 186 | */ |
||
| 187 | public static function offset($count) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $sliceSize |
||
| 194 | * @param int $sliceNumber |
||
| 195 | * |
||
| 196 | * @return Slice |
||
| 197 | */ |
||
| 198 | public static function slice($sliceSize, $sliceNumber = 0) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $field |
||
| 205 | * @param string $order |
||
| 206 | * @param string|null $dqlAlias |
||
| 207 | * |
||
| 208 | * @return OrderBy |
||
| 209 | */ |
||
| 210 | public static function orderBy($field, $order = 'ASC', $dqlAlias = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param string $field |
||
| 217 | * @param string $dqlAlias |
||
| 218 | * |
||
| 219 | * @return GroupBy |
||
| 220 | */ |
||
| 221 | public static function groupBy($field, $dqlAlias = null) |
||
| 225 | |||
| 226 | /* |
||
| 227 | * Selection |
||
| 228 | */ |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param mixed $field |
||
| 232 | * |
||
| 233 | * @return Select |
||
| 234 | */ |
||
| 235 | public static function select($field) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param mixed $field |
||
| 242 | * |
||
| 243 | * @return AddSelect |
||
| 244 | */ |
||
| 245 | public static function addSelect($field) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $dqlAlias |
||
| 252 | * |
||
| 253 | * @return SelectEntity |
||
| 254 | */ |
||
| 255 | public static function selectEntity($dqlAlias) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param Filter|Operand|string $expression |
||
| 262 | * @param string $alias |
||
| 263 | * |
||
| 264 | * @return SelectAs |
||
| 265 | */ |
||
| 266 | public static function selectAs($expression, $alias) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param Filter|Operand|string $expression |
||
| 273 | * @param string $alias |
||
| 274 | * |
||
| 275 | * @return SelectHiddenAs |
||
| 276 | */ |
||
| 277 | public static function selectHiddenAs($expression, $alias) |
||
| 281 | |||
| 282 | /* |
||
| 283 | * Result modifier |
||
| 284 | */ |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return AsArray |
||
| 288 | */ |
||
| 289 | public static function asArray() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return AsSingleScalar |
||
| 296 | */ |
||
| 297 | public static function asSingleScalar() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 304 | * |
||
| 305 | * @return Cache |
||
| 306 | */ |
||
| 307 | public static function cache($cacheLifetime) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param int $roundSeconds How may seconds to round time |
||
| 314 | * |
||
| 315 | * @return RoundDateTime |
||
| 316 | */ |
||
| 317 | public static function roundDateTimeParams($roundSeconds) |
||
| 321 | |||
| 322 | /* |
||
| 323 | * Filters |
||
| 324 | */ |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param Operand|string $field |
||
| 328 | * @param string|null $dqlAlias |
||
| 329 | * |
||
| 330 | * @return IsNull |
||
| 331 | */ |
||
| 332 | public static function isNull($field, $dqlAlias = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param Operand|string $field |
||
| 339 | * @param string|null $dqlAlias |
||
| 340 | * |
||
| 341 | * @return IsNotNull |
||
| 342 | */ |
||
| 343 | public static function isNotNull($field, $dqlAlias = null) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Make sure the $field has a value equals to $value. |
||
| 350 | * |
||
| 351 | * @param string $field |
||
| 352 | * @param mixed $value |
||
| 353 | * @param string $dqlAlias |
||
| 354 | * |
||
| 355 | * @return In |
||
| 356 | */ |
||
| 357 | public static function in($field, $value, $dqlAlias = null) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $field |
||
| 364 | * @param mixed $value |
||
| 365 | * @param string $dqlAlias |
||
| 366 | * |
||
| 367 | * @return Not |
||
| 368 | */ |
||
| 369 | public static function notIn($field, $value, $dqlAlias = null) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param Operand|string $field |
||
| 376 | * @param Operand|mixed $value |
||
| 377 | * @param string|null $dqlAlias |
||
| 378 | * |
||
| 379 | * @return Comparison |
||
| 380 | */ |
||
| 381 | public static function eq($field, $value, $dqlAlias = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param Operand|string $field |
||
| 388 | * @param Operand|mixed $value |
||
| 389 | * @param string|null $dqlAlias |
||
| 390 | * |
||
| 391 | * @return Comparison |
||
| 392 | */ |
||
| 393 | public static function neq($field, $value, $dqlAlias = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param Operand|string $field |
||
| 400 | * @param Operand|mixed $value |
||
| 401 | * @param string|null $dqlAlias |
||
| 402 | * |
||
| 403 | * @return Comparison |
||
| 404 | */ |
||
| 405 | public static function lt($field, $value, $dqlAlias = null) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param Operand|string $field |
||
| 412 | * @param Operand|mixed $value |
||
| 413 | * @param string|null $dqlAlias |
||
| 414 | * |
||
| 415 | * @return Comparison |
||
| 416 | */ |
||
| 417 | public static function lte($field, $value, $dqlAlias = null) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param Operand|string $field |
||
| 424 | * @param Operand|mixed $value |
||
| 425 | * @param string|null $dqlAlias |
||
| 426 | * |
||
| 427 | * @return Comparison |
||
| 428 | */ |
||
| 429 | public static function gt($field, $value, $dqlAlias = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param Operand|string $field |
||
| 436 | * @param Operand|mixed $value |
||
| 437 | * @param string|null $dqlAlias |
||
| 438 | * |
||
| 439 | * @return Comparison |
||
| 440 | */ |
||
| 441 | public static function gte($field, $value, $dqlAlias = null) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param Operand|string $field |
||
| 448 | * @param string $value |
||
| 449 | * @param string $format |
||
| 450 | * @param string|null $dqlAlias |
||
| 451 | * |
||
| 452 | * @return Like |
||
| 453 | */ |
||
| 454 | public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $value |
||
| 461 | * @param string|null $dqlAlias |
||
| 462 | * |
||
| 463 | * @return InstanceOfX |
||
| 464 | */ |
||
| 465 | public static function instanceOfX($value, $dqlAlias = null) |
||
| 469 | |||
| 470 | /* |
||
| 471 | * Specifications |
||
| 472 | */ |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param Filter|QueryModifier $spec |
||
| 476 | * |
||
| 477 | * @return CountOf |
||
| 478 | */ |
||
| 479 | public static function countOf($spec) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param Filter|string $spec |
||
| 486 | * |
||
| 487 | * @return Having |
||
| 488 | */ |
||
| 489 | public static function having($spec) |
||
| 493 | |||
| 494 | /* |
||
| 495 | * Operands |
||
| 496 | */ |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $fieldName |
||
| 500 | * |
||
| 501 | * @return Field |
||
| 502 | */ |
||
| 503 | public static function field($fieldName) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param mixed $value |
||
| 510 | * @param int|string|null $valueType |
||
| 511 | * |
||
| 512 | * @return Value |
||
| 513 | */ |
||
| 514 | public static function value($value, $valueType = null) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param array $values |
||
| 521 | * @param int|string|null $valueType |
||
| 522 | * |
||
| 523 | * @return Values |
||
| 524 | */ |
||
| 525 | public static function values($values, $valueType = null) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param string $value |
||
| 532 | * @param string $format |
||
| 533 | * |
||
| 534 | * @return LikePattern |
||
| 535 | */ |
||
| 536 | public static function likePattern($value, $format = LikePattern::CONTAINS) |
||
| 540 | |||
| 541 | /* |
||
| 542 | * Arithmetic operands |
||
| 543 | */ |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param Operand|string $field |
||
| 547 | * @param Operand|mixed $value |
||
| 548 | * |
||
| 549 | * @return Addition |
||
| 550 | */ |
||
| 551 | public static function add($field, $value) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param Operand|string $field |
||
| 558 | * @param Operand|mixed $value |
||
| 559 | * |
||
| 560 | * @return Subtraction |
||
| 561 | */ |
||
| 562 | public static function sub($field, $value) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @param Operand|string $field |
||
| 569 | * @param Operand|mixed $value |
||
| 570 | * |
||
| 571 | * @return Multiplication |
||
| 572 | */ |
||
| 573 | public static function mul($field, $value) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @param Operand|string $field |
||
| 580 | * @param Operand|mixed $value |
||
| 581 | * |
||
| 582 | * @return Division |
||
| 583 | */ |
||
| 584 | public static function div($field, $value) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param Operand|string $field |
||
| 591 | * @param Operand|mixed $value |
||
| 592 | * |
||
| 593 | * @return Modulo |
||
| 594 | */ |
||
| 595 | public static function mod($field, $value) |
||
| 599 | |||
| 600 | /* |
||
| 601 | * Bitwise operands |
||
| 602 | */ |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param Operand|string $field |
||
| 606 | * @param Operand|mixed $value |
||
| 607 | * |
||
| 608 | * @return BitAnd |
||
| 609 | */ |
||
| 610 | public static function bAnd($field, $value) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param Operand|string $field |
||
| 617 | * @param Operand|mixed $value |
||
| 618 | * |
||
| 619 | * @return BitOr |
||
| 620 | */ |
||
| 621 | public static function bOr($field, $value) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param Operand|string $field |
||
| 628 | * @param Operand|mixed $value |
||
| 629 | * |
||
| 630 | * @return BitXor |
||
| 631 | */ |
||
| 632 | public static function bXor($field, $value) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @param Operand|string $field |
||
| 639 | * @param Operand|mixed $value |
||
| 640 | * |
||
| 641 | * @return BitLeftShift |
||
| 642 | */ |
||
| 643 | public static function bLs($field, $value) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @param Operand|string $field |
||
| 650 | * @param Operand|mixed $value |
||
| 651 | * |
||
| 652 | * @return BitRightShift |
||
| 653 | */ |
||
| 654 | public static function bRs($field, $value) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @param Operand|string $field |
||
| 661 | * |
||
| 662 | * @return BitNot |
||
| 663 | */ |
||
| 664 | public static function bNot($field) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param string $functionName |
||
| 671 | * @param mixed $arguments |
||
| 672 | * |
||
| 673 | * @return PlatformFunction |
||
| 674 | */ |
||
| 675 | public static function fun($functionName, $arguments = []) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @param string $alias |
||
| 684 | * |
||
| 685 | * @return Alias |
||
| 686 | */ |
||
| 687 | public static function alias($alias) |
||
| 691 | } |
||
| 692 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.