Completed
Push — master ( 13b26c...ac5926 )
by Gordon
07:17 queued 06:10
created

RandomEnglishGenerator::sentence()   C

Complexity

Conditions 10
Paths 86

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 6.7224
c 0
b 0
f 0
cc 10
nc 86
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php declare(strict_types = 1);
2
3
namespace Suilven\RandomEnglish;
4
5
use Suilven\RandomEnglish\Helper\LanguageHelper;
6
7
class RandomEnglishGenerator
8
{
9
10
    private const POSSIBLE_WORD_TYPES = [
11
        'noun',
12
        'countable_noun',
13
14
        'adjective',
15
        'preposition',
16
        'conjunction',
17
        'contraction',
18
        'control_verb',
19
20
        'sequence_adverb',
21
        'frequency_adverb',
22
23
        // pluralize and ing these?
24
        'intransitive_verb',
25
        'irregular_verb',
26
        'transitive_verb',
27
        'verb',
28
        'adverb',
29
30
        'intransitive_verb_ing',
31
        'irregular_verb_ing',
32
        'transitive_verb_ing',
33
        'verb_ing',
34
35
36
        // positions
37
        'locative',
38
39
        'plural_noun',
40
41
42
        // names
43
        'mens_name',
44
        'womens_name',
45
    ];
46
47
    /** @var string configuration file for sentences */
48
    private $config = '';
49
50
    public function __construct()
51
    {
52
        $configFile = \dirname(__FILE__) . '/../config/sentence-structure.cfg';
53
        $this->setConfig(\file_get_contents($configFile));
54
    }
55
56
57
    /** @param string $newConfig configuration file of how sentence structures */
58
    public function setConfig(string $newConfig): void
59
    {
60
        $this->config = \trim($newConfig);
61
    }
62
63
64
    /**
65
     * Generate a random sentence
66
     *
67
     * @param bool $titleCase true to generate a title, false (default) to generate a sentence
68
     * @return string a random sentence
69
     */
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
144
145
    /**
146
     * Generate a random title
147
     *
148
     * @return string a random sentence, all in caps, no trailing full stop
149
     */
150
    public function title(): string
151
    {
152
        return $this->sentence(true);
153
    }
154
155
156
    /**
157
     * @param int $maxSentences The maximum number of sentences
158
     * @return string a paragraph of random sentences
159
     */
160
    public function paragraph(int $maxSentences = 10): string
161
    {
162
        $nParagraph = \rand(1, $maxSentences);
163
        $sentences = [];
164
165
        for ($i=0; $i< $nParagraph; $i++) {
166
            $sentences[] = $this->sentence();
167
        }
168
169
        return \implode("  ", $sentences);
170
    }
171
172
173
    /** @return string a plural noun */
174
    public function pluralNoun(): string
175
    {
176
        $plurableNoun = $this->getRandomWord('countable_noun');
177
178
        return $plurableNoun . 's';
179
    }
180
181
182
    /** @return string a plural verb */
183
    public function pluralVerb(): string
184
    {
185
        // @todo Choose a random verb source file
186
        $plurableNoun = $this->getRandomWord('verb');
187
188
        return $plurableNoun . 's';
189
    }
190
191
192
    /** @return string doing version of a verb */
193
    public function verbing(): string
194
    {
195
        // @todo Choose a random verb source file
196
        $plurableNoun = $this->getRandomWord('verb');
197
198
        return $plurableNoun . 'ing';
199
    }
200
201
202
    /**
203
     * @param string $wordType the type of word, e.g. noun, verb
204
     * @return string a random word for the given word type
205
     */
206
    private function getRandomWord(string $wordType): string
207
    {
208
        $wordsFile = \dirname(__FILE__) . '/../words/english_' . $wordType . 's.txt';
209
        $words = \explode(\PHP_EOL, \trim(\file_get_contents($wordsFile)));
210
        \shuffle($words);
211
212
        return $words[0];
213
    }
214
}
215