| Total Complexity | 56 |
| Total Lines | 254 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 3 | Features | 1 |
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.
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 |
||
| 12 | final class Assert { |
||
| 13 | use \Nette\StaticClass; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param string|array $variable |
||
| 17 | */ |
||
| 18 | private static function showStringOrArray($variable): string { |
||
| 19 | return (is_string($variable) ? $variable : "(array)"); |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Tries an assertion |
||
| 24 | * |
||
| 25 | * @param mixed $code Assertion to try |
||
| 26 | * @param string $failureText Text to print on failure |
||
| 27 | * @deprecated |
||
| 28 | * @see TAssertions::assert() |
||
| 29 | */ |
||
| 30 | public static function tryAssertion($code, string $failureText = ""): void { |
||
| 31 | $success = ($code == true); |
||
| 32 | if(Environment::getShouldFail()) { |
||
| 33 | $success = !$success; |
||
| 34 | } |
||
| 35 | if(!$success) { |
||
| 36 | $message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText; |
||
| 37 | } |
||
| 38 | Environment::testResult($message ?? "", $success); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Are both values same? |
||
| 43 | * |
||
| 44 | * @param mixed $expected |
||
| 45 | * @param mixed $actual |
||
| 46 | * @deprecated |
||
| 47 | * @see TAssertions::assertSame() |
||
| 48 | */ |
||
| 49 | public static function same($expected, $actual): void { |
||
| 50 | $success = ($expected == $actual); |
||
| 51 | if(Environment::getShouldFail()) { |
||
| 52 | $success = !$success; |
||
| 53 | } |
||
| 54 | if(!$success) { |
||
| 55 | $message = "The value is not $expected but $actual."; |
||
| 56 | } |
||
| 57 | Environment::testResult($message ?? "", $success); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Are not both values same? |
||
| 62 | * |
||
| 63 | * @param mixed $expected |
||
| 64 | * @param mixed $actual |
||
| 65 | * @deprecated |
||
| 66 | * @see TAssertions::assertNotSame() |
||
| 67 | */ |
||
| 68 | public static function notSame($expected, $actual): void { |
||
| 69 | $success = ($expected !== $actual); |
||
| 70 | if(Environment::getShouldFail()) { |
||
| 71 | $success = !$success; |
||
| 72 | } |
||
| 73 | if(!$success) { |
||
| 74 | $message = "The value is $expected."; |
||
| 75 | } |
||
| 76 | Environment::testResult($message ?? "", $success); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Is the expression true? |
||
| 81 | * |
||
| 82 | * @param mixed $actual |
||
| 83 | * @deprecated |
||
| 84 | * @see TAssertions::assertTrue() |
||
| 85 | */ |
||
| 86 | public static function true($actual): void { |
||
| 87 | $success = ($actual == true); |
||
| 88 | if(Environment::getShouldFail()) { |
||
| 89 | $success = !$success; |
||
| 90 | } |
||
| 91 | if(!$success) { |
||
| 92 | $message = "The expression is not true."; |
||
| 93 | } |
||
| 94 | Environment::testResult($message ?? "", $success); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Is the expression false? |
||
| 99 | * |
||
| 100 | * @param mixed $actual |
||
| 101 | * @deprecated |
||
| 102 | * @see TAssertions::assertFalse() |
||
| 103 | */ |
||
| 104 | public static function false($actual): void { |
||
| 105 | $success = ($actual == false); |
||
| 106 | if(Environment::getShouldFail()) { |
||
| 107 | $success = !$success; |
||
| 108 | } |
||
| 109 | if(!$success) { |
||
| 110 | $message = "The expression is not false."; |
||
| 111 | } |
||
| 112 | Environment::testResult($message ?? "", $success); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Is the value null? |
||
| 117 | * |
||
| 118 | * @param mixed $actual |
||
| 119 | * @deprecated |
||
| 120 | * @see TAssertions::assertNull() |
||
| 121 | */ |
||
| 122 | public static function null($actual): void { |
||
| 123 | $success = ($actual == null); |
||
| 124 | if(Environment::getShouldFail()) { |
||
| 125 | $success = !$success; |
||
| 126 | } |
||
| 127 | if(!$success) { |
||
| 128 | $message = "The value is not null."; |
||
| 129 | } |
||
| 130 | Environment::testResult($message ?? "", $success); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Is not the value null? |
||
| 135 | * |
||
| 136 | * @param mixed $actual |
||
| 137 | * @deprecated |
||
| 138 | * @see TAssertions::assertNotNull() |
||
| 139 | */ |
||
| 140 | public static function notNull($actual): void { |
||
| 141 | $success = ($actual !== null); |
||
| 142 | if(Environment::getShouldFail()) { |
||
| 143 | $success = !$success; |
||
| 144 | } |
||
| 145 | if(!$success) { |
||
| 146 | $message = "The value is null."; |
||
| 147 | } |
||
| 148 | Environment::testResult($message ?? "", $success); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Does $actual contain $needle? |
||
| 153 | * |
||
| 154 | * @param string|array $needle |
||
| 155 | * @param string|array $actual |
||
| 156 | * @deprecated |
||
| 157 | * @see TAssertions::assertContains() |
||
| 158 | */ |
||
| 159 | public static function contains($needle, $actual): void { |
||
| 160 | if(!is_string($needle) && !is_array($needle)) { |
||
| 161 | Environment::testResult("The variable is not string or array.", false); |
||
| 162 | } elseif(is_string($actual) && is_string($needle)) { |
||
| 163 | if($needle !== "" && strpos($actual, $needle) !== false) { |
||
| 164 | Environment::testResult(""); |
||
| 165 | } else { |
||
| 166 | Environment::testResult("$needle is not in the variable.", false); |
||
| 167 | } |
||
| 168 | } elseif(is_array($actual)) { |
||
| 169 | if(in_array($needle, $actual)) { |
||
| 170 | Environment::testResult(self::showStringOrArray($needle) . " is in the variable."); |
||
| 171 | } else { |
||
| 172 | Environment::testResult(self::showStringOrArray($needle) . " is not in the variable.", false); |
||
| 173 | } |
||
| 174 | } else { |
||
| 175 | Environment::testResult(self::showStringOrArray($needle) . " is not in the variable.", false); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Does $actual not contain $needle? |
||
| 181 | * |
||
| 182 | * @param string|array $needle |
||
| 183 | * @param string|array $actual |
||
| 184 | * @deprecated |
||
| 185 | * @see TAssertions::assertNotContains() |
||
| 186 | */ |
||
| 187 | public static function notContains($needle, $actual): void { |
||
| 188 | if(!is_string($needle) && !is_array($needle)) { |
||
| 189 | Environment::testResult("The variable is not string or array.", false); |
||
| 190 | } elseif(is_string($actual) && is_string($needle)) { |
||
| 191 | if($needle === "" || strpos($actual, $needle) === false) { |
||
| 192 | Environment::testResult(""); |
||
| 193 | } else { |
||
| 194 | Environment::testResult("$needle is in the variable.", false); |
||
| 195 | } |
||
| 196 | } elseif(is_array($actual)) { |
||
| 197 | if(!in_array($needle, $actual)) { |
||
| 198 | Environment::testResult(""); |
||
| 199 | } else { |
||
| 200 | Environment::testResult(self::showStringOrArray($needle) . " is in the variable.", false); |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | Environment::testResult(self::showStringOrArray($needle) . " is not in the variable.", false); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Does $value contain $count items? |
||
| 209 | * |
||
| 210 | * @param string|array|\Countable $value |
||
| 211 | * @deprecated |
||
| 212 | * @see TAssertions::assertCount() |
||
| 213 | */ |
||
| 214 | public static function count(int $count, $value): void { |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Does $value not contain $count items? |
||
| 227 | * |
||
| 228 | * @param string|array|\Countable $value |
||
| 229 | * @deprecated |
||
| 230 | * @see TAssertions::assertNotCount() |
||
| 231 | */ |
||
| 232 | public static function notCount(int $count, $value): void { |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Is $value of type $type? |
||
| 245 | * |
||
| 246 | * @param string|object $type |
||
| 247 | * @param mixed $value |
||
| 248 | * @deprecated |
||
| 249 | * @see TAssertions::assertType() |
||
| 250 | */ |
||
| 251 | public static function type($type, $value): void { |
||
| 269 | ?> |