Conditions | 5 |
Paths | 7 |
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); |
||
72 | public function sentence(bool $titleCase = false): string |
||
73 | { |
||
74 | // choose a random sentence structure |
||
75 | $structures = \explode(\PHP_EOL, $this->config); |
||
76 | \shuffle($structures); |
||
77 | $structure = $structures[0]; |
||
78 | |||
79 | $expression='/[\s]+/'; |
||
80 | $splits = \preg_split($expression, $structure, -1, \PREG_SPLIT_NO_EMPTY); |
||
81 | |||
82 | $sentenceArray = []; |
||
83 | |||
84 | /** @var string $possiblyRandomWord */ |
||
85 | foreach ($splits as $possiblyRandomWord) { |
||
86 | $randomized = false; |
||
87 | |||
88 | foreach (self::POSSIBLE_WORD_TYPES as $wordType) { |
||
89 | $pluralNoun = $this->pluralNounCheck($wordType, $possiblyRandomWord); |
||
90 | $ing = $this->ingVerbCheck($wordType, $possiblyRandomWord); |
||
91 | |||
92 | $start = '[' . $wordType . ']'; |
||
93 | |||
94 | // check the start of the word as it may be suffixed by likes of a question of exclamation mark. |
||
95 | // If no match for the possible word type continue until the next one |
||
96 | if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) { |
||
97 | continue; |
||
98 | } |
||
99 | |||
100 | // ---- we are now getting a random word from a file ---- |
||
101 | |||
102 | // note the suffix of the word, and append this to the random word |
||
103 | $restOfWord = \str_replace($start, '', $possiblyRandomWord); |
||
104 | |||
105 | // This avoids having to use 'countrie' in the sentence structure file : country -> countries |
||
106 | $wordType = \str_replace('country', 'countrie', $wordType); |
||
107 | |||
108 | /** @var string $randomWord */ |
||
109 | $randomWord = $this->getRandomWord($wordType); |
||
110 | |||
111 | // augment the random word if a plural noun or an ing verb |
||
112 | $this->augmentIfPluralNoun($pluralNoun, $randomWord); |
||
113 | $this->augmentIfIngVerb($ing, $randomWord); |
||
114 | |||
115 | // add the random word, and then the suffix of the word such as ?! or !! |
||
116 | $sentenceArray[] =$randomWord . $restOfWord; |
||
117 | |||
118 | // flag as randomized |
||
119 | $randomized = true; |
||
120 | |||
121 | break; |
||
122 | } |
||
123 | |||
124 | // if we have found a randomized word, skill to the next word in the sentence |
||
125 | if ($randomized) { |
||
126 | continue; |
||
127 | } |
||
128 | |||
129 | // no randomized word has been found, aka no [verb] or [noun], thus append the possibly random word as is |
||
130 | $sentenceArray[] = $possiblyRandomWord; |
||
131 | } |
||
132 | |||
133 | // ensure sentence starts with a capital |
||
134 | $sentenceArray[0] = \ucfirst($sentenceArray[0]); |
||
135 | |||
136 | $result = \implode(" ", $sentenceArray) . '.'; |
||
137 | |||
138 | $this->augmentSentenceTitleCase($titleCase, $result); |
||
139 | |||
140 | $result = \str_replace('?.', '?', $result); |
||
141 | $result = \str_replace('!.', '?', $result); |
||
142 | |||
143 | return $result; |
||
144 | } |
||
145 | |||
279 |