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 |
||
| 17 | class TreeController extends Controller |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Display a listing of trees. |
||
| 21 | * |
||
| 22 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 23 | */ |
||
| 24 | public function index() |
||
| 25 | { |
||
| 26 | $tournament = Tournament::with( |
||
|
|
|||
| 27 | 'competitors', |
||
| 28 | 'championships.settings', |
||
| 29 | 'championships.category')->first(); |
||
| 30 | |||
| 31 | return view('laravel-tournaments::tree.index') |
||
| 32 | ->with('tournament', $tournament); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Build Tree. |
||
| 37 | * |
||
| 38 | * @param Request $request |
||
| 39 | * |
||
| 40 | * @return \Illuminate\Http\Response|string |
||
| 41 | */ |
||
| 42 | public function store(Request $request, $championshipId) |
||
| 61 | |||
| 62 | private function deleteEverything() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param Request $request |
||
| 75 | * @param $isTeam |
||
| 76 | * @param $numFighters |
||
| 77 | * |
||
| 78 | * @return Championship |
||
| 79 | */ |
||
| 80 | protected function provisionObjects(Request $request, $isTeam, $numFighters) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param Request $request |
||
| 105 | * |
||
| 106 | * @return \Illuminate\Http\RedirectResponse |
||
| 107 | */ |
||
| 108 | public function update(Request $request, Championship $championship) |
||
| 138 | |||
| 139 | |||
| 140 | function getWinnerId($fighters, $scores, $numFighter) |
||
| 144 | } |
||
| 145 |
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: