Total Complexity | 60 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 | /** @internal */ |
||
8 | protected int $taskCount = 0; |
||
9 | /** @internal */ |
||
10 | protected bool $shouldFail = false; |
||
11 | |||
12 | /** |
||
13 | * Prints result of a test |
||
14 | * |
||
15 | * @internal |
||
16 | */ |
||
17 | protected function testResult(string $text, bool $success = true): void { |
||
18 | $this->incCounter(); |
||
19 | if($success) { |
||
20 | return; |
||
21 | } |
||
22 | echo "Test $this->taskCount failed. $text\n"; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Increases task counter |
||
27 | * |
||
28 | * @internal |
||
29 | */ |
||
30 | protected function incCounter(): void { |
||
31 | $this->taskCount++; |
||
32 | Environment::incCounter(); |
||
|
|||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Resets task counter |
||
37 | * |
||
38 | * @internal |
||
39 | */ |
||
40 | protected function resetCounter(): void { |
||
41 | $this->taskCount = 0; |
||
42 | Environment::resetCounter(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @internal |
||
47 | */ |
||
48 | public function getCounter(): int { |
||
49 | return $this->taskCount; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string|array $variable |
||
54 | */ |
||
55 | protected function showStringOrArray($variable): string { |
||
56 | return (is_string($variable) ? $variable : "(array)"); |
||
57 | } |
||
58 | |||
59 | protected function isSuccess(bool $success): bool { |
||
60 | if($this->shouldFail) { |
||
61 | $success = !$success; |
||
62 | } |
||
63 | return $success; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Tries an assertion |
||
68 | * |
||
69 | * @param mixed $code Assertion to try |
||
70 | */ |
||
71 | protected function assert($code, string $failureText = ""): void { |
||
72 | $success = $this->isSuccess($code == true); |
||
73 | if(!$success) { |
||
74 | $message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText; |
||
75 | } |
||
76 | $this->testResult($message ?? "", $success); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Are both values same? |
||
81 | * |
||
82 | * @param mixed $expected |
||
83 | * @param mixed $actual |
||
84 | */ |
||
85 | protected function assertSame($expected, $actual): void { |
||
86 | $success = $this->isSuccess($expected == $actual); |
||
87 | if(!$success) { |
||
88 | $message = "The value is not $expected but $actual."; |
||
89 | } |
||
90 | $this->testResult($message ?? "", $success); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Are not both values same? |
||
95 | * |
||
96 | * @param mixed $expected |
||
97 | * @param mixed $actual |
||
98 | */ |
||
99 | protected function assertNotSame($expected, $actual): void { |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Is $actual equal to true? |
||
109 | */ |
||
110 | protected function assertTrue(bool $actual): void { |
||
111 | $success = $this->isSuccess($actual); |
||
112 | if(!$success) { |
||
113 | $message = "The value is not true."; |
||
114 | } |
||
115 | $this->testResult($message ?? "", $success); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Is the expression true? |
||
120 | * |
||
121 | * @param mixed $actual |
||
122 | */ |
||
123 | protected function assertTruthy($actual): void { |
||
124 | $success = $this->isSuccess($actual == true); |
||
125 | if(!$success) { |
||
126 | $message = "The expression is not true."; |
||
127 | } |
||
128 | $this->testResult($message ?? "", $success); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Is $actual equal to false? |
||
133 | */ |
||
134 | protected function assertFalse(bool $actual): void { |
||
135 | $success = $this->isSuccess(!$actual); |
||
136 | if(!$success) { |
||
137 | $message = "The value is not false."; |
||
138 | } |
||
139 | $this->testResult($message ?? "", $success); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Is the expression false? |
||
144 | * |
||
145 | * @param mixed $actual |
||
146 | */ |
||
147 | protected function assertFalsey($actual): void { |
||
148 | $success = $this->isSuccess($actual == false); |
||
149 | if(!$success) { |
||
150 | $message = "The expression is not false."; |
||
151 | } |
||
152 | $this->testResult($message ?? "", $success); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Is the value null? |
||
157 | * |
||
158 | * @param mixed $actual |
||
159 | */ |
||
160 | protected function assertNull($actual): void { |
||
161 | $success = $this->isSuccess($actual == null); |
||
162 | if(!$success) { |
||
163 | $message = "The value is not null."; |
||
164 | } |
||
165 | $this->testResult($message ?? "", $success); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Is not the value null? |
||
170 | * |
||
171 | * @param mixed $actual |
||
172 | */ |
||
173 | protected function assertNotNull($actual): void { |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Does $actual contain $needle? |
||
183 | * |
||
184 | * @param string|array $needle |
||
185 | * @param string|array $actual |
||
186 | */ |
||
187 | protected function assertContains($needle, $actual): void { |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Does $actual not contain $needle? |
||
211 | * |
||
212 | * @param string|array $needle |
||
213 | * @param string|array $actual |
||
214 | */ |
||
215 | protected function assertNotContains($needle, $actual): void { |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Does $value contain $count items? |
||
239 | * |
||
240 | * @param string|array|\Countable $value |
||
241 | */ |
||
242 | protected function assertCount(int $count, $value): void { |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Does $value not contain $count items? |
||
258 | * |
||
259 | * @param string|array|\Countable $value |
||
260 | */ |
||
261 | protected function assertNotCount(int $count, $value): void { |
||
262 | if(!is_array($value) && !$value instanceof \Countable) { |
||
263 | $this->testResult("The variable is not array or countable object.", false); |
||
264 | return; |
||
265 | } |
||
266 | $success = $this->isSuccess(count($value) !== $count); |
||
267 | if($success) { |
||
268 | $this->testResult(""); |
||
269 | } else { |
||
270 | $actual = count($value); |
||
271 | $this->testResult("Count of the variable is $actual.", false); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Is $value of type $type? |
||
277 | * |
||
278 | * @param string|object $type |
||
279 | * @param mixed $value |
||
280 | */ |
||
281 | protected function assertType($type, $value): void { |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | ?> |