| Conditions | 7 |
| Paths | 18 |
| Total Lines | 52 |
| Code Lines | 35 |
| 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 |
||
| 125 | public function generatePostings(ConsoleIo $io) |
||
| 126 | { |
||
| 127 | $nPostings = (int)$io->ask('Number of postings to generate?', '100'); |
||
| 128 | if ($nPostings === 0) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | $ratio = (int)$io->ask('Average answers per thread?', '10'); |
||
| 132 | $seed = $nPostings / $ratio; |
||
| 133 | |||
| 134 | for ($i = 0; $i < $nPostings; $i++) { |
||
| 135 | $newThread = $i < $seed; |
||
| 136 | |||
| 137 | $user = $this->_randomUser(); |
||
| 138 | $posting = [ |
||
| 139 | 'name' => $user->get('username'), |
||
| 140 | 'subject' => "$i", |
||
| 141 | 'text' => rand(0, 1) ? $this->_randomText() : '', |
||
| 142 | 'user_id' => $user->getId(), |
||
| 143 | ]; |
||
| 144 | if ($newThread) { |
||
| 145 | $posting['pid'] = 0; |
||
| 146 | $posting['category_id'] = $this->_randomCategory(); |
||
| 147 | } else { |
||
| 148 | $id = array_rand($this->_Threads, 1); |
||
| 149 | $posting['category_id'] = $this->_Threads[$id]['category_id']; |
||
| 150 | $posting['tid'] = $this->_Threads[$id]['tid']; |
||
| 151 | $posting['pid'] = $this->_Threads[$id]['id']; |
||
| 152 | } |
||
| 153 | |||
| 154 | $posting = $this->Entries->createEntry($posting); |
||
| 155 | if ($posting->hasErrors()) { |
||
| 156 | var_dump($posting->getErrors()); |
||
|
|
|||
| 157 | } |
||
| 158 | |||
| 159 | if (empty($posting)) { |
||
| 160 | throw new \RuntimeException( |
||
| 161 | 'Could not create posting.' |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | $this->_progress($io, $i, $nPostings); |
||
| 166 | |||
| 167 | $id = $posting->get('id'); |
||
| 168 | $this->_Threads[] = [ |
||
| 169 | 'category_id' => $posting->get('category_id'), |
||
| 170 | 'id' => $id, |
||
| 171 | 'tid' => $posting->get('tid'), |
||
| 172 | ]; |
||
| 173 | } |
||
| 174 | |||
| 175 | $io->out(''); |
||
| 176 | $io->out("Generated $i postings."); |
||
| 177 | } |
||
| 289 |