Conditions | 5 |
Paths | 5 |
Total Lines | 54 |
Code Lines | 23 |
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 |
||
148 | public function onRun() |
||
149 | { |
||
150 | //Prepare vars |
||
151 | $this->prepareVars(); |
||
152 | |||
153 | // Load the target post |
||
154 | $post = Post::where('slug', $this->property('slug')) |
||
155 | ->with('tags') |
||
156 | ->first(); |
||
157 | |||
158 | // Abort if there is no source, or it has no tags |
||
159 | if (!$post || (!$tagIds = $post->tags->lists('id'))) |
||
160 | return; |
||
161 | |||
162 | // Start building our query for related posts |
||
163 | $query = Post::isPublished() |
||
164 | ->where('id', '<>', $post->id) |
||
165 | ->whereHas('tags', function($tag) use ($tagIds) { |
||
166 | $tag->whereIn('id', $tagIds); |
||
167 | }) |
||
168 | ->with('tags'); |
||
169 | |||
170 | // Sort the related posts |
||
171 | $subQuery = DB::raw('( |
||
172 | select count(*) |
||
173 | from `ginopane_blogtaxonomy_post_tag` |
||
174 | where `ginopane_blogtaxonomy_post_tag`.`post_id` = `rainlab_blog_posts`.`id` |
||
175 | and `ginopane_blogtaxonomy_post_tag`.`tag_id` in ('.implode(', ', $tagIds).') |
||
176 | )'); |
||
177 | |||
178 | $key = $this->property('orderBy') ?: $subQuery; |
||
179 | $query->orderBy($key, $this->property('direction')); |
||
180 | |||
181 | // Limit the number of results |
||
182 | if ($take = intval($this->property('results'))) { |
||
183 | $query->take($take); |
||
184 | } |
||
185 | |||
186 | // Execute the query |
||
187 | $posts = $query->get(); |
||
188 | |||
189 | /* |
||
190 | * Add a "url" helper attribute for linking to each post |
||
191 | */ |
||
192 | $posts->each( |
||
193 | function($post) |
||
194 | { |
||
195 | $post->setUrl($this->postPage,$this->controller); |
||
196 | } |
||
197 | ); |
||
198 | |||
199 | $this->posts = $posts; |
||
200 | |||
201 | } |
||
202 | } |
||
203 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.