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 |
||
| 26 | class GroupsController extends Controller |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Shows the project groups view. |
||
| 30 | * |
||
| 31 | * @return \Illuminate\View\View |
||
| 32 | */ |
||
| 33 | public function indexAction() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Shows the group view. |
||
| 43 | * |
||
| 44 | * @return \Illuminate\View\View |
||
| 45 | */ |
||
| 46 | public function showAction($path) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Shows the new project view. |
||
| 57 | * |
||
| 58 | * @return \Illuminate\View\View |
||
| 59 | */ |
||
| 60 | public function newAction() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a new project. |
||
| 68 | * |
||
| 69 | * @return \Illuminate\Http\RedirectResponse |
||
| 70 | */ |
||
| 71 | public function createAction() |
||
| 72 | { |
||
| 73 | $groupData = Request::get('group'); |
||
| 74 | $groupData['type'] = 'group'; |
||
| 75 | $groupData['user_id'] = Auth::user()->id; |
||
| 76 | try { |
||
| 77 | $group = $this->dispatchFromArray(AddOwnerCommand::class, $groupData); |
||
| 78 | } catch (ValidationException $e) { |
||
| 79 | return Redirect::route('groups.new') |
||
| 80 | ->withInput(Request::all()) |
||
| 81 | ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('gitamin.groups.add.failure'))) |
||
| 82 | ->withErrors($e->getMessageBag()); |
||
| 83 | } catch (QueryException $e) { |
||
| 84 | return Redirect::route('groups.new') |
||
| 85 | ->withInput(Request::all()) |
||
| 86 | ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('gitamin.groups.add.failure'))) |
||
| 87 | ->withErrors('Path has been used'); |
||
| 88 | } |
||
| 89 | |||
| 90 | return Redirect::route('dashboard.groups.index') |
||
| 91 | ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('gitamin.groups.add.success'))); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Shows the edit project namespace view. |
||
| 96 | * |
||
| 97 | * @param \Gitamin\Models\Owner $namespace |
||
| 98 | * |
||
| 99 | * @return \Illuminate\View\View |
||
| 100 | */ |
||
| 101 | public function editAction($path) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Updates a project namespace. |
||
| 112 | * |
||
| 113 | * @param \Gitamin\Models\Owner $namespace |
||
| 114 | * |
||
| 115 | * @return \Illuminate\Http\RedirectResponse |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function updateAction($path) |
|
| 135 | } |
||
| 136 |
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: