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 |
||
31 | View Code Duplication | class UserTransformer extends TransformerAbstract |
|
|
|||
32 | { |
||
33 | /** |
||
34 | * List of resources that can be included. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $availableIncludes = [ |
||
39 | 'access', |
||
40 | 'servers', |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * The Illuminate Request object if provided. |
||
45 | * |
||
46 | * @var \Illuminate\Http\Request|bool |
||
47 | */ |
||
48 | protected $request; |
||
49 | |||
50 | /** |
||
51 | * Setup request object for transformer. |
||
52 | * |
||
53 | * @param \Illuminate\Http\Request|bool $request |
||
54 | * @return void |
||
55 | */ |
||
56 | public function __construct($request = false) |
||
64 | |||
65 | /** |
||
66 | * Return a generic transformed subuser array. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | public function transform(User $user) |
||
74 | |||
75 | /** |
||
76 | * Return the servers associated with this user. |
||
77 | * |
||
78 | * @return \Leauge\Fractal\Resource\Collection |
||
79 | */ |
||
80 | public function includeServers(User $user) |
||
88 | |||
89 | /** |
||
90 | * Return the servers that this user can access. |
||
91 | * |
||
92 | * @return \Leauge\Fractal\Resource\Collection |
||
93 | */ |
||
94 | public function includeAccess(User $user) |
||
102 | } |
||
103 |
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.