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); |
||
70 | public function sentence(bool $titleCase = false): string |
||
71 | { |
||
72 | // choose a random sentence structure |
||
73 | $structures = \explode(\PHP_EOL, $this->config); |
||
74 | \shuffle($structures); |
||
75 | $structure = $structures[0]; |
||
76 | |||
77 | $expression='/[\s]+/'; |
||
78 | $splits = \preg_split($expression, $structure, -1, \PREG_SPLIT_NO_EMPTY); |
||
79 | |||
80 | $sentenceArray = []; |
||
81 | |||
82 | foreach ($splits as $possiblyRandomWord) { |
||
83 | $randomized = false; |
||
84 | |||
85 | foreach (self::POSSIBLE_WORD_TYPES as $wordType) { |
||
86 | $pluralNoun = ($wordType === 'plural_noun'); |
||
87 | if ($pluralNoun) { |
||
88 | $wordType = 'noun'; |
||
89 | $possiblyRandomWord = \str_replace('plural_', '', $possiblyRandomWord); |
||
90 | } |
||
91 | |||
92 | $ing = \substr($wordType, -4)=== '_ing'; |
||
93 | if ($ing) { |
||
94 | $wordType = \str_replace('_ing', '', $wordType); |
||
95 | $possiblyRandomWord = \str_replace('_ing', '', $possiblyRandomWord); |
||
96 | } |
||
97 | $start = '[' . $wordType . ']'; |
||
98 | |||
99 | if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) { |
||
100 | continue; |
||
101 | } |
||
102 | |||
103 | $restOfWord = \str_replace($start, '', $possiblyRandomWord); |
||
104 | $randomWord = $this->getRandomWord($wordType); |
||
105 | if ($pluralNoun) { |
||
106 | $helper = new LanguageHelper(); |
||
107 | |||
108 | $randomWord = $helper->pluralizeNoun($randomWord); |
||
109 | } |
||
110 | if ($ing) { |
||
111 | $helper = new LanguageHelper(); |
||
112 | $randomWord = $helper->ingVerb($randomWord); |
||
113 | } |
||
114 | $sentenceArray[] =$randomWord . $restOfWord; |
||
115 | $randomized = true; |
||
116 | |||
117 | break; |
||
118 | } |
||
119 | |||
120 | if ($randomized) { |
||
121 | continue; |
||
122 | } |
||
123 | |||
124 | $sentenceArray[] = |
||
125 | $possiblyRandomWord; |
||
126 | } |
||
127 | |||
128 | // ensure sentence starts with a capital |
||
129 | $sentenceArray[0] = \ucfirst($sentenceArray[0]); |
||
130 | |||
131 | $result = \implode(" ", $sentenceArray) . '.'; |
||
132 | |||
133 | if ($titleCase) { |
||
134 | $result = \ucwords($result); |
||
135 | $result = \substr_replace($result, "", -1); |
||
136 | } |
||
137 | |||
138 | $result = \str_replace('?.', '?', $result); |
||
139 | $result = \str_replace('!.', '?', $result); |
||
140 | |||
141 | return $result; |
||
142 | } |
||
143 | |||
215 |