Complex classes like Style 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 Style, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Style |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Color: black. |
||
| 27 | */ |
||
| 28 | const BLACK = 'black'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Color: red. |
||
| 32 | */ |
||
| 33 | const RED = 'red'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Color: green. |
||
| 37 | */ |
||
| 38 | const GREEN = 'green'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Color: yellow. |
||
| 42 | */ |
||
| 43 | const YELLOW = 'yellow'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Color: blue. |
||
| 47 | */ |
||
| 48 | const BLUE = 'blue'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Color: magenta. |
||
| 52 | */ |
||
| 53 | const MAGENTA = 'magenta'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Color: cyan. |
||
| 57 | */ |
||
| 58 | const CYAN = 'cyan'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Color: white. |
||
| 62 | */ |
||
| 63 | const WHITE = 'white'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string[] |
||
| 67 | */ |
||
| 68 | private static $colors = array( |
||
| 69 | self::BLACK, |
||
| 70 | self::RED, |
||
| 71 | self::GREEN, |
||
| 72 | self::YELLOW, |
||
| 73 | self::BLUE, |
||
| 74 | self::MAGENTA, |
||
| 75 | self::CYAN, |
||
| 76 | self::WHITE, |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $tag; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $fgColor; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | private $bgColor; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | private $bold = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | private $underlined = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | private $blinking = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | private $inverse = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | private $hidden = false; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Creates a style with the given tag name. |
||
| 121 | * |
||
| 122 | * @param string $tag The tag name. |
||
| 123 | * |
||
| 124 | * @return static The created style. |
||
| 125 | * |
||
| 126 | * @see noTag() |
||
| 127 | */ |
||
| 128 | 249 | public static function tag($tag) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Creates a style without a tag name. |
||
| 135 | * |
||
| 136 | * @return static The created style. |
||
| 137 | * |
||
| 138 | * @see tag() |
||
| 139 | */ |
||
| 140 | 24 | public static function noTag() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Creates a style. |
||
| 147 | * |
||
| 148 | * @param string $tag The tag name. |
||
|
|
|||
| 149 | */ |
||
| 150 | 273 | public function __construct($tag = null) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the foreground color. |
||
| 157 | * |
||
| 158 | * @param string $color One of the color constants. |
||
| 159 | * |
||
| 160 | * @return static The current instance. |
||
| 161 | */ |
||
| 162 | 2 | public function fg($color) |
|
| 163 | { |
||
| 164 | 2 | Assert::nullOrOneOf($color, self::$colors, 'The color must be null or one of the Style::* color constants. Got: "%s"'); |
|
| 165 | |||
| 166 | 2 | $this->fgColor = $color; |
|
| 167 | |||
| 168 | 2 | return $this; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Resets the foreground color to the system's default. |
||
| 173 | * |
||
| 174 | * @return static The current instance. |
||
| 175 | */ |
||
| 176 | 1 | public function fgDefault() |
|
| 177 | { |
||
| 178 | 1 | $this->fgColor = null; |
|
| 179 | |||
| 180 | 1 | return $this; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets the foreground color to black. |
||
| 185 | * |
||
| 186 | * @return static The current instance. |
||
| 187 | */ |
||
| 188 | 235 | public function fgBlack() |
|
| 189 | { |
||
| 190 | 235 | $this->fgColor = self::BLACK; |
|
| 191 | |||
| 192 | 235 | return $this; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets the foreground color to red. |
||
| 197 | * |
||
| 198 | * @return static The current instance. |
||
| 199 | */ |
||
| 200 | 1 | public function fgRed() |
|
| 201 | { |
||
| 202 | 1 | $this->fgColor = self::RED; |
|
| 203 | |||
| 204 | 1 | return $this; |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Sets the foreground color to green. |
||
| 209 | * |
||
| 210 | * @return static The current instance. |
||
| 211 | */ |
||
| 212 | 1 | public function fgGreen() |
|
| 213 | { |
||
| 214 | 1 | $this->fgColor = self::GREEN; |
|
| 215 | |||
| 216 | 1 | return $this; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Sets the foreground color to yellow. |
||
| 221 | * |
||
| 222 | * @return static The current instance. |
||
| 223 | */ |
||
| 224 | 238 | public function fgYellow() |
|
| 225 | { |
||
| 226 | 238 | $this->fgColor = self::YELLOW; |
|
| 227 | |||
| 228 | 238 | return $this; |
|
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Sets the foreground color to blue. |
||
| 233 | * |
||
| 234 | * @return static The current instance. |
||
| 235 | */ |
||
| 236 | 7 | public function fgBlue() |
|
| 237 | { |
||
| 238 | 7 | $this->fgColor = self::BLUE; |
|
| 239 | |||
| 240 | 7 | return $this; |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sets the foreground color to magenta. |
||
| 245 | * |
||
| 246 | * @return static The current instance. |
||
| 247 | */ |
||
| 248 | 1 | public function fgMagenta() |
|
| 249 | { |
||
| 250 | 1 | $this->fgColor = self::MAGENTA; |
|
| 251 | |||
| 252 | 1 | return $this; |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sets the foreground color to cyan. |
||
| 257 | * |
||
| 258 | * @return static The current instance. |
||
| 259 | */ |
||
| 260 | 235 | public function fgCyan() |
|
| 261 | { |
||
| 262 | 235 | $this->fgColor = self::CYAN; |
|
| 263 | |||
| 264 | 235 | return $this; |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sets the foreground color to white. |
||
| 269 | * |
||
| 270 | * @return static The current instance. |
||
| 271 | */ |
||
| 272 | 235 | public function fgWhite() |
|
| 273 | { |
||
| 274 | 235 | $this->fgColor = self::WHITE; |
|
| 275 | |||
| 276 | 235 | return $this; |
|
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Sets the background color. |
||
| 281 | * |
||
| 282 | * @param string $color One of the color constants. |
||
| 283 | * |
||
| 284 | * @return static The current instance. |
||
| 285 | */ |
||
| 286 | 2 | public function bg($color) |
|
| 287 | { |
||
| 288 | 2 | Assert::nullOrOneOf($color, self::$colors, 'The color must be null or one of the Style::* color constants. Got: "%s"'); |
|
| 289 | |||
| 290 | 2 | $this->bgColor = $color; |
|
| 291 | |||
| 292 | 2 | return $this; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Resets the background color to the system's default. |
||
| 297 | * |
||
| 298 | * @return static The current instance. |
||
| 299 | */ |
||
| 300 | 1 | public function bgDefault() |
|
| 301 | { |
||
| 302 | 1 | $this->bgColor = null; |
|
| 303 | |||
| 304 | 1 | return $this; |
|
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Sets the background color to black. |
||
| 309 | * |
||
| 310 | * @return static The current instance. |
||
| 311 | */ |
||
| 312 | 1 | public function bgBlack() |
|
| 313 | { |
||
| 314 | 1 | $this->bgColor = self::BLACK; |
|
| 315 | |||
| 316 | 1 | return $this; |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Sets the background color to red. |
||
| 321 | * |
||
| 322 | * @return static The current instance. |
||
| 323 | */ |
||
| 324 | 235 | public function bgRed() |
|
| 325 | { |
||
| 326 | 235 | $this->bgColor = self::RED; |
|
| 327 | |||
| 328 | 235 | return $this; |
|
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Sets the background color to green. |
||
| 333 | * |
||
| 334 | * @return static The current instance. |
||
| 335 | */ |
||
| 336 | 1 | public function bgGreen() |
|
| 337 | { |
||
| 338 | 1 | $this->bgColor = self::GREEN; |
|
| 339 | |||
| 340 | 1 | return $this; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Sets the background color to yellow. |
||
| 345 | * |
||
| 346 | * @return static The current instance. |
||
| 347 | */ |
||
| 348 | 235 | public function bgYellow() |
|
| 349 | { |
||
| 350 | 235 | $this->bgColor = self::YELLOW; |
|
| 351 | |||
| 352 | 235 | return $this; |
|
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Sets the background color to blue. |
||
| 357 | * |
||
| 358 | * @return static The current instance. |
||
| 359 | */ |
||
| 360 | 1 | public function bgBlue() |
|
| 361 | { |
||
| 362 | 1 | $this->bgColor = self::BLUE; |
|
| 363 | |||
| 364 | 1 | return $this; |
|
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Sets the background color to magenta. |
||
| 369 | * |
||
| 370 | * @return static The current instance. |
||
| 371 | */ |
||
| 372 | 7 | public function bgMagenta() |
|
| 373 | { |
||
| 374 | 7 | $this->bgColor = self::MAGENTA; |
|
| 375 | |||
| 376 | 7 | return $this; |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Sets the background color to cyan. |
||
| 381 | * |
||
| 382 | * @return static The current instance. |
||
| 383 | */ |
||
| 384 | 235 | public function bgCyan() |
|
| 385 | { |
||
| 386 | 235 | $this->bgColor = self::CYAN; |
|
| 387 | |||
| 388 | 235 | return $this; |
|
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Sets the background color to white. |
||
| 393 | * |
||
| 394 | * @return static The current instance. |
||
| 395 | */ |
||
| 396 | 1 | public function bgWhite() |
|
| 397 | { |
||
| 398 | 1 | $this->bgColor = self::WHITE; |
|
| 399 | |||
| 400 | 1 | return $this; |
|
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Sets the font weight to bold. |
||
| 405 | * |
||
| 406 | * @return static The current instance. |
||
| 407 | */ |
||
| 408 | 243 | public function bold() |
|
| 409 | { |
||
| 410 | 243 | $this->bold = true; |
|
| 411 | |||
| 412 | 243 | return $this; |
|
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Sets the font weight to normal. |
||
| 417 | * |
||
| 418 | * @return static The current instance. |
||
| 419 | */ |
||
| 420 | 1 | public function notBold() |
|
| 421 | { |
||
| 422 | 1 | $this->bold = false; |
|
| 423 | |||
| 424 | 1 | return $this; |
|
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Enables underlining. |
||
| 429 | * |
||
| 430 | * @return static The current instance. |
||
| 431 | */ |
||
| 432 | 236 | public function underlined() |
|
| 433 | { |
||
| 434 | 236 | $this->underlined = true; |
|
| 435 | |||
| 436 | 236 | return $this; |
|
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Disables underlining. |
||
| 441 | * |
||
| 442 | * @return static The current instance. |
||
| 443 | */ |
||
| 444 | 1 | public function notUnderlined() |
|
| 445 | { |
||
| 446 | 1 | $this->underlined = false; |
|
| 447 | |||
| 448 | 1 | return $this; |
|
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Enables blinking. |
||
| 453 | * |
||
| 454 | * @return static The current instance. |
||
| 455 | */ |
||
| 456 | 2 | public function blinking() |
|
| 457 | { |
||
| 458 | 2 | $this->blinking = true; |
|
| 459 | |||
| 460 | 2 | return $this; |
|
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Disables blinking. |
||
| 465 | * |
||
| 466 | * @return static The current instance. |
||
| 467 | */ |
||
| 468 | 1 | public function notBlinking() |
|
| 469 | { |
||
| 470 | 1 | $this->blinking = false; |
|
| 471 | |||
| 472 | 1 | return $this; |
|
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Enables inverse colors. |
||
| 477 | * |
||
| 478 | * @return static The current instance. |
||
| 479 | */ |
||
| 480 | 2 | public function inverse() |
|
| 481 | { |
||
| 482 | 2 | $this->inverse = true; |
|
| 483 | |||
| 484 | 2 | return $this; |
|
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Disables inverse colors. |
||
| 489 | * |
||
| 490 | * @return static The current instance. |
||
| 491 | */ |
||
| 492 | 1 | public function notInverse() |
|
| 493 | { |
||
| 494 | 1 | $this->inverse = false; |
|
| 495 | |||
| 496 | 1 | return $this; |
|
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Hides the text. |
||
| 501 | * |
||
| 502 | * @return static The current instance. |
||
| 503 | */ |
||
| 504 | 2 | public function hidden() |
|
| 505 | { |
||
| 506 | 2 | $this->hidden = true; |
|
| 507 | |||
| 508 | 2 | return $this; |
|
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Does not hide the text. |
||
| 513 | * |
||
| 514 | * @return static The current instance. |
||
| 515 | */ |
||
| 516 | 1 | public function notHidden() |
|
| 517 | { |
||
| 518 | 1 | $this->hidden = false; |
|
| 519 | |||
| 520 | 1 | return $this; |
|
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Returns the style's tag name. |
||
| 525 | * |
||
| 526 | * @return string The tag name or `null` if the style has no tag. |
||
| 527 | */ |
||
| 528 | 251 | public function getTag() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Returns the foreground color. |
||
| 535 | * |
||
| 536 | * @return string One of the color constants or `null` if the system's |
||
| 537 | * default should be used. |
||
| 538 | */ |
||
| 539 | 270 | public function getForegroundColor() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Returns the background color. |
||
| 546 | * |
||
| 547 | * @return string One of the color constants or `null` if the system's |
||
| 548 | * default should be used. |
||
| 549 | */ |
||
| 550 | 270 | public function getBackgroundColor() |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Returns whether the text is bold. |
||
| 557 | * |
||
| 558 | * @return bool Returns `true` if text is formatted bold and `false` |
||
| 559 | * otherwise. |
||
| 560 | */ |
||
| 561 | 262 | public function isBold() |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Returns whether the text is underlined. |
||
| 568 | * |
||
| 569 | * @return bool Returns `true` if text is formatted underlined and `false` |
||
| 570 | * otherwise. |
||
| 571 | */ |
||
| 572 | 262 | public function isUnderlined() |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Returns whether the text is blinking. |
||
| 579 | * |
||
| 580 | * @return bool Returns `true` if text is formatted blinking and `false` |
||
| 581 | * otherwise. |
||
| 582 | */ |
||
| 583 | 262 | public function isBlinking() |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Returns whether the text is reversed. |
||
| 590 | * |
||
| 591 | * @return bool Returns `true` if text is formatted reversed and `false` |
||
| 592 | * otherwise. |
||
| 593 | */ |
||
| 594 | 262 | public function isInverse() |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Returns whether the text is concealed. |
||
| 601 | * |
||
| 602 | * @return bool Returns `true` if text is formatted concealed and `false` |
||
| 603 | * otherwise. |
||
| 604 | */ |
||
| 605 | 262 | public function isHidden() |
|
| 609 | } |
||
| 610 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.