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 |
||
13 | class UuidDriver implements DriverInterface |
||
14 | { |
||
15 | /** |
||
16 | * Create an invite code. |
||
17 | * |
||
18 | * @return string |
||
19 | */ |
||
20 | 9 | public function code(): string |
|
32 | |||
33 | /** |
||
34 | * @return string |
||
35 | * @throws \Exception |
||
36 | */ |
||
37 | 1 | protected function createVersion1Uuid(): string |
|
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | * @throws \Throwable |
||
45 | */ |
||
46 | 3 | View Code Duplication | protected function createVersion3Uuid(): string |
|
|||
47 | { |
||
48 | 3 | throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 3'); |
|
49 | 2 | throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 3'); |
|
50 | |||
51 | 1 | return Uuid::uuid3(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString(); |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | 1 | protected function createVersion4Uuid(): string |
|
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | * @throws \Throwable |
||
66 | */ |
||
67 | 3 | View Code Duplication | protected function createVersion5Uuid(): string |
74 | } |
||
75 |
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.