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:
Complex classes like Assert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assert, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class Assert { |
||
14 | use \Nette\StaticClass; |
||
15 | |||
16 | /** |
||
17 | * Tries an assertion |
||
18 | * |
||
19 | * @param mixed $code Assertion to try |
||
20 | * @param string $successText Text to print on success |
||
21 | * @param string $failureText Text to print on failure |
||
22 | */ |
||
23 | public static function tryAssertion($code, string $successText = "", string $failureText = ""): void { |
||
33 | |||
34 | /** |
||
35 | * Are both values same? |
||
36 | * |
||
37 | * @param mixed $expected |
||
38 | * @param mixed $actual |
||
39 | */ |
||
40 | public static function same($expected, $actual): void { |
||
50 | |||
51 | /** |
||
52 | * Are not both values same? |
||
53 | * |
||
54 | * @param mixed $expected |
||
55 | * @param mixed $actual |
||
56 | */ |
||
57 | View Code Duplication | public static function notSame($expected, $actual): void { |
|
67 | |||
68 | /** |
||
69 | * Is the expression true? |
||
70 | * |
||
71 | * @param mixed $actual |
||
72 | */ |
||
73 | View Code Duplication | public static function true($actual): void { |
|
83 | |||
84 | /** |
||
85 | * Is the expression false? |
||
86 | * |
||
87 | * @param mixed $actual |
||
88 | */ |
||
89 | View Code Duplication | public static function false($actual): void { |
|
99 | |||
100 | /** |
||
101 | * Is the value null? |
||
102 | * |
||
103 | * @param mixed $actual |
||
104 | */ |
||
105 | View Code Duplication | public static function null($actual): void { |
|
115 | |||
116 | /** |
||
117 | * Is not the value null? |
||
118 | * |
||
119 | * @param mixed $actual |
||
120 | */ |
||
121 | View Code Duplication | public static function notNull($actual): void { |
|
131 | |||
132 | /** |
||
133 | * Does $actual contain $needle? |
||
134 | * |
||
135 | * @param string|array $needle |
||
136 | * @param string|array $actual |
||
137 | */ |
||
138 | public static function contains($needle, $actual): void { |
||
157 | |||
158 | /** |
||
159 | * Does $actual not contain $needle? |
||
160 | * |
||
161 | * @param string|array $needle |
||
162 | * @param string|array $actual |
||
163 | */ |
||
164 | public static function notContains($needle, $actual): void { |
||
183 | |||
184 | /** |
||
185 | * Does $value contain $count items? |
||
186 | * |
||
187 | * @param string|array|\Countable $value |
||
188 | */ |
||
189 | View Code Duplication | public static function count(int $count, $value): void { |
|
199 | |||
200 | /** |
||
201 | * Does $value not contain $count items? |
||
202 | * |
||
203 | * @param string|array|\Countable $value |
||
204 | */ |
||
205 | View Code Duplication | public static function notCount(int $count, $value): void { |
|
215 | |||
216 | /** |
||
217 | * Is $value of type $type? |
||
218 | * |
||
219 | * @param string|object $type |
||
220 | * @param mixed $value |
||
221 | */ |
||
222 | public static function type($type, $value): void { |
||
239 | } |
||
240 | ?> |