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 |
||
10 | View Code Duplication | class PushNotificationDevicesController extends BaseApiController |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * List of all route actions that the base api controller |
||
14 | * will skip permissions check for them. |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $skipPermissionCheck = ['registerDevice']; |
||
18 | |||
19 | /** |
||
20 | * The validations rules used by the base api controller |
||
21 | * to check before add. |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $validationRules = [ |
||
25 | 'device_token' => 'required|string|max:255', |
||
26 | 'user_id' => 'required|exists:users,id' |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * Init new object. |
||
31 | * |
||
32 | * @param PushNotificationDeviceRepository $repo |
||
33 | * @param CoreConfig $config |
||
34 | * @return void |
||
35 | */ |
||
36 | public function __construct(PushNotificationDeviceRepository $repo, CoreConfig $config) |
||
40 | |||
41 | /** |
||
42 | * Register the given device to the logged in user. |
||
43 | * |
||
44 | * @param \Illuminate\Http\Request $request |
||
45 | * @return \Illuminate\Http\Response |
||
46 | */ |
||
47 | public function registerDevice(Request $request) |
||
55 | } |
||
56 |
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.