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 namespace Bedard\Shop\Api; |
||
| 7 | class CartApi extends ApiController |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Add an item to the cart. |
||
| 11 | * |
||
| 12 | * @param CartRepository $repository |
||
| 13 | * @return \Bedard\Shop\Models\CartItem |
||
| 14 | */ |
||
| 15 | View Code Duplication | public function add(CartRepository $repository) |
|
| 22 | |||
| 23 | /** |
||
| 24 | * Find the current cart. |
||
| 25 | * |
||
| 26 | * @param CartRepository $repository |
||
| 27 | * @return \Bedard\Shop\Models\Cart |
||
| 28 | */ |
||
| 29 | public function index(CartRepository $repository) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Remove an item from the cart. |
||
| 42 | * |
||
| 43 | * @param CartRepository $repository |
||
| 44 | * @return \Bedard\Shop\Models\CartItem |
||
| 45 | */ |
||
| 46 | public function remove(CartRepository $repository) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Touch a cart. |
||
| 55 | * |
||
| 56 | * @param CartRepository $repository |
||
| 57 | * @return \Bedard\Shop\Models\Cart |
||
| 58 | */ |
||
| 59 | public function touch(CartRepository $repository) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Add or remove quantity to an existing CartItem. |
||
| 66 | * |
||
| 67 | * @param CartRepository $repository |
||
| 68 | * @return \Bedard\Shop\Models\CartItem |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function update(CartRepository $repository) |
|
| 77 | } |
||
| 78 |