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 |
||
16 | class ResourceKeyResolver implements ResourceKeyResolverContract |
||
17 | { |
||
18 | /** |
||
19 | * Transformable to resource key mappings. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $bindings = []; |
||
24 | |||
25 | /** |
||
26 | * Register a transformable to resource key binding. |
||
27 | * |
||
28 | * @param string|array $transformable |
||
29 | * @param string $resourceKey |
||
30 | * @return void |
||
31 | */ |
||
32 | 2 | public function bind($transformable, string $resourceKey) |
|
38 | |||
39 | /** |
||
40 | * Resolve a resource key from the given data. |
||
41 | * |
||
42 | * @param mixed $data |
||
43 | * @return string |
||
44 | */ |
||
45 | 50 | View Code Duplication | public function resolve($data) |
59 | |||
60 | /** |
||
61 | * Resolve a resource key from the given model. |
||
62 | * |
||
63 | * @param \Illuminate\Database\Eloquent\Model $model |
||
64 | * @return string |
||
65 | */ |
||
66 | 43 | public function resolveFromModel(Model $model) |
|
74 | |||
75 | /** |
||
76 | * Resolve a transformable item from the given data. |
||
77 | * |
||
78 | * @param mixed $data |
||
79 | * @return mixed |
||
80 | */ |
||
81 | 50 | protected function resolveTransformableItem($data) |
|
91 | } |
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.