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 |
||
| 72 | class Spec |
||
| 73 | { |
||
| 74 | /** |
||
| 75 | * @param string $name |
||
| 76 | * @param array $arguments |
||
| 77 | * |
||
| 78 | * @return PlatformFunction |
||
| 79 | */ |
||
| 80 | public static function __callStatic($name, array $arguments = []) |
||
| 84 | |||
| 85 | /* |
||
| 86 | * Logic |
||
| 87 | */ |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return AndX |
||
| 91 | */ |
||
| 92 | public static function andX() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return OrX |
||
| 102 | */ |
||
| 103 | public static function orX() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param Filter $spec |
||
| 113 | * |
||
| 114 | * @return Not |
||
| 115 | */ |
||
| 116 | public static function not(Filter $spec) |
||
| 120 | |||
| 121 | /* |
||
| 122 | * Query modifier |
||
| 123 | */ |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $field |
||
| 127 | * @param string $newAlias |
||
| 128 | * @param string $dqlAlias |
||
| 129 | * |
||
| 130 | * @return Join |
||
| 131 | */ |
||
| 132 | public static function join($field, $newAlias, $dqlAlias = null) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $field |
||
| 139 | * @param string $newAlias |
||
| 140 | * @param string $dqlAlias |
||
| 141 | * |
||
| 142 | * @return LeftJoin |
||
| 143 | */ |
||
| 144 | public static function leftJoin($field, $newAlias, $dqlAlias = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $field |
||
| 151 | * @param string $newAlias |
||
| 152 | * @param string $dqlAlias |
||
| 153 | * |
||
| 154 | * @return InnerJoin |
||
| 155 | */ |
||
| 156 | public static function innerJoin($field, $newAlias, $dqlAlias = null) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param int $count |
||
| 163 | * |
||
| 164 | * @return Limit |
||
| 165 | */ |
||
| 166 | public static function limit($count) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param int $count |
||
| 173 | * |
||
| 174 | * @return Offset |
||
| 175 | */ |
||
| 176 | public static function offset($count) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param int $sliceSize |
||
| 183 | * @param int $sliceNumber |
||
| 184 | * |
||
| 185 | * @return Slice |
||
| 186 | */ |
||
| 187 | public static function slice($sliceSize, $sliceNumber = 0) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $field |
||
| 194 | * @param string $order |
||
| 195 | * @param string|null $dqlAlias |
||
| 196 | * |
||
| 197 | * @return OrderBy |
||
| 198 | */ |
||
| 199 | public static function orderBy($field, $order = 'ASC', $dqlAlias = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $field |
||
| 206 | * @param string $dqlAlias |
||
| 207 | * |
||
| 208 | * @return GroupBy |
||
| 209 | */ |
||
| 210 | public static function groupBy($field, $dqlAlias = null) |
||
| 214 | |||
| 215 | /* |
||
| 216 | * Selection |
||
| 217 | */ |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param mixed $field |
||
| 221 | * |
||
| 222 | * @return Select |
||
| 223 | */ |
||
| 224 | public static function select($field) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param mixed $field |
||
| 231 | * |
||
| 232 | * @return AddSelect |
||
| 233 | */ |
||
| 234 | public static function addSelect($field) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $dqlAlias |
||
| 241 | * |
||
| 242 | * @return SelectEntity |
||
| 243 | */ |
||
| 244 | public static function selectEntity($dqlAlias) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param Filter|Operand|string $expression |
||
| 251 | * @param string $alias |
||
| 252 | * |
||
| 253 | * @return SelectAs |
||
| 254 | */ |
||
| 255 | public static function selectAs($expression, $alias) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param Filter|Operand|string $expression |
||
| 262 | * @param string $alias |
||
| 263 | * |
||
| 264 | * @return SelectHiddenAs |
||
| 265 | */ |
||
| 266 | public static function selectHiddenAs($expression, $alias) |
||
| 270 | |||
| 271 | /* |
||
| 272 | * Result modifier |
||
| 273 | */ |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return AsArray |
||
| 277 | */ |
||
| 278 | public static function asArray() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return AsSingleScalar |
||
| 285 | */ |
||
| 286 | public static function asSingleScalar() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param int $cacheLifetime How many seconds the cached entry is valid |
||
| 293 | * |
||
| 294 | * @return Cache |
||
| 295 | */ |
||
| 296 | public static function cache($cacheLifetime) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param int $roundSeconds How may seconds to round time |
||
| 303 | * |
||
| 304 | * @return RoundDateTime |
||
| 305 | */ |
||
| 306 | public static function roundDateTimeParams($roundSeconds) |
||
| 310 | |||
| 311 | /* |
||
| 312 | * Filters |
||
| 313 | */ |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param Operand|string $field |
||
| 317 | * @param string|null $dqlAlias |
||
| 318 | * |
||
| 319 | * @return IsNull |
||
| 320 | */ |
||
| 321 | public static function isNull($field, $dqlAlias = null) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param Operand|string $field |
||
| 328 | * @param string|null $dqlAlias |
||
| 329 | * |
||
| 330 | * @return IsNotNull |
||
| 331 | */ |
||
| 332 | public static function isNotNull($field, $dqlAlias = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Make sure the $field has a value equals to $value. |
||
| 339 | * |
||
| 340 | * @param string $field |
||
| 341 | * @param mixed $value |
||
| 342 | * @param string $dqlAlias |
||
| 343 | * |
||
| 344 | * @return In |
||
| 345 | */ |
||
| 346 | public static function in($field, $value, $dqlAlias = null) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $field |
||
| 353 | * @param mixed $value |
||
| 354 | * @param string $dqlAlias |
||
| 355 | * |
||
| 356 | * @return Not |
||
| 357 | */ |
||
| 358 | public static function notIn($field, $value, $dqlAlias = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param Operand|string $field |
||
| 365 | * @param Operand|mixed $value |
||
| 366 | * @param string|null $dqlAlias |
||
| 367 | * |
||
| 368 | * @return Comparison |
||
| 369 | */ |
||
| 370 | public static function eq($field, $value, $dqlAlias = null) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param Operand|string $field |
||
| 377 | * @param Operand|mixed $value |
||
| 378 | * @param string|null $dqlAlias |
||
| 379 | * |
||
| 380 | * @return Comparison |
||
| 381 | */ |
||
| 382 | public static function neq($field, $value, $dqlAlias = null) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param Operand|string $field |
||
| 389 | * @param Operand|mixed $value |
||
| 390 | * @param string|null $dqlAlias |
||
| 391 | * |
||
| 392 | * @return Comparison |
||
| 393 | */ |
||
| 394 | public static function lt($field, $value, $dqlAlias = null) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param Operand|string $field |
||
| 401 | * @param Operand|mixed $value |
||
| 402 | * @param string|null $dqlAlias |
||
| 403 | * |
||
| 404 | * @return Comparison |
||
| 405 | */ |
||
| 406 | public static function lte($field, $value, $dqlAlias = null) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param Operand|string $field |
||
| 413 | * @param Operand|mixed $value |
||
| 414 | * @param string|null $dqlAlias |
||
| 415 | * |
||
| 416 | * @return Comparison |
||
| 417 | */ |
||
| 418 | public static function gt($field, $value, $dqlAlias = null) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param Operand|string $field |
||
| 425 | * @param Operand|mixed $value |
||
| 426 | * @param string|null $dqlAlias |
||
| 427 | * |
||
| 428 | * @return Comparison |
||
| 429 | */ |
||
| 430 | public static function gte($field, $value, $dqlAlias = null) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param Operand|string $field |
||
| 437 | * @param string $value |
||
| 438 | * @param string $format |
||
| 439 | * @param string|null $dqlAlias |
||
| 440 | * |
||
| 441 | * @return Like |
||
| 442 | */ |
||
| 443 | public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $value |
||
| 450 | * @param string|null $dqlAlias |
||
| 451 | * |
||
| 452 | * @return InstanceOfX |
||
| 453 | */ |
||
| 454 | public static function instanceOfX($value, $dqlAlias = null) |
||
| 458 | |||
| 459 | /* |
||
| 460 | * Specifications |
||
| 461 | */ |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param Filter|QueryModifier $spec |
||
| 465 | * |
||
| 466 | * @return CountOf |
||
| 467 | */ |
||
| 468 | public static function countOf($spec) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param Filter|string $spec |
||
| 475 | * |
||
| 476 | * @return Having |
||
| 477 | */ |
||
| 478 | public static function having($spec) |
||
| 482 | |||
| 483 | /* |
||
| 484 | * Operands |
||
| 485 | */ |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param string $fieldName |
||
| 489 | * |
||
| 490 | * @return Field |
||
| 491 | */ |
||
| 492 | public static function field($fieldName) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param mixed $value |
||
| 499 | * @param int|string|null $valueType |
||
| 500 | * |
||
| 501 | * @return Value |
||
| 502 | */ |
||
| 503 | public static function value($value, $valueType = null) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param array $values |
||
| 510 | * @param int|string|null $valueType |
||
| 511 | * |
||
| 512 | * @return Values |
||
| 513 | */ |
||
| 514 | public static function values($values, $valueType = null) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $value |
||
| 521 | * @param string $format |
||
| 522 | * |
||
| 523 | * @return LikePattern |
||
| 524 | */ |
||
| 525 | public static function likePattern($value, $format = LikePattern::CONTAINS) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param string $functionName |
||
| 532 | * @param mixed $arguments |
||
| 533 | * |
||
| 534 | * @return PlatformFunction |
||
| 535 | */ |
||
| 536 | public static function fun($functionName, $arguments = []) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param string $alias |
||
| 545 | * |
||
| 546 | * @return Alias |
||
| 547 | */ |
||
| 548 | public static function alias($alias) |
||
| 552 | } |
||
| 553 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.