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 |
||
| 13 | class MpesaController extends Controller |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var Mpesa |
||
| 17 | */ |
||
| 18 | private $repository; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * MpesaController constructor. |
||
| 22 | * @param Mpesa $repository |
||
| 23 | */ |
||
| 24 | public function __construct(Mpesa $repository) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param Request $request |
||
| 31 | * @param string|null $initiator |
||
| 32 | * @return \Illuminate\Http\JsonResponse |
||
| 33 | */ |
||
| 34 | public function timeout(Request $request, $initiator = null) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string|null $initiator |
||
| 48 | * @return \Illuminate\Http\JsonResponse |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function result($initiator = null) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param string|null $initiator |
||
| 64 | * @return \Illuminate\Http\JsonResponse |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function paymentCallback($initiator) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param Request $request |
||
| 79 | * @return \Illuminate\Http\JsonResponse |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function confirmation(Request $request) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return \Illuminate\Http\JsonResponse |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function callback() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param Request $request |
||
| 107 | * @return \Illuminate\Http\JsonResponse |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function stkCallback(Request $request) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return \Illuminate\Http\JsonResponse |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function validatePayment() |
|
| 132 | } |
||
| 133 |
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: