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 |
||
| 11 | class TaskController extends ApiController |
||
| 12 | { |
||
| 13 | protected $taskTransformer; |
||
| 14 | /** |
||
| 15 | * TaskController constructor. |
||
| 16 | * @param $taskTransformer |
||
| 17 | */ |
||
| 18 | public function __construct(TaskTransformer $taskTransformer) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Display a listing of the resource. |
||
| 26 | * |
||
| 27 | * @return \Illuminate\Http\Response |
||
| 28 | */ |
||
| 29 | public function index() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Show the form for creating a new resource. |
||
| 37 | * |
||
| 38 | * @return \Illuminate\Http\Response |
||
| 39 | */ |
||
| 40 | public function create() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Store a newly created resource in storage. |
||
| 47 | * @return \Illuminate\Http\Response |
||
| 48 | */ |
||
| 49 | public function store() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Display the specified resource. |
||
| 64 | * |
||
| 65 | * @param int $id |
||
| 66 | * @return \Illuminate\Http\Response |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function show($id) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Show the form for editing the specified resource. |
||
| 83 | * |
||
| 84 | * @param int $id |
||
| 85 | * @return \Illuminate\Http\Response |
||
| 86 | */ |
||
| 87 | public function edit($id) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Update the specified resource in storage. |
||
| 94 | * |
||
| 95 | * @param \Illuminate\Http\Request $request |
||
| 96 | * @param int $id |
||
| 97 | * @return \Illuminate\Http\Response |
||
| 98 | */ |
||
| 99 | public function update(Request $request, $id) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Remove the specified resource from storage. |
||
| 116 | * |
||
| 117 | * @param int $id |
||
| 118 | * @return \Illuminate\Http\Response |
||
| 119 | */ |
||
| 120 | public function destroy($id) |
||
| 124 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.