| Total Complexity | 57 |
| Total Lines | 228 |
| 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 { |
||
| 11 | return (is_string($variable) ? $variable : "(array)"); |
||
| 12 | } |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Tries an assertion |
||
| 16 | * |
||
| 17 | * @param mixed $code Assertion to try |
||
| 18 | * @param string $failureText Text to print on failure |
||
| 19 | */ |
||
| 20 | protected function assert($code, string $failureText = ""): void { |
||
| 21 | $success = ($code == true); |
||
| 22 | if(Environment::getShouldFail()) { |
||
| 23 | $success = !$success; |
||
| 24 | } |
||
| 25 | if(!$success) { |
||
| 26 | $message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText; |
||
| 27 | } |
||
| 28 | Environment::testResult($message ?? "", $success); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Are both values same? |
||
| 33 | * |
||
| 34 | * @param mixed $expected |
||
| 35 | * @param mixed $actual |
||
| 36 | */ |
||
| 37 | protected function assertSame($expected, $actual): void { |
||
| 38 | $success = ($expected == $actual); |
||
| 39 | if(Environment::getShouldFail()) { |
||
| 40 | $success = !$success; |
||
| 41 | } |
||
| 42 | if(!$success) { |
||
| 43 | $message = "The value is not $expected but $actual."; |
||
| 44 | } |
||
| 45 | Environment::testResult($message ?? "", $success); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Are not both values same? |
||
| 50 | * |
||
| 51 | * @param mixed $expected |
||
| 52 | * @param mixed $actual |
||
| 53 | */ |
||
| 54 | protected function assertNotSame($expected, $actual): void { |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Is the expression true? |
||
| 67 | * |
||
| 68 | * @param mixed $actual |
||
| 69 | */ |
||
| 70 | protected function assertTrue($actual): void { |
||
| 71 | $success = ($actual == true); |
||
| 72 | if(Environment::getShouldFail()) { |
||
| 73 | $success = !$success; |
||
| 74 | } |
||
| 75 | if(!$success) { |
||
| 76 | $message = "The expression is not true."; |
||
| 77 | } |
||
| 78 | Environment::testResult($message ?? "", $success); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Is the expression false? |
||
| 83 | * |
||
| 84 | * @param mixed $actual |
||
| 85 | */ |
||
| 86 | protected function assertFalse($actual): void { |
||
| 87 | $success = ($actual == false); |
||
| 88 | if(Environment::getShouldFail()) { |
||
| 89 | $success = !$success; |
||
| 90 | } |
||
| 91 | if(!$success) { |
||
| 92 | $message = "The expression is not false."; |
||
| 93 | } |
||
| 94 | Environment::testResult($message ?? "", $success); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Is the value null? |
||
| 99 | * |
||
| 100 | * @param mixed $actual |
||
| 101 | */ |
||
| 102 | protected function assertNull($actual): void { |
||
| 103 | $success = ($actual == null); |
||
| 104 | if(Environment::getShouldFail()) { |
||
| 105 | $success = !$success; |
||
| 106 | } |
||
| 107 | if(!$success) { |
||
| 108 | $message = "The value is not null."; |
||
| 109 | } |
||
| 110 | Environment::testResult($message ?? "", $success); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Is not the value null? |
||
| 115 | * |
||
| 116 | * @param mixed $actual |
||
| 117 | */ |
||
| 118 | protected function assertNotNull($actual): void { |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Does $actual contain $needle? |
||
| 131 | * |
||
| 132 | * @param string|array $needle |
||
| 133 | * @param string|array $actual |
||
| 134 | */ |
||
| 135 | protected function assertContains($needle, $actual): void { |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Does $actual not contain $needle? |
||
| 157 | * |
||
| 158 | * @param string|array $needle |
||
| 159 | * @param string|array $actual |
||
| 160 | */ |
||
| 161 | protected function assertNotContains($needle, $actual): void { |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Does $value contain $count items? |
||
| 183 | * |
||
| 184 | * @param string|array|\Countable $value |
||
| 185 | */ |
||
| 186 | protected function assertCount(int $count, $value): void { |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Does $value not contain $count items? |
||
| 199 | * |
||
| 200 | * @param string|array|\Countable $value |
||
| 201 | */ |
||
| 202 | protected function assertNotCount(int $count, $value): void { |
||
| 203 | if(!is_array($value) && !$value instanceof \Countable) { |
||
| 204 | Environment::testResult("The variable is not array or countable object.", false); |
||
| 205 | } elseif(count($value) === $count) { |
||
| 206 | $actual = count($value); |
||
| 207 | Environment::testResult("Count of the variable is $actual.", false); |
||
| 208 | } else { |
||
| 209 | Environment::testResult(""); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Is $value of type $type? |
||
| 215 | * |
||
| 216 | * @param string|object $type |
||
| 217 | * @param mixed $value |
||
| 218 | */ |
||
| 219 | protected function assertType($type, $value): void { |
||
| 234 | } |
||
| 235 | } |
||
| 237 | ?> |