| Conditions | 7 |
| Paths | 9 |
| Total Lines | 52 |
| 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 |
||
| 155 | private function loadRelatedPosts() |
||
| 156 | { |
||
| 157 | $post = Post::where('slug', $this->property('slug')) |
||
| 158 | ->with('tags') |
||
| 159 | ->first(); |
||
| 160 | |||
| 161 | if (!$post || (!$tagIds = $post->tags->lists('id'))) { |
||
| 162 | return null; |
||
| 163 | } |
||
| 164 | |||
| 165 | $query = Post::isPublished() |
||
| 166 | ->where('id', '<>', $post->id) |
||
| 167 | ->whereHas('tags', function ($tag) use ($tagIds) { |
||
| 168 | $tag->whereIn('id', $tagIds); |
||
| 169 | }) |
||
| 170 | ->with('tags'); |
||
| 171 | |||
| 172 | if (in_array($this->orderBy, array_keys($this->getOrderByOptions()))) { |
||
| 173 | if ($this->orderBy == 'random') { |
||
| 174 | $query->inRandomOrder(); |
||
| 175 | } else { |
||
| 176 | list($sortField, $sortDirection) = explode(' ', $this->orderBy); |
||
| 177 | |||
| 178 | if ($sortField == 'relevance') { |
||
| 179 | $sortField = DB::raw( |
||
| 180 | sprintf( |
||
| 181 | '( |
||
| 182 | select count(*) |
||
| 183 | from `%1$s` |
||
| 184 | where `%1$s`.`post_id` = `rainlab_blog_posts`.`id` |
||
| 185 | and `%1$s`.`tag_id` in (%2$s) |
||
| 186 | )', |
||
| 187 | Tag::CROSS_REFERENCE_TABLE_NAME, |
||
| 188 | DB::getPdo()->quote(implode(', ', $tagIds)) |
||
| 189 | ) |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | $query->orderBy($sortField, $sortDirection); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($take = intval($this->property('limit'))) { |
||
| 198 | $query->take($take); |
||
| 199 | } |
||
| 200 | |||
| 201 | $posts = $query->get(); |
||
| 202 | |||
| 203 | $this->setPostUrls($posts); |
||
| 204 | |||
| 205 | return $posts; |
||
| 206 | } |
||
| 207 | } |
||
| 208 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.