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|null |
||
44 | */ |
||
45 | 5 | public function resolve($data) |
|
59 | |||
60 | /** |
||
61 | * Resolve a transformable from the given data. |
||
62 | * |
||
63 | * @param mixed $data |
||
64 | * @return mixed |
||
65 | */ |
||
66 | 5 | protected function resolveTransformable($data) |
|
76 | |||
77 | /** |
||
78 | * Resolve a resource key from the given model. |
||
79 | * |
||
80 | * @param \Illuminate\Database\Eloquent\Model $model |
||
81 | * @return string |
||
82 | */ |
||
83 | 2 | protected function resolveFromModel(Model $model) |
|
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.