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 |
||
| 27 | class FrontendController extends Controller |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Data for view |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $_arViewData = []; |
||
| 35 | |||
| 36 | protected $_arViewGeneratedData = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check if view exists, if so - return, if not throw 404 |
||
| 40 | * |
||
| 41 | * @param string $strView |
||
| 42 | * @return View|void |
||
| 43 | */ |
||
| 44 | protected function _showViewOr404($strView){ |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Save all inputs to session |
||
| 57 | * @param $request |
||
| 58 | */ |
||
| 59 | protected function _saveSessionFields($request){ |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Reset all session fields |
||
| 70 | */ |
||
| 71 | protected function _resetSessionFields($request){ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Calculate price |
||
| 78 | * |
||
| 79 | * @param $request |
||
| 80 | */ |
||
| 81 | protected function _calculatePrice($request){ |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get frontend fields |
||
| 88 | * |
||
| 89 | * @param $request |
||
| 90 | * @return mixed |
||
| 91 | */ |
||
| 92 | protected function _getSessionFields($request){ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Load page by URL |
||
| 114 | * |
||
| 115 | * @param $url |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | protected function _loadPageByUrl($url){ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Load page by id |
||
| 125 | * |
||
| 126 | * @param $id |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | protected function _loadPageById($id){ |
||
| 132 | |||
| 133 | protected function _getSettings($id){ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Save transaction |
||
| 147 | * |
||
| 148 | * @param int $typeId |
||
| 149 | * @param string $text |
||
| 150 | * @param $userId |
||
| 151 | * @param $amount |
||
| 152 | */ |
||
| 153 | View Code Duplication | protected function _saveTransation($status_id = 1, $user_id, $amount, |
|
| 175 | } |
||
| 176 |
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: