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 ServiceTransformer extends TransformerAbstract |
|
|
|||
32 | { |
||
33 | /** |
||
34 | * List of resources that can be included. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $availableIncludes = [ |
||
39 | 'options', |
||
40 | 'servers', |
||
41 | 'packs', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * The Illuminate Request object if provided. |
||
46 | * |
||
47 | * @var \Illuminate\Http\Request|bool |
||
48 | */ |
||
49 | protected $request; |
||
50 | |||
51 | /** |
||
52 | * Setup request object for transformer. |
||
53 | * |
||
54 | * @param \Illuminate\Http\Request|bool $request |
||
55 | * @return void |
||
56 | */ |
||
57 | public function __construct($request = false) |
||
65 | |||
66 | /** |
||
67 | * Return a generic transformed service array. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | public function transform(Service $service) |
||
75 | |||
76 | /** |
||
77 | * Return the the service options. |
||
78 | * |
||
79 | * @return \Leauge\Fractal\Resource\Collection |
||
80 | */ |
||
81 | public function includeOptions(Service $service) |
||
89 | |||
90 | /** |
||
91 | * Return the servers associated with this service. |
||
92 | * |
||
93 | * @return \Leauge\Fractal\Resource\Collection |
||
94 | */ |
||
95 | public function includeServers(Service $service) |
||
103 | |||
104 | /** |
||
105 | * Return the packs associated with this service. |
||
106 | * |
||
107 | * @return \Leauge\Fractal\Resource\Collection |
||
108 | */ |
||
109 | public function includePacks(Service $service) |
||
117 | } |
||
118 |
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.