| Conditions | 10 |
| Paths | 86 |
| Total Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types = 1); |
||
| 69 | public function sentence(bool $titleCase = false): string |
||
| 70 | { |
||
| 71 | // choose a random sentence structure |
||
| 72 | $structures = \explode(\PHP_EOL, $this->config); |
||
| 73 | \shuffle($structures); |
||
| 74 | $structure = $structures[0]; |
||
| 75 | |||
| 76 | $expression='/[\s]+/'; |
||
| 77 | $splits = \preg_split($expression, $structure, -1, \PREG_SPLIT_NO_EMPTY); |
||
| 78 | |||
| 79 | $sentenceArray = []; |
||
| 80 | |||
| 81 | foreach ($splits as $possiblyRandomWord) { |
||
| 82 | $randomized = false; |
||
| 83 | |||
| 84 | foreach (self::POSSIBLE_WORD_TYPES as $wordType) { |
||
| 85 | $pluralNoun = ($wordType === 'plural_noun'); |
||
| 86 | if ($pluralNoun) { |
||
| 87 | $wordType = 'noun'; |
||
| 88 | $possiblyRandomWord = \str_replace('plural_', '', $possiblyRandomWord); |
||
| 89 | } |
||
| 90 | |||
| 91 | $ing = \substr($wordType, -4)=== '_ing'; |
||
| 92 | if ($ing) { |
||
| 93 | $wordType = \str_replace('_ing', '', $wordType); |
||
| 94 | $possiblyRandomWord = \str_replace('_ing', '', $possiblyRandomWord); |
||
| 95 | } |
||
| 96 | $start = '[' . $wordType . ']'; |
||
| 97 | |||
| 98 | if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $restOfWord = \str_replace($start, '', $possiblyRandomWord); |
||
| 103 | $randomWord = $this->getRandomWord($wordType); |
||
| 104 | if ($pluralNoun) { |
||
| 105 | $helper = new LanguageHelper(); |
||
| 106 | |||
| 107 | $randomWord = $helper->pluralizeNoun($randomWord); |
||
| 108 | } |
||
| 109 | if ($ing) { |
||
| 110 | $helper = new LanguageHelper(); |
||
| 111 | $randomWord = $helper->ingVerb($randomWord); |
||
| 112 | } |
||
| 113 | $sentenceArray[] =$randomWord . $restOfWord; |
||
| 114 | $randomized = true; |
||
| 115 | |||
| 116 | break; |
||
| 117 | } |
||
| 118 | |||
| 119 | if ($randomized) { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | |||
| 123 | $sentenceArray[] = |
||
| 124 | $possiblyRandomWord; |
||
| 125 | } |
||
| 126 | |||
| 127 | // ensure sentence starts with a capital |
||
| 128 | $sentenceArray[0] = \ucfirst($sentenceArray[0]); |
||
| 129 | |||
| 130 | $result = \implode(" ", $sentenceArray) . '.'; |
||
| 131 | |||
| 132 | if ($titleCase) { |
||
| 133 | $result = \ucwords($result); |
||
| 134 | $result = \substr_replace($result, "", -1); |
||
| 135 | } |
||
| 136 | |||
| 137 | $result = \str_replace('?.', '?', $result); |
||
| 138 | $result = \str_replace('!.', '?', $result); |
||
| 139 | |||
| 140 | return $result; |
||
| 141 | } |
||
| 142 | |||
| 214 |