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) |
||
| 22 | /** |
||
| 23 | * Display a listing of the resource. |
||
| 24 | * |
||
| 25 | * @return \Illuminate\Http\Response |
||
| 26 | */ |
||
| 27 | public function index() |
||
| 33 | /** |
||
| 34 | * Show the form for creating a new resource. |
||
| 35 | * |
||
| 36 | * @return \Illuminate\Http\Response |
||
| 37 | */ |
||
| 38 | public function create() |
||
| 42 | /** |
||
| 43 | * Store a newly created resource in storage. |
||
| 44 | * @return \Illuminate\Http\Response |
||
| 45 | */ |
||
| 46 | public function store() |
||
| 56 | /** |
||
| 57 | * Display the specified resource. |
||
| 58 | * |
||
| 59 | * @param int $id |
||
| 60 | * @return \Illuminate\Http\Response |
||
| 61 | */ |
||
| 62 | View Code Duplication | public function show($id) |
|
| 72 | /** |
||
| 73 | * Show the form for editing the specified resource. |
||
| 74 | * |
||
| 75 | * @param int $id |
||
| 76 | * @return \Illuminate\Http\Response |
||
| 77 | */ |
||
| 78 | public function edit($id) |
||
| 82 | /** |
||
| 83 | * Update the specified resource in storage. |
||
| 84 | * |
||
| 85 | * @param \Illuminate\Http\Request $request |
||
| 86 | * @param int $id |
||
| 87 | * @return \Illuminate\Http\Response |
||
| 88 | */ |
||
| 89 | public function update(Request $request, $id) |
||
| 101 | /** |
||
| 102 | * Remove the specified resource from storage. |
||
| 103 | * |
||
| 104 | * @param int $id |
||
| 105 | * @return \Illuminate\Http\Response |
||
| 106 | */ |
||
| 107 | public function destroy($id) |
||
| 111 | } |
||
| 112 |
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.