Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 152 | class Assert |
||
|
|
|||
| 153 | { |
||
| 154 | 90 | public static function string($value, $message = '') |
|
| 155 | { |
||
| 156 | 90 | if (!is_string($value)) { |
|
| 157 | 14 | static::reportInvalidArgument(sprintf( |
|
| 158 | 14 | $message ?: 'Expected a string. Got: %s', |
|
| 159 | 14 | static::typeToString($value) |
|
| 160 | )); |
||
| 161 | } |
||
| 162 | 76 | } |
|
| 163 | |||
| 164 | 12 | public static function stringNotEmpty($value, $message = '') |
|
| 165 | { |
||
| 166 | 12 | static::string($value, $message); |
|
| 167 | 8 | static::notEmpty($value, $message); |
|
| 168 | 4 | } |
|
| 169 | |||
| 170 | 17 | View Code Duplication | public static function integer($value, $message = '') |
| 171 | { |
||
| 172 | 17 | if (!is_int($value)) { |
|
| 173 | 13 | static::reportInvalidArgument(sprintf( |
|
| 174 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
| 175 | 13 | static::typeToString($value) |
|
| 176 | )); |
||
| 177 | } |
||
| 178 | 4 | } |
|
| 179 | |||
| 180 | 16 | View Code Duplication | public static function integerish($value, $message = '') |
| 181 | { |
||
| 182 | 16 | if (!is_numeric($value) || $value != (int) $value) { |
|
| 183 | 4 | static::reportInvalidArgument(sprintf( |
|
| 184 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
| 185 | 4 | static::typeToString($value) |
|
| 186 | )); |
||
| 187 | } |
||
| 188 | 12 | } |
|
| 189 | |||
| 190 | 16 | View Code Duplication | public static function float($value, $message = '') |
| 191 | { |
||
| 192 | 16 | if (!is_float($value)) { |
|
| 193 | 8 | static::reportInvalidArgument(sprintf( |
|
| 194 | 8 | $message ?: 'Expected a float. Got: %s', |
|
| 195 | 8 | static::typeToString($value) |
|
| 196 | )); |
||
| 197 | } |
||
| 198 | 8 | } |
|
| 199 | |||
| 200 | 20 | View Code Duplication | public static function numeric($value, $message = '') |
| 201 | { |
||
| 202 | 20 | if (!is_numeric($value)) { |
|
| 203 | 4 | static::reportInvalidArgument(sprintf( |
|
| 204 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
| 205 | 4 | static::typeToString($value) |
|
| 206 | )); |
||
| 207 | } |
||
| 208 | 16 | } |
|
| 209 | |||
| 210 | 16 | View Code Duplication | public static function boolean($value, $message = '') |
| 211 | { |
||
| 212 | 16 | if (!is_bool($value)) { |
|
| 213 | 8 | static::reportInvalidArgument(sprintf( |
|
| 214 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
| 215 | 8 | static::typeToString($value) |
|
| 216 | )); |
||
| 217 | } |
||
| 218 | 8 | } |
|
| 219 | |||
| 220 | 23 | View Code Duplication | public static function scalar($value, $message = '') |
| 221 | { |
||
| 222 | 23 | if (!is_scalar($value)) { |
|
| 223 | 11 | static::reportInvalidArgument(sprintf( |
|
| 224 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
| 225 | 11 | static::typeToString($value) |
|
| 226 | )); |
||
| 227 | } |
||
| 228 | 12 | } |
|
| 229 | |||
| 230 | 23 | View Code Duplication | public static function object($value, $message = '') |
| 231 | { |
||
| 232 | 23 | if (!is_object($value)) { |
|
| 233 | 15 | static::reportInvalidArgument(sprintf( |
|
| 234 | 15 | $message ?: 'Expected an object. Got: %s', |
|
| 235 | 15 | static::typeToString($value) |
|
| 236 | )); |
||
| 237 | } |
||
| 238 | 8 | } |
|
| 239 | |||
| 240 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 241 | { |
||
| 242 | 16 | if (!is_resource($value)) { |
|
| 243 | 4 | static::reportInvalidArgument(sprintf( |
|
| 244 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
| 245 | 4 | static::typeToString($value) |
|
| 246 | )); |
||
| 247 | } |
||
| 248 | |||
| 249 | 12 | if ($type && $type !== get_resource_type($value)) { |
|
| 250 | 4 | static::reportInvalidArgument(sprintf( |
|
| 251 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
| 252 | 4 | static::typeToString($value), |
|
| 253 | 4 | $type |
|
| 254 | )); |
||
| 255 | } |
||
| 256 | 8 | } |
|
| 257 | |||
| 258 | 20 | View Code Duplication | public static function isCallable($value, $message = '') |
| 259 | { |
||
| 260 | 20 | if (!is_callable($value)) { |
|
| 261 | 8 | static::reportInvalidArgument(sprintf( |
|
| 262 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
| 263 | 8 | static::typeToString($value) |
|
| 264 | )); |
||
| 265 | } |
||
| 266 | 12 | } |
|
| 267 | |||
| 268 | 20 | View Code Duplication | public static function isArray($value, $message = '') |
| 269 | { |
||
| 270 | 20 | if (!is_array($value)) { |
|
| 271 | 12 | static::reportInvalidArgument(sprintf( |
|
| 272 | 12 | $message ?: 'Expected an array. Got: %s', |
|
| 273 | 12 | static::typeToString($value) |
|
| 274 | )); |
||
| 275 | } |
||
| 276 | 8 | } |
|
| 277 | |||
| 278 | 516 | View Code Duplication | public static function isTraversable($value, $message = '') |
| 279 | { |
||
| 280 | 516 | if (!is_array($value) && !($value instanceof Traversable)) { |
|
| 281 | 8 | static::reportInvalidArgument(sprintf( |
|
| 282 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
| 283 | 8 | static::typeToString($value) |
|
| 284 | )); |
||
| 285 | } |
||
| 286 | 512 | } |
|
| 287 | |||
| 288 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 289 | { |
||
| 290 | 20 | if (!is_array($value) && !($value instanceof ArrayAccess)) { |
|
| 291 | 8 | static::reportInvalidArgument(sprintf( |
|
| 292 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
| 293 | 8 | static::typeToString($value) |
|
| 294 | )); |
||
| 295 | } |
||
| 296 | 12 | } |
|
| 297 | |||
| 298 | 16 | View Code Duplication | public static function isInstanceOf($value, $class, $message = '') |
| 299 | { |
||
| 300 | 16 | if (!($value instanceof $class)) { |
|
| 301 | 12 | static::reportInvalidArgument(sprintf( |
|
| 302 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
| 303 | 12 | static::typeToString($value), |
|
| 304 | 12 | $class |
|
| 305 | )); |
||
| 306 | } |
||
| 307 | 4 | } |
|
| 308 | |||
| 309 | 16 | View Code Duplication | public static function notInstanceOf($value, $class, $message = '') |
| 310 | { |
||
| 311 | 16 | if ($value instanceof $class) { |
|
| 312 | 4 | static::reportInvalidArgument(sprintf( |
|
| 313 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
| 314 | 4 | static::typeToString($value), |
|
| 315 | 4 | $class |
|
| 316 | )); |
||
| 317 | } |
||
| 318 | 12 | } |
|
| 319 | |||
| 320 | 23 | public static function isEmpty($value, $message = '') |
|
| 321 | { |
||
| 322 | 23 | if (!empty($value)) { |
|
| 323 | 8 | static::reportInvalidArgument(sprintf( |
|
| 324 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
| 325 | 8 | static::valueToString($value) |
|
| 326 | )); |
||
| 327 | } |
||
| 328 | 15 | } |
|
| 329 | |||
| 330 | 31 | public static function notEmpty($value, $message = '') |
|
| 331 | { |
||
| 332 | 31 | if (empty($value)) { |
|
| 333 | 19 | static::reportInvalidArgument(sprintf( |
|
| 334 | 19 | $message ?: 'Expected a non-empty value. Got: %s', |
|
| 335 | 19 | static::valueToString($value) |
|
| 336 | )); |
||
| 337 | } |
||
| 338 | 12 | } |
|
| 339 | |||
| 340 | 11 | public static function null($value, $message = '') |
|
| 341 | { |
||
| 342 | 11 | if (null !== $value) { |
|
| 343 | 8 | static::reportInvalidArgument(sprintf( |
|
| 344 | 8 | $message ?: 'Expected null. Got: %s', |
|
| 345 | 8 | static::valueToString($value) |
|
| 346 | )); |
||
| 347 | } |
||
| 348 | 3 | } |
|
| 349 | |||
| 350 | 11 | public static function notNull($value, $message = '') |
|
| 351 | { |
||
| 352 | 11 | if (null === $value) { |
|
| 353 | 3 | static::reportInvalidArgument( |
|
| 354 | 3 | $message ?: 'Expected a value other than null.' |
|
| 355 | ); |
||
| 356 | } |
||
| 357 | 8 | } |
|
| 358 | |||
| 359 | 15 | public static function true($value, $message = '') |
|
| 360 | { |
||
| 361 | 15 | if (true !== $value) { |
|
| 362 | 11 | static::reportInvalidArgument(sprintf( |
|
| 363 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
| 364 | 11 | static::valueToString($value) |
|
| 365 | )); |
||
| 366 | } |
||
| 367 | 4 | } |
|
| 368 | |||
| 369 | 19 | public static function false($value, $message = '') |
|
| 370 | { |
||
| 371 | 19 | if (false !== $value) { |
|
| 372 | 15 | static::reportInvalidArgument(sprintf( |
|
| 373 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
| 374 | 15 | static::valueToString($value) |
|
| 375 | )); |
||
| 376 | } |
||
| 377 | 4 | } |
|
| 378 | |||
| 379 | 32 | public static function eq($value, $value2, $message = '') |
|
| 380 | { |
||
| 381 | 32 | if ($value2 != $value) { |
|
| 382 | 16 | static::reportInvalidArgument(sprintf( |
|
| 383 | 16 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 384 | 16 | static::valueToString($value), |
|
| 385 | 16 | static::valueToString($value2) |
|
| 386 | )); |
||
| 387 | } |
||
| 388 | 16 | } |
|
| 389 | |||
| 390 | 16 | public static function notEq($value, $value2, $message = '') |
|
| 391 | { |
||
| 392 | 16 | if ($value2 == $value) { |
|
| 393 | 12 | static::reportInvalidArgument(sprintf( |
|
| 394 | 12 | $message ?: 'Expected a different value than %s.', |
|
| 395 | 12 | static::valueToString($value2) |
|
| 396 | )); |
||
| 397 | } |
||
| 398 | 4 | } |
|
| 399 | |||
| 400 | 16 | public static function same($value, $value2, $message = '') |
|
| 401 | { |
||
| 402 | 16 | if ($value2 !== $value) { |
|
| 403 | 12 | static::reportInvalidArgument(sprintf( |
|
| 404 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 405 | 12 | static::valueToString($value), |
|
| 406 | 12 | static::valueToString($value2) |
|
| 407 | )); |
||
| 408 | } |
||
| 409 | 4 | } |
|
| 410 | |||
| 411 | 16 | public static function notSame($value, $value2, $message = '') |
|
| 412 | { |
||
| 413 | 16 | if ($value2 === $value) { |
|
| 414 | 4 | static::reportInvalidArgument(sprintf( |
|
| 415 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
| 416 | 4 | static::valueToString($value2) |
|
| 417 | )); |
||
| 418 | } |
||
| 419 | 12 | } |
|
| 420 | |||
| 421 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 422 | { |
||
| 423 | 8 | if ($value <= $limit) { |
|
| 424 | 4 | static::reportInvalidArgument(sprintf( |
|
| 425 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 426 | 4 | static::valueToString($value), |
|
| 427 | 4 | static::valueToString($limit) |
|
| 428 | )); |
||
| 429 | } |
||
| 430 | 4 | } |
|
| 431 | |||
| 432 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 433 | { |
||
| 434 | 12 | if ($value < $limit) { |
|
| 435 | 4 | static::reportInvalidArgument(sprintf( |
|
| 436 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
| 437 | 4 | static::valueToString($value), |
|
| 438 | 4 | static::valueToString($limit) |
|
| 439 | )); |
||
| 440 | } |
||
| 441 | 8 | } |
|
| 442 | |||
| 443 | 8 | public static function lessThan($value, $limit, $message = '') |
|
| 444 | { |
||
| 445 | 8 | if ($value >= $limit) { |
|
| 446 | 4 | static::reportInvalidArgument(sprintf( |
|
| 447 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
| 448 | 4 | static::valueToString($value), |
|
| 449 | 4 | static::valueToString($limit) |
|
| 450 | )); |
||
| 451 | } |
||
| 452 | 4 | } |
|
| 453 | |||
| 454 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 455 | { |
||
| 456 | 12 | if ($value > $limit) { |
|
| 457 | 4 | static::reportInvalidArgument(sprintf( |
|
| 458 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
| 459 | 4 | static::valueToString($value), |
|
| 460 | 4 | static::valueToString($limit) |
|
| 461 | )); |
||
| 462 | } |
||
| 463 | 8 | } |
|
| 464 | |||
| 465 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 466 | { |
||
| 467 | 16 | if ($value < $min || $value > $max) { |
|
| 468 | 8 | static::reportInvalidArgument(sprintf( |
|
| 469 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 470 | 8 | static::valueToString($value), |
|
| 471 | 8 | static::valueToString($min), |
|
| 472 | 8 | static::valueToString($max) |
|
| 473 | )); |
||
| 474 | } |
||
| 475 | 8 | } |
|
| 476 | |||
| 477 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 478 | { |
||
| 479 | 8 | if (!in_array($value, $values, true)) { |
|
| 480 | 4 | static::reportInvalidArgument(sprintf( |
|
| 481 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
| 482 | 4 | static::valueToString($value), |
|
| 483 | 4 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
|
| 484 | )); |
||
| 485 | } |
||
| 486 | 4 | } |
|
| 487 | |||
| 488 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 489 | { |
||
| 490 | 20 | if (false === strpos($value, $subString)) { |
|
| 491 | 8 | static::reportInvalidArgument(sprintf( |
|
| 492 | 8 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
| 493 | 8 | static::valueToString($value), |
|
| 494 | 8 | static::valueToString($subString) |
|
| 495 | )); |
||
| 496 | } |
||
| 497 | 12 | } |
|
| 498 | |||
| 499 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 509 | |||
| 510 | 12 | public static function startsWithLetter($value, $message = '') |
|
| 511 | { |
||
| 512 | 12 | $valid = isset($value[0]); |
|
| 513 | |||
| 514 | 12 | if ($valid) { |
|
| 515 | 8 | $locale = setlocale(LC_CTYPE, 0); |
|
| 516 | 8 | setlocale(LC_CTYPE, 'C'); |
|
| 517 | 8 | $valid = ctype_alpha($value[0]); |
|
| 518 | 8 | setlocale(LC_CTYPE, $locale); |
|
| 519 | } |
||
| 520 | |||
| 521 | 12 | if (!$valid) { |
|
| 522 | 8 | static::reportInvalidArgument(sprintf( |
|
| 523 | 8 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
| 524 | 8 | static::valueToString($value) |
|
| 525 | )); |
||
| 526 | } |
||
| 527 | 4 | } |
|
| 528 | |||
| 529 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 530 | { |
||
| 531 | 12 | if ($suffix !== substr($value, -static::strlen($suffix))) { |
|
| 532 | 8 | static::reportInvalidArgument(sprintf( |
|
| 533 | 8 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
| 534 | 8 | static::valueToString($value), |
|
| 535 | 8 | static::valueToString($suffix) |
|
| 536 | )); |
||
| 537 | } |
||
| 538 | 4 | } |
|
| 539 | |||
| 540 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 541 | { |
||
| 542 | 12 | if (!preg_match($pattern, $value)) { |
|
| 543 | 8 | static::reportInvalidArgument(sprintf( |
|
| 544 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
| 545 | 8 | static::valueToString($value) |
|
| 546 | )); |
||
| 547 | } |
||
| 548 | 4 | } |
|
| 549 | |||
| 550 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
| 564 | |||
| 565 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 579 | |||
| 580 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 594 | |||
| 595 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 609 | |||
| 610 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 611 | { |
||
| 612 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
| 613 | 16 | setlocale(LC_CTYPE, 'C'); |
|
| 614 | 16 | $valid = !ctype_upper($value); |
|
| 615 | 16 | setlocale(LC_CTYPE, $locale); |
|
| 616 | |||
| 617 | 16 | if ($valid) { |
|
| 618 | 12 | static::reportInvalidArgument(sprintf( |
|
| 619 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
| 620 | 12 | static::valueToString($value) |
|
| 621 | )); |
||
| 622 | } |
||
| 623 | 4 | } |
|
| 624 | |||
| 625 | 24 | public static function length($value, $length, $message = '') |
|
| 626 | { |
||
| 627 | 24 | if ($length !== static::strlen($value)) { |
|
| 628 | 16 | static::reportInvalidArgument(sprintf( |
|
| 629 | 16 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
| 630 | 16 | static::valueToString($value), |
|
| 631 | 16 | $length |
|
| 632 | )); |
||
| 633 | } |
||
| 634 | 8 | } |
|
| 635 | |||
| 636 | 24 | public static function minLength($value, $min, $message = '') |
|
| 637 | { |
||
| 638 | 24 | if (static::strlen($value) < $min) { |
|
| 639 | 8 | static::reportInvalidArgument(sprintf( |
|
| 640 | 8 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
| 641 | 8 | static::valueToString($value), |
|
| 642 | 8 | $min |
|
| 643 | )); |
||
| 644 | } |
||
| 645 | 16 | } |
|
| 646 | |||
| 647 | 24 | public static function maxLength($value, $max, $message = '') |
|
| 648 | { |
||
| 649 | 24 | if (static::strlen($value) > $max) { |
|
| 650 | 8 | static::reportInvalidArgument(sprintf( |
|
| 651 | 8 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
| 652 | 8 | static::valueToString($value), |
|
| 653 | 8 | $max |
|
| 654 | )); |
||
| 655 | } |
||
| 656 | 16 | } |
|
| 657 | |||
| 658 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 659 | { |
||
| 660 | 40 | $length = static::strlen($value); |
|
| 661 | |||
| 662 | 40 | if ($length < $min || $length > $max) { |
|
| 663 | 16 | static::reportInvalidArgument(sprintf( |
|
| 664 | 16 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
| 665 | 16 | static::valueToString($value), |
|
| 666 | $min, |
||
| 667 | 16 | $max |
|
| 668 | )); |
||
| 669 | } |
||
| 670 | 24 | } |
|
| 671 | |||
| 672 | 36 | public static function fileExists($value, $message = '') |
|
| 673 | { |
||
| 674 | 36 | static::string($value); |
|
| 675 | |||
| 676 | 36 | if (!file_exists($value)) { |
|
| 677 | 12 | static::reportInvalidArgument(sprintf( |
|
| 678 | 12 | $message ?: 'The file %s does not exist.', |
|
| 679 | 12 | static::valueToString($value) |
|
| 680 | )); |
||
| 681 | } |
||
| 682 | 24 | } |
|
| 683 | |||
| 684 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 685 | { |
||
| 686 | 12 | static::fileExists($value, $message); |
|
| 687 | |||
| 688 | 8 | if (!is_file($value)) { |
|
| 689 | 4 | static::reportInvalidArgument(sprintf( |
|
| 690 | 4 | $message ?: 'The path %s is not a file.', |
|
| 691 | 4 | static::valueToString($value) |
|
| 692 | )); |
||
| 693 | } |
||
| 694 | 4 | } |
|
| 695 | |||
| 696 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 697 | { |
||
| 698 | 12 | static::fileExists($value, $message); |
|
| 699 | |||
| 700 | 8 | if (!is_dir($value)) { |
|
| 701 | 4 | static::reportInvalidArgument(sprintf( |
|
| 702 | 4 | $message ?: 'The path %s is no directory.', |
|
| 703 | 4 | static::valueToString($value) |
|
| 704 | )); |
||
| 705 | } |
||
| 706 | 4 | } |
|
| 707 | |||
| 708 | public static function readable($value, $message = '') |
||
| 709 | { |
||
| 710 | if (!is_readable($value)) { |
||
| 711 | static::reportInvalidArgument(sprintf( |
||
| 712 | $message ?: 'The path %s is not readable.', |
||
| 713 | static::valueToString($value) |
||
| 714 | )); |
||
| 715 | } |
||
| 716 | } |
||
| 717 | |||
| 718 | public static function writable($value, $message = '') |
||
| 719 | { |
||
| 720 | if (!is_writable($value)) { |
||
| 721 | static::reportInvalidArgument(sprintf( |
||
| 722 | $message ?: 'The path %s is not writable.', |
||
| 723 | static::valueToString($value) |
||
| 724 | )); |
||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | 8 | public static function classExists($value, $message = '') |
|
| 737 | |||
| 738 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 748 | |||
| 749 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 750 | { |
||
| 751 | 8 | if (!in_array($interface, class_implements($value))) { |
|
| 752 | 4 | static::reportInvalidArgument(sprintf( |
|
| 753 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
| 754 | 4 | static::valueToString($value), |
|
| 755 | 4 | static::valueToString($interface) |
|
| 756 | )); |
||
| 757 | } |
||
| 759 | |||
| 760 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 769 | |||
| 770 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 779 | |||
| 780 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 789 | |||
| 790 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 799 | |||
| 800 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 809 | |||
| 810 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 819 | |||
| 820 | 8 | public static function count($array, $number, $message = '') |
|
| 821 | { |
||
| 822 | 8 | static::eq( |
|
| 823 | 8 | count($array), |
|
| 824 | $number, |
||
| 825 | 8 | $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
|
| 826 | ); |
||
| 827 | 4 | } |
|
| 828 | |||
| 829 | 48 | public static function uuid($value, $message = '') |
|
| 846 | |||
| 847 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 848 | { |
||
| 849 | 24 | static::string($class); |
|
| 850 | |||
| 851 | 24 | $actual = 'none'; |
|
| 852 | try { |
||
| 853 | 24 | $expression(); |
|
| 854 | 24 | } catch (Exception $e) { |
|
| 855 | 20 | $actual = get_class($e); |
|
| 856 | 20 | if ($e instanceof $class) { |
|
| 857 | 20 | return; |
|
| 858 | } |
||
| 859 | 4 | } catch (Throwable $e) { |
|
| 860 | 4 | $actual = get_class($e); |
|
| 861 | 4 | if ($e instanceof $class) { |
|
| 862 | 4 | return; |
|
| 863 | } |
||
| 864 | } |
||
| 865 | |||
| 866 | 8 | static::reportInvalidArgument($message ?: sprintf( |
|
| 867 | 8 | 'Expected to throw "%s", got "%s"', |
|
| 868 | $class, |
||
| 869 | 8 | $actual |
|
| 870 | )); |
||
| 871 | } |
||
| 872 | |||
| 873 | 820 | public static function __callStatic($name, $arguments) |
|
| 901 | |||
| 902 | 372 | protected static function valueToString($value) |
|
| 934 | |||
| 935 | 137 | protected static function typeToString($value) |
|
| 939 | |||
| 940 | 124 | protected static function strlen($value) |
|
| 952 | |||
| 953 | 520 | protected static function reportInvalidArgument($message) |
|
| 957 | |||
| 958 | private function __construct() |
||
| 961 | } |
||
| 962 |