| Conditions | 10 |
| Paths | 20 |
| Total Lines | 56 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 7 |
| CRAP Score | 10 |
| Changes | 10 | ||
| Bugs | 3 | Features | 2 |
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 |
||
| 127 | 1 | public function importAction() |
|
| 128 | { |
||
| 129 | 1 | $request = $this->getRequest(); |
|
| 130 | 1 | $form = new \mQueue\Form\Import(); |
|
| 131 | 1 | $form->setDefaults(['favoriteMinimum' => 9, 'excellentMinimum' => 7, 'okMinimum' => 5]); |
|
| 132 | 1 | $this->view->form = $form; |
|
|
1 ignored issue
–
show
|
|||
| 133 | |||
| 134 | 1 | if ($this->getRequest()->isPost() && $form->isValid($request->getPost())) { |
|
|
1 ignored issue
–
show
|
|||
| 135 | if (\mQueue\Model\User::getCurrent() == null) { |
||
| 136 | $this->_helper->FlashMessenger(['error' => _tr('You must be logged in.')]); |
||
| 137 | |||
| 138 | return; |
||
| 139 | } |
||
| 140 | |||
| 141 | $values = $form->getValues(); |
||
| 142 | $page = file_get_contents($values['url']); |
||
| 143 | |||
| 144 | $pattern = '|<a href="/title/tt(\d{7})/">.*</td>\s*<td.*>(\d+(\.\d)*)</td>|U'; |
||
| 145 | preg_match_all($pattern, $page, $matches); |
||
| 146 | |||
| 147 | $movies = []; |
||
| 148 | $matchesCount = count($matches[1]); |
||
| 149 | for ($i = 0; $i < $matchesCount; ++$i) { |
||
| 150 | $id = $matches[1][$i]; |
||
| 151 | $imdbRating = $matches[2][$i]; |
||
| 152 | |||
| 153 | $movie = \mQueue\Model\MovieMapper::find($id); |
||
| 154 | if (!$movie) { |
||
| 155 | $movie = \mQueue\Model\MovieMapper::getDbTable()->createRow(); |
||
| 156 | $movie->setId($id); |
||
| 157 | $movie->save(); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($imdbRating >= $values['favoriteMinimum']) { |
||
| 161 | $rating = \mQueue\Model\Status::Favorite; |
||
| 162 | } elseif ($imdbRating >= $values['excellentMinimum']) { |
||
| 163 | $rating = \mQueue\Model\Status::Excellent; |
||
| 164 | } elseif ($imdbRating >= $values['okMinimum']) { |
||
| 165 | $rating = \mQueue\Model\Status::Ok; |
||
| 166 | } else { |
||
| 167 | $rating = \mQueue\Model\Status::Bad; |
||
| 168 | } |
||
| 169 | |||
| 170 | $movie->setStatus(\mQueue\Model\User::getCurrent(), $rating); |
||
| 171 | $movies [] = $movie; |
||
| 172 | } |
||
| 173 | |||
| 174 | $count = count($movies); |
||
| 175 | if ($count) { |
||
| 176 | $this->_helper->FlashMessenger(_tr('Movies imported.')); |
||
| 177 | $this->view->movies = $movies; |
||
|
1 ignored issue
–
show
|
|||
| 178 | } else { |
||
| 179 | $this->_helper->FlashMessenger(['warning' => _tr('No movies found for import.')]); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | 1 | } |
|
| 183 | } |
||
| 184 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.