| Total Complexity | 54 |
| Total Lines | 245 |
| Duplicated Lines | 0 % |
| Coverage | 95.87% |
| Changes | 6 | ||
| 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 |
||
| 7 | trait TAssertions |
||
| 8 | { |
||
| 9 | /** @internal */ |
||
| 10 | protected int $taskCount = 0; |
||
| 11 | /** @internal */ |
||
| 12 | protected bool $shouldFail = false; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Prints result of a test |
||
| 16 | * |
||
| 17 | * @internal |
||
| 18 | */ |
||
| 19 | 1 | protected function testResult(string $text, bool $success = true): void |
|
| 20 | { |
||
| 21 | 1 | $this->incCounter(); |
|
| 22 | 1 | if ($success) { |
|
| 23 | 1 | return; |
|
| 24 | } |
||
| 25 | 1 | echo "Test $this->taskCount failed. $text\n"; |
|
| 26 | 1 | } |
|
| 27 | |||
| 28 | /** |
||
| 29 | * Increases task counter |
||
| 30 | * |
||
| 31 | * @internal |
||
| 32 | */ |
||
| 33 | 1 | protected function incCounter(): void |
|
| 34 | { |
||
| 35 | 1 | $this->taskCount++; |
|
| 36 | 1 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Resets task counter |
||
| 40 | * |
||
| 41 | * @internal |
||
| 42 | */ |
||
| 43 | 1 | protected function resetCounter(): void |
|
| 46 | 1 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @internal |
||
| 50 | */ |
||
| 51 | 1 | protected function getCounter(): int |
|
| 54 | } |
||
| 55 | |||
| 56 | protected function showStringOrArray(string|array $variable): string |
||
| 57 | { |
||
| 58 | return (is_string($variable) ? $variable : "(array)"); |
||
|
1 ignored issue
–
show
|
|||
| 59 | } |
||
| 60 | |||
| 61 | 1 | protected function isSuccess(bool $success): bool |
|
| 62 | { |
||
| 63 | 1 | if ($this->shouldFail) { |
|
| 64 | 1 | $success = !$success; |
|
| 65 | } |
||
| 66 | 1 | return $success; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Tries an assertion |
||
| 71 | */ |
||
| 72 | 1 | protected function assert(mixed $code, string $failureText = ""): void |
|
| 73 | { |
||
| 74 | 1 | $success = $this->isSuccess($code == true); |
|
| 75 | 1 | $message = ""; |
|
| 76 | 1 | if (!$success) { |
|
| 77 | $message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText; |
||
| 78 | } |
||
| 79 | 1 | $this->testResult($message, $success); |
|
| 80 | 1 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Are both values same? |
||
| 84 | */ |
||
| 85 | 1 | protected function assertSame(mixed $expected, mixed $actual): void |
|
| 86 | { |
||
| 87 | 1 | $success = $this->isSuccess($expected == $actual); |
|
| 88 | 1 | $message = ($success) ? "" : "The value is not $expected but $actual."; |
|
| 89 | 1 | $this->testResult($message, $success); |
|
| 90 | 1 | } |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Are not both values same? |
||
| 94 | */ |
||
| 95 | 1 | protected function assertNotSame(mixed $expected, mixed $actual): void |
|
| 100 | 1 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Is $actual equal to true? |
||
| 104 | */ |
||
| 105 | 1 | protected function assertTrue(bool $actual): void |
|
| 106 | { |
||
| 107 | 1 | $success = $this->isSuccess($actual); |
|
| 108 | 1 | $message = ($success) ? "" : "The value is not true."; |
|
| 109 | 1 | $this->testResult($message, $success); |
|
| 110 | 1 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Is the expression true? |
||
| 114 | */ |
||
| 115 | 1 | protected function assertTruthy(mixed $actual): void |
|
| 116 | { |
||
| 117 | 1 | $success = $this->isSuccess($actual == true); |
|
| 118 | 1 | $message = ($success) ? "" : "The expression is not true."; |
|
| 119 | 1 | $this->testResult($message, $success); |
|
| 120 | 1 | } |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Is $actual equal to false? |
||
| 124 | */ |
||
| 125 | 1 | protected function assertFalse(bool $actual): void |
|
| 126 | { |
||
| 127 | 1 | $success = $this->isSuccess(!$actual); |
|
| 128 | 1 | $message = ($success) ? "" : "The value is not false."; |
|
| 129 | 1 | $this->testResult($message, $success); |
|
| 130 | 1 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Is the expression false? |
||
| 134 | */ |
||
| 135 | 1 | protected function assertFalsey(mixed $actual): void |
|
| 136 | { |
||
| 137 | 1 | $success = $this->isSuccess($actual == false); |
|
| 138 | 1 | $message = ($success) ? "" : "The expression is not false."; |
|
| 139 | 1 | $this->testResult($message, $success); |
|
| 140 | 1 | } |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Is the value null? |
||
| 144 | */ |
||
| 145 | 1 | protected function assertNull(mixed $actual): void |
|
| 146 | { |
||
| 147 | 1 | $success = $this->isSuccess($actual == null); |
|
| 148 | 1 | $message = ($success) ? "" : "The value is not null."; |
|
| 149 | 1 | $this->testResult($message ?? "", $success); |
|
| 150 | 1 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Is not the value null? |
||
| 154 | */ |
||
| 155 | 1 | protected function assertNotNull(mixed $actual): void |
|
| 160 | 1 | } |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Does $actual contain $needle? |
||
| 164 | */ |
||
| 165 | 1 | protected function assertContains(string|array $needle, string|array $actual): void |
|
| 180 | } |
||
| 181 | 1 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Does $actual not contain $needle? |
||
| 185 | */ |
||
| 186 | 1 | protected function assertNotContains(string|array $needle, string|array $actual): void |
|
| 198 | } |
||
| 199 | 1 | } |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Does $value contain $count items? |
||
| 203 | */ |
||
| 204 | 1 | protected function assertCount(int $count, string|array|\Countable $value): void |
|
| 214 | 1 | } |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Does $value not contain $count items? |
||
| 218 | */ |
||
| 219 | 1 | protected function assertNotCount(int $count, string|array|\Countable $value): void |
|
| 220 | { |
||
| 221 | 1 | if (!is_array($value) && !$value instanceof \Countable) { |
|
| 222 | 1 | $this->testResult("The variable is not array or countable object.", false); |
|
| 223 | 1 | return; |
|
| 224 | } |
||
| 225 | 1 | $actual = count($value); |
|
| 226 | 1 | $success = $this->isSuccess($actual !== $count); |
|
| 227 | 1 | $message = ($success) ? "" : "Count of the variable is $actual."; |
|
| 228 | 1 | $this->testResult($message, $success); |
|
| 229 | 1 | } |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Is $value of type $type? |
||
| 233 | */ |
||
| 234 | 1 | protected function assertType(string|object $type, mixed $value): void |
|
| 252 | 1 | } |
|
| 253 | } |
||
| 254 |