Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php namespace Taskforcedev\LaravelForum\Http\Controllers; |
||
| 11 | class ForumController extends BaseController |
||
| 12 | { |
||
| 13 | public function index() |
||
| 18 | |||
| 19 | View Code Duplication | public function view($id) |
|
| 32 | |||
| 33 | View Code Duplication | public function createPost($forum_id) |
|
| 46 | |||
| 47 | public function viewPost($forum_id, $post_id) |
||
| 48 | { |
||
| 49 | try { |
||
| 50 | $post = ForumPost::with('replies')->where('id', $post_id)->firstOrFail(); |
||
|
|
|||
| 51 | |||
| 52 | $data = $this->buildData(); |
||
| 53 | $data['post'] = $post; |
||
| 54 | |||
| 55 | return view('laravel-forum::forum.thread', $data); |
||
| 56 | } catch (Exception $e) { |
||
| 57 | return view('laravel-forum::errors.404'); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Validates if the forum exists, if so returns it. |
||
| 63 | * |
||
| 64 | * @param integer $id |
||
| 65 | * |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | private function validateForumId($id) |
||
| 77 | } |
||
| 78 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: