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 |
||
5 | class StrategyEnum |
||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | private $value; |
||
11 | |||
12 | /** |
||
13 | * @var self[] |
||
14 | */ |
||
15 | static protected $registry = []; |
||
16 | |||
17 | private function __construct($value) |
||
21 | |||
22 | |||
23 | /** |
||
24 | * @return self |
||
25 | */ |
||
26 | public static function FIRST_VOTE_DECIDES() |
||
30 | |||
31 | |||
32 | /** |
||
33 | * @return self |
||
34 | */ |
||
35 | public static function ALLOW_UNLESS_DENIED() |
||
39 | |||
40 | |||
41 | /** |
||
42 | * @return self |
||
43 | */ |
||
44 | public static function DENY_UNLESS_ALLOWED() |
||
48 | |||
49 | |||
50 | /** |
||
51 | * @return self |
||
52 | */ |
||
53 | public static function EVERYONE_MUST_ALLOW_TO_BE_ALLOWED() |
||
57 | |||
58 | |||
59 | /** |
||
60 | * @return self |
||
61 | */ |
||
62 | public static function EVERYONE_MUST_DENY_TO_BE_DENIED() |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @param string $string |
||
70 | * @return self |
||
71 | */ |
||
72 | View Code Duplication | protected static function instance($string) |
|
79 | |||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function __toString() |
||
88 | } |
||
89 |
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.