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 TagController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * TaskController constructor. |
||
| 18 | */ |
||
| 19 | public function __construct(TagTransformer $tagTransformer) |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Display a listing of the resource. |
||
| 27 | * |
||
| 28 | * @return \Illuminate\Http\Response |
||
| 29 | */ |
||
| 30 | public function index($tagId = null) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Show the form for creating a new resource. |
||
| 42 | * |
||
| 43 | * @return \Illuminate\Http\Response |
||
| 44 | */ |
||
| 45 | public function create() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Store a newly created resource in storage. |
||
| 52 | * |
||
| 53 | * @param \Illuminate\Http\Request $request |
||
| 54 | * @return \Illuminate\Http\Response |
||
| 55 | */ |
||
| 56 | public function store() |
||
| 57 | { |
||
| 58 | |||
| 59 | View Code Duplication | if (! Input::get('name') or ! Input::get('done') or ! Input::get('priority')) |
|
| 60 | { |
||
| 61 | return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY) |
||
| 62 | ->respondWithError('Parameters failed validation for a tag.'); |
||
| 63 | } |
||
| 64 | Tag::create(Input::all()); |
||
| 65 | return $this->respondCreated('Tag successfully created.'); |
||
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Display the specified resource. |
||
| 71 | * |
||
| 72 | * @param int $id |
||
| 73 | * @return \Illuminate\Http\Response |
||
| 74 | */ |
||
| 75 | public function show($id) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Show the form for editing the specified resource. |
||
| 97 | * |
||
| 98 | * @param int $id |
||
| 99 | * @return \Illuminate\Http\Response |
||
| 100 | */ |
||
| 101 | public function edit($id) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Update the specified resource in storage. |
||
| 108 | * |
||
| 109 | * @param \Illuminate\Http\Request $request |
||
| 110 | * @param int $id |
||
| 111 | * @return \Illuminate\Http\Response |
||
| 112 | */ |
||
| 113 | public function update(Request $request, $id) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Remove the specified resource from storage. |
||
| 121 | * |
||
| 122 | * @param int $id |
||
| 123 | * @return \Illuminate\Http\Response |
||
| 124 | */ |
||
| 125 | public function destroy($id) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param Request $request |
||
| 132 | * @param $tag |
||
| 133 | */ |
||
| 134 | public function saveTag(Request $request, $tag) |
||
| 141 | |||
| 142 | } |
||
| 143 |
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: