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 |
||
27 | class UuidGenerator extends AbstractIdGenerator |
||
28 | { |
||
29 | /** |
||
30 | * A unique environment value to salt each UUID with. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $salt = null; |
||
35 | |||
36 | 6 | View Code Duplication | public function __construct() |
44 | |||
45 | /** |
||
46 | * Used to set the salt that will be applied to each id |
||
47 | */ |
||
48 | 2 | public function setSalt(string $salt) : void |
|
52 | |||
53 | /** |
||
54 | * Returns the current salt value |
||
55 | * |
||
56 | * @return string $salt The current salt |
||
57 | */ |
||
58 | 1 | public function getSalt() : string |
|
62 | |||
63 | /** |
||
64 | * Checks that a given string is a valid uuid. |
||
65 | */ |
||
66 | 2 | public function isValid(string $uuid) : bool |
|
71 | |||
72 | /** |
||
73 | * Generates a new UUID |
||
74 | * |
||
75 | * @param DocumentManager $dm Not used. |
||
76 | * @param object $document Not used. |
||
77 | * |
||
78 | * @return string UUID |
||
79 | * |
||
80 | * @throws Exception |
||
81 | */ |
||
82 | 2 | public function generate(DocumentManager $dm, object $document) |
|
87 | |||
88 | /** |
||
89 | * Generates a v4 UUID |
||
90 | */ |
||
91 | 4 | public function generateV4() : string |
|
113 | |||
114 | /** |
||
115 | * Generates a v5 UUID |
||
116 | * |
||
117 | * @throws Exception When the provided namespace is invalid. |
||
118 | */ |
||
119 | 2 | public function generateV5(string $namespace, string $salt) : string |
|
156 | } |
||
157 |
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.