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 |
||
33 | class AlnumGenerator extends IncrementGenerator |
||
34 | { |
||
35 | /** @var int|null */ |
||
36 | protected $pad = null; |
||
37 | |||
38 | /** @var bool */ |
||
39 | protected $awkwardSafeMode = false; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $awkwardSafeChars = '0123456789BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz'; |
||
46 | |||
47 | 1 | View Code Duplication | public function __construct() |
55 | |||
56 | /** |
||
57 | * Set padding on generated id |
||
58 | */ |
||
59 | public function setPad(int $pad) : void |
||
63 | |||
64 | /** |
||
65 | * Enable awkwardSafeMode character set |
||
66 | */ |
||
67 | public function setAwkwardSafeMode(bool $awkwardSafeMode = false) : void |
||
71 | |||
72 | /** |
||
73 | * Set the character set used for ID generation |
||
74 | */ |
||
75 | 1 | public function setChars(string $chars) : void |
|
79 | |||
80 | /** @inheritDoc */ |
||
81 | 1 | public function generate(DocumentManager $dm, object $document) |
|
99 | } |
||
100 |
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.