Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TextStatistics 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 TextStatistics, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class TextStatistics |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var string $strEncoding Used to hold character encoding to be used |
||
| 43 | * by object, if set |
||
| 44 | */ |
||
| 45 | protected $strEncoding = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Maximum grade level to be reported. Calculated grades above |
||
| 49 | * this level will be returned as this value. |
||
| 50 | */ |
||
| 51 | protected $maxGradeLevel = 12; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool $normalise Should the result be normalised? |
||
| 55 | */ |
||
| 56 | public $normalise = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var int $dps How many decimal places should results be given to? |
||
| 60 | */ |
||
| 61 | public $dps = 1; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string $strText Holds the last text checked. If no text passed to |
||
| 65 | * function, it will use this text instead. |
||
| 66 | */ |
||
| 67 | private static $strText = false; |
||
| 68 | |||
| 69 | 44 | /** |
|
| 70 | * Constructor. |
||
| 71 | 44 | * |
|
| 72 | * @param string $strEncoding Optional character encoding. |
||
| 73 | * @return void |
||
|
|
|||
| 74 | */ |
||
| 75 | 44 | public function __construct($strEncoding = '') |
|
| 82 | 38 | ||
| 83 | /** |
||
| 84 | * Set the text to measure the readability of. |
||
| 85 | * @param string|boolean $strText Text to be checked |
||
| 86 | 38 | * @return string Cleaned text |
|
| 87 | 38 | */ |
|
| 88 | 38 | public function setText($strText) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Set the encoding of the text being measured. |
||
| 101 | * @param string $strEncoding New encoding |
||
| 102 | * @return boolean |
||
| 103 | */ |
||
| 104 | public function setEncoding($strEncoding) |
||
| 109 | 3 | ||
| 110 | /** |
||
| 111 | 3 | * Set the maximum grade level for grade-level type indexes |
|
| 112 | * (Flesch-Kincaid Grade Level, Gunning-Fog, Coleman-Liau, SMOG, Automated Readability) |
||
| 113 | 3 | * @param integer $maxGradeLevel Grade level to use |
|
| 114 | 3 | * @return boolean Success |
|
| 115 | 3 | */ |
|
| 116 | 3 | public function setMaxGradeLevel($maxGradeLevel) |
|
| 126 | 3 | ||
| 127 | 3 | /** |
|
| 128 | 3 | * Gives the Flesch-Kincaid Reading Ease of text entered rounded to one digit |
|
| 129 | 3 | * @param boolean|string $strText Text to be checked |
|
| 130 | * @return int|float |
||
| 131 | 3 | */ |
|
| 132 | View Code Duplication | public function fleschKincaidReadingEase($strText = false) |
|
| 160 | 3 | ||
| 161 | /** |
||
| 162 | 3 | * Gives the Flesch-Kincaid Grade level of text entered rounded to one digit |
|
| 163 | 3 | * @param boolean|string $strText Text to be checked |
|
| 164 | * @return int|float |
||
| 165 | 3 | */ |
|
| 166 | View Code Duplication | public function fleschKincaidGradeLevel($strText = false) |
|
| 194 | 3 | ||
| 195 | /** |
||
| 196 | * Gives the Gunning-Fog score of text entered rounded to one digit |
||
| 197 | * @param boolean|string $strText Text to be checked |
||
| 198 | * @return int|float |
||
| 199 | */ |
||
| 200 | public function gunningFogScore($strText = false) |
||
| 220 | 3 | ||
| 221 | 3 | /** |
|
| 222 | 3 | * Gives the Coleman-Liau Index of text entered rounded to one digit |
|
| 223 | 3 | * @param boolean|string $strText Text to be checked |
|
| 224 | 3 | * @return int|float |
|
| 225 | 3 | */ |
|
| 226 | 3 | View Code Duplication | public function colemanLiauIndex($strText = false) |
| 262 | 3 | ||
| 263 | 3 | /** |
|
| 264 | * Gives the SMOG Index of text entered rounded to one digit |
||
| 265 | 3 | * @param boolean|string $strText Text to be checked |
|
| 266 | 3 | * @return int|float |
|
| 267 | */ |
||
| 268 | 3 | View Code Duplication | public function smogIndex($strText = false) |
| 300 | 3 | ||
| 301 | 3 | /** |
|
| 302 | 3 | * Gives the Automated Readability Index of text entered rounded to one digit |
|
| 303 | 3 | * @param boolean|string $strText Text to be checked |
|
| 304 | 3 | * @return int|float |
|
| 305 | 3 | */ |
|
| 306 | 3 | View Code Duplication | public function automatedReadabilityIndex($strText = false) |
| 342 | |||
| 343 | /** |
||
| 344 | * Gives the Dale-Chall readability score of text entered rounded to one digit |
||
| 345 | * @param boolean|string $strText Text to be checked |
||
| 346 | * @return int|float |
||
| 347 | */ |
||
| 348 | View Code Duplication | public function daleChallReadabilityScore($strText = false) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Gives the Spache readability score of text entered rounded to one digit |
||
| 387 | * @param boolean|string $strText Text to be checked |
||
| 388 | * @return int|float |
||
| 389 | */ |
||
| 390 | View Code Duplication | public function spacheReadabilityScore($strText = false) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Returns the number of words NOT on the Dale-Chall easy word list |
||
| 425 | * @param boolean|string $strText Text to be measured |
||
| 426 | * @return int |
||
| 427 | */ |
||
| 428 | public function daleChallDifficultWordCount($strText = false) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Returns the number of unique words NOT on the Spache easy word list |
||
| 452 | * @param boolean|string $strText Text to be measured |
||
| 453 | * @return int |
||
| 454 | */ |
||
| 455 | public function spacheDifficultWordCount($strText = false) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns letter count for text. |
||
| 485 | * @param boolean|string $strText Text to be measured |
||
| 486 | * @return int |
||
| 487 | */ |
||
| 488 | public function letterCount($strText = false) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns sentence count for text. |
||
| 497 | * @param boolean|string $strText Text to be measured |
||
| 498 | * @return int |
||
| 499 | */ |
||
| 500 | public function sentenceCount($strText = false) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns word count for text. |
||
| 509 | * @param boolean|string $strText Text to be measured |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | public function wordCount($strText = false) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns average words per sentence for text. |
||
| 521 | * @param boolean|string $strText Text to be measured |
||
| 522 | * @return int|float |
||
| 523 | */ |
||
| 524 | public function averageWordsPerSentence($strText = false) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Returns number of syllables in a word |
||
| 533 | * @param boolean|string $strText Text to be measured |
||
| 534 | * @return int |
||
| 535 | */ |
||
| 536 | public function syllableCount($strText = false) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns total syllable count for text. |
||
| 545 | * @param boolean|string $strText Text to be measured |
||
| 546 | * @return int |
||
| 547 | */ |
||
| 548 | public function totalSyllables($strText = false) |
||
| 554 | 1 | ||
| 555 | /** |
||
| 556 | * Returns average syllables per word for text. |
||
| 557 | * @param boolean|string $strText Text to be measured |
||
| 558 | * @return int|float |
||
| 559 | */ |
||
| 560 | public function averageSyllablesPerWord($strText = false) |
||
| 566 | |||
| 567 | 1 | /** |
|
| 568 | * Returns the number of words with more than three syllables |
||
| 569 | * @param boolean|string $strText Text to be measured |
||
| 570 | * @param bool $blnCountProperNouns Boolean - should proper nouns be included in words count |
||
| 571 | * @return int |
||
| 572 | */ |
||
| 573 | public function wordsWithThreeSyllables($strText = false, $blnCountProperNouns = true) |
||
| 579 | 3 | ||
| 580 | /** |
||
| 581 | 3 | * Returns the percentage of words with more than three syllables |
|
| 582 | * @param boolean|string $strText Text to be measured |
||
| 583 | * @param bool $blnCountProperNouns Boolean - should proper nouns be included in words count |
||
| 584 | 3 | * @return int|float |
|
| 585 | */ |
||
| 586 | 3 | public function percentageWordsWithThreeSyllables($strText = false, $blnCountProperNouns = true) |
|
| 592 | |||
| 593 | /** |
||
| 594 | 3 | * We switched to camel-case but we'll leave these aliases in for |
|
| 595 | * convenience for anyone switching from the previous version. |
||
| 596 | 3 | */ |
|
| 597 | public function flesch_kincaid_reading_ease($strText = false) |
||
| 601 | 3 | ||
| 602 | public function flesch_kincaid_grade_level($strText = false) |
||
| 606 | |||
| 607 | public function gunning_fog_score($strText = false) |
||
| 611 | |||
| 612 | public function coleman_liau_index($strText = false) |
||
| 616 | |||
| 617 | public function smog_index($strText = false) |
||
| 621 | |||
| 622 | public function automated_readability_index($strText = false) |
||
| 626 | |||
| 627 | public function dale_chall_readability_score($strText = false) |
||
| 631 | |||
| 632 | public function spache_readability_score($strText = false) |
||
| 636 | } |
||
| 637 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.