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 |
||
| 14 | class UserController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Constructor |
||
| 18 | */ |
||
| 19 | public function __construct() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Show all the users. |
||
| 33 | * |
||
| 34 | * @return \Illuminate\View\View |
||
| 35 | */ |
||
| 36 | public function index(): View |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Show the user profile page. |
||
| 43 | * |
||
| 44 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View |
||
| 45 | */ |
||
| 46 | public function show(Request $request, $slug, $id) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Show the settings form. |
||
| 72 | * |
||
| 73 | * @return \Illuminate\View\View |
||
| 74 | */ |
||
| 75 | public function showSettingsForm(): View |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Handle an update request for the user. |
||
| 86 | * |
||
| 87 | * @param \Illuminate\Http\Request $request |
||
| 88 | * |
||
| 89 | * @return \Illuminate\Http\RedirectResponse |
||
| 90 | */ |
||
| 91 | public function update(Request $request): RedirectResponse |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Handle the delete request for the user. |
||
| 111 | * |
||
| 112 | * @param \Illuminate\Http\Request $request |
||
| 113 | * |
||
| 114 | * @return \Illuminate\Http\RedirectResponse |
||
| 115 | */ |
||
| 116 | public function delete(Request $request): RedirectResponse |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Handle a E-mail update request for the user. |
||
| 140 | * |
||
| 141 | * @param \Illuminate\Http\Request $request |
||
| 142 | * |
||
| 143 | * @return \Illuminate\Http\RedirectResponse |
||
| 144 | */ |
||
| 145 | protected function updateEmail(Request $request): RedirectResponse |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Handle a Password update request for the user. |
||
| 157 | * |
||
| 158 | * @param \Illuminate\Http\Request $request |
||
| 159 | * |
||
| 160 | * @return \Illuminate\Http\RedirectResponse |
||
| 161 | */ |
||
| 162 | protected function updatePassword(Request $request): RedirectResponse |
||
| 179 | } |
||
| 180 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: