Total Complexity | 56 |
Total Lines | 227 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TAssertions 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.
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 TAssertions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | trait TAssertions { |
||
7 | /** |
||
8 | * @param string|array $variable |
||
9 | */ |
||
10 | protected function showStringOrArray($variable): string { |
||
12 | } |
||
13 | |||
14 | /** |
||
15 | * Tries an assertion |
||
16 | * |
||
17 | * @param mixed $code Assertion to try |
||
18 | */ |
||
19 | protected function assert($code, string $failureText = ""): void { |
||
20 | $success = ($code == true); |
||
21 | if(Environment::getShouldFail()) { |
||
22 | $success = !$success; |
||
23 | } |
||
24 | if(!$success) { |
||
25 | $message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText; |
||
26 | } |
||
27 | Environment::testResult($message ?? "", $success); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Are both values same? |
||
32 | * |
||
33 | * @param mixed $expected |
||
34 | * @param mixed $actual |
||
35 | */ |
||
36 | protected function assertSame($expected, $actual): void { |
||
37 | $success = ($expected == $actual); |
||
38 | if(Environment::getShouldFail()) { |
||
39 | $success = !$success; |
||
40 | } |
||
41 | if(!$success) { |
||
42 | $message = "The value is not $expected but $actual."; |
||
43 | } |
||
44 | Environment::testResult($message ?? "", $success); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Are not both values same? |
||
49 | * |
||
50 | * @param mixed $expected |
||
51 | * @param mixed $actual |
||
52 | */ |
||
53 | protected function assertNotSame($expected, $actual): void { |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Is the expression true? |
||
66 | * |
||
67 | * @param mixed $actual |
||
68 | */ |
||
69 | protected function assertTrue($actual): void { |
||
70 | $success = ($actual == true); |
||
71 | if(Environment::getShouldFail()) { |
||
72 | $success = !$success; |
||
73 | } |
||
74 | if(!$success) { |
||
75 | $message = "The expression is not true."; |
||
76 | } |
||
77 | Environment::testResult($message ?? "", $success); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Is the expression false? |
||
82 | * |
||
83 | * @param mixed $actual |
||
84 | */ |
||
85 | protected function assertFalse($actual): void { |
||
86 | $success = ($actual == false); |
||
87 | if(Environment::getShouldFail()) { |
||
88 | $success = !$success; |
||
89 | } |
||
90 | if(!$success) { |
||
91 | $message = "The expression is not false."; |
||
92 | } |
||
93 | Environment::testResult($message ?? "", $success); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Is the value null? |
||
98 | * |
||
99 | * @param mixed $actual |
||
100 | */ |
||
101 | protected function assertNull($actual): void { |
||
102 | $success = ($actual == null); |
||
103 | if(Environment::getShouldFail()) { |
||
104 | $success = !$success; |
||
105 | } |
||
106 | if(!$success) { |
||
107 | $message = "The value is not null."; |
||
108 | } |
||
109 | Environment::testResult($message ?? "", $success); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Is not the value null? |
||
114 | * |
||
115 | * @param mixed $actual |
||
116 | */ |
||
117 | protected function assertNotNull($actual): void { |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Does $actual contain $needle? |
||
130 | * |
||
131 | * @param string|array $needle |
||
132 | * @param string|array $actual |
||
133 | */ |
||
134 | protected function assertContains($needle, $actual): void { |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Does $actual not contain $needle? |
||
156 | * |
||
157 | * @param string|array $needle |
||
158 | * @param string|array $actual |
||
159 | */ |
||
160 | protected function assertNotContains($needle, $actual): void { |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Does $value contain $count items? |
||
182 | * |
||
183 | * @param string|array|\Countable $value |
||
184 | */ |
||
185 | protected function assertCount(int $count, $value): void { |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Does $value not contain $count items? |
||
198 | * |
||
199 | * @param string|array|\Countable $value |
||
200 | */ |
||
201 | protected function assertNotCount(int $count, $value): void { |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Is $value of type $type? |
||
214 | * |
||
215 | * @param string|object $type |
||
216 | * @param mixed $value |
||
217 | */ |
||
218 | protected function assertType($type, $value): void { |
||
236 | ?> |