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 |
||
12 | class ClassEnsurance |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $class; |
||
18 | |||
19 | use EnforcementTrait; |
||
20 | |||
21 | /** |
||
22 | * ClassEnsurance constructor. |
||
23 | * |
||
24 | * @param string $class |
||
25 | * |
||
26 | * @throws EnsuranceException |
||
27 | */ |
||
28 | public function __construct(string $class) |
||
36 | |||
37 | /** |
||
38 | * @param string $class |
||
39 | * |
||
40 | * @return ClassEnsurance |
||
41 | */ |
||
42 | public function is(string $class): ClassEnsurance |
||
48 | |||
49 | /** |
||
50 | * @param string $class |
||
51 | * |
||
52 | * @return ClassEnsurance |
||
53 | */ |
||
54 | public function isNot(string $class): ClassEnsurance |
||
60 | |||
61 | /** |
||
62 | * @param string $class |
||
63 | * |
||
64 | * @return ClassEnsurance |
||
65 | */ |
||
66 | public function extends (string $class): ClassEnsurance |
||
72 | |||
73 | /** |
||
74 | * @param string $class |
||
75 | * |
||
76 | * @return ClassEnsurance |
||
77 | */ |
||
78 | public function extendsNot(string $class): ClassEnsurance |
||
84 | |||
85 | /** |
||
86 | * @param string $interface |
||
87 | * |
||
88 | * @return ClassEnsurance |
||
89 | */ |
||
90 | View Code Duplication | public function implements (string $interface): ClassEnsurance |
|
97 | |||
98 | /** |
||
99 | * @param string $interface |
||
100 | * |
||
101 | * @return ClassEnsurance |
||
102 | */ |
||
103 | View Code Duplication | public function implementsNot(string $interface): ClassEnsurance |
|
110 | |||
111 | /** |
||
112 | * @param string $class |
||
113 | * |
||
114 | * @return ClassEnsurance |
||
115 | */ |
||
116 | View Code Duplication | public function isParentOf(string $class): ClassEnsurance |
|
123 | |||
124 | /** |
||
125 | * @param string $class |
||
126 | * |
||
127 | * @return ClassEnsurance |
||
128 | */ |
||
129 | View Code Duplication | public function isNotParentOf(string $class): ClassEnsurance |
|
136 | |||
137 | /** |
||
138 | * @param string $trait |
||
139 | * |
||
140 | * @return ClassEnsurance |
||
141 | */ |
||
142 | View Code Duplication | public function uses(string $trait): ClassEnsurance |
|
149 | |||
150 | /** |
||
151 | * @param string $property |
||
152 | * |
||
153 | * @return ClassEnsurance |
||
154 | */ |
||
155 | public function hasProperty(string $property): ClassEnsurance |
||
162 | |||
163 | /** |
||
164 | * @param string $method |
||
165 | * |
||
166 | * @return ClassEnsurance |
||
167 | */ |
||
168 | public function hasMethod(string $method): ClassEnsurance |
||
175 | } |