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 |
||
21 | class FavoriteController extends ApiController { |
||
22 | |||
23 | private $userId; |
||
24 | private $favoriteMapper; |
||
25 | |||
26 | public function __construct($appName, IRequest $request, FavoriteMapper $favoriteMapper, $userId) { |
||
31 | |||
32 | /** |
||
33 | * @NoAdminRequired |
||
34 | * |
||
35 | * @param $name string |
||
36 | * @param $id int |
||
37 | * @return JSONResponse |
||
38 | */ |
||
39 | View Code Duplication | public function updateFavorite($name, $id) { |
|
56 | |||
57 | /** |
||
58 | * @NoAdminRequired |
||
59 | * |
||
60 | * @param $lat float |
||
61 | * @param $lng float |
||
62 | * @return JSONResponse |
||
63 | */ |
||
64 | public function addFavorite($lat, $lng, $name = null){ |
||
78 | |||
79 | /** |
||
80 | * @NoAdminRequired |
||
81 | * |
||
82 | * @return JSONResponse |
||
83 | */ |
||
84 | public function getFavorites(){ |
||
88 | |||
89 | /** |
||
90 | * @NoAdminRequired |
||
91 | * |
||
92 | * @param $name string |
||
93 | * |
||
94 | * @return JSONResponse |
||
95 | */ |
||
96 | public function getFavoritesByName($name){ |
||
100 | |||
101 | /** |
||
102 | * @NoAdminRequired |
||
103 | * |
||
104 | * @param $id int |
||
105 | * @return JSONResponse |
||
106 | */ |
||
107 | public function removeFavorite($id){ |
||
114 | |||
115 | } |
||
116 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.