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 |
||
| 209 | class Assert |
||
|
|
|||
| 210 | { |
||
| 211 | /** |
||
| 212 | * @psalm-assert string $value |
||
| 213 | * |
||
| 214 | * @param mixed $value |
||
| 215 | * @param string $message |
||
| 216 | * |
||
| 217 | * @throws InvalidArgumentException |
||
| 218 | */ |
||
| 219 | 241 | public static function string($value, $message = '') |
|
| 220 | { |
||
| 221 | 241 | if (!\is_string($value)) { |
|
| 222 | 45 | static::reportInvalidArgument(\sprintf( |
|
| 223 | 45 | $message ?: 'Expected a string. Got: %s', |
|
| 224 | 45 | static::typeToString($value) |
|
| 225 | )); |
||
| 226 | } |
||
| 227 | 196 | } |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param mixed $value |
||
| 231 | * @param string $message |
||
| 232 | * |
||
| 233 | * @throws InvalidArgumentException |
||
| 234 | */ |
||
| 235 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 236 | { |
||
| 237 | 16 | static::string($value, $message); |
|
| 238 | 12 | static::notEq($value, '', $message); |
|
| 239 | 8 | } |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @psalm-assert int $value |
||
| 243 | * |
||
| 244 | * @param mixed $value |
||
| 245 | * @param string $message |
||
| 246 | * |
||
| 247 | * @throws InvalidArgumentException |
||
| 248 | */ |
||
| 249 | 17 | public static function integer($value, $message = '') |
|
| 250 | { |
||
| 251 | 17 | if (!\is_int($value)) { |
|
| 252 | 13 | static::reportInvalidArgument(\sprintf( |
|
| 253 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
| 254 | 13 | static::typeToString($value) |
|
| 255 | )); |
||
| 256 | } |
||
| 257 | 4 | } |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @psalm-assert numeric $value |
||
| 261 | * |
||
| 262 | * @param mixed $value |
||
| 263 | * @param string $message |
||
| 264 | * |
||
| 265 | * @throws InvalidArgumentException |
||
| 266 | */ |
||
| 267 | 16 | public static function integerish($value, $message = '') |
|
| 268 | { |
||
| 269 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
| 270 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 271 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
| 272 | 4 | static::typeToString($value) |
|
| 273 | )); |
||
| 274 | } |
||
| 275 | 12 | } |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @psalm-assert float $value |
||
| 279 | * |
||
| 280 | * @param mixed $value |
||
| 281 | * @param string $message |
||
| 282 | * |
||
| 283 | * @throws InvalidArgumentException |
||
| 284 | */ |
||
| 285 | 16 | public static function float($value, $message = '') |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @psalm-assert numeric $value |
||
| 297 | * |
||
| 298 | * @param mixed $value |
||
| 299 | * @param string $message |
||
| 300 | * |
||
| 301 | * @throws InvalidArgumentException |
||
| 302 | */ |
||
| 303 | 20 | public static function numeric($value, $message = '') |
|
| 304 | { |
||
| 305 | 20 | if (!\is_numeric($value)) { |
|
| 306 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 307 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
| 308 | 4 | static::typeToString($value) |
|
| 309 | )); |
||
| 310 | } |
||
| 311 | 16 | } |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @psalm-assert int $value |
||
| 315 | * |
||
| 316 | * @param mixed $value |
||
| 317 | * @param string $message |
||
| 318 | * |
||
| 319 | * @throws InvalidArgumentException |
||
| 320 | */ |
||
| 321 | 24 | public static function natural($value, $message = '') |
|
| 322 | { |
||
| 323 | 24 | if (!\is_int($value) || $value < 0) { |
|
| 324 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 325 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
| 326 | 16 | static::valueToString($value) |
|
| 327 | )); |
||
| 328 | } |
||
| 329 | 8 | } |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @psalm-assert bool $value |
||
| 333 | * |
||
| 334 | * @param mixed $value |
||
| 335 | * @param string $message |
||
| 336 | * |
||
| 337 | * @throws InvalidArgumentException |
||
| 338 | */ |
||
| 339 | 16 | public static function boolean($value, $message = '') |
|
| 340 | { |
||
| 341 | 16 | if (!\is_bool($value)) { |
|
| 342 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 343 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
| 344 | 8 | static::typeToString($value) |
|
| 345 | )); |
||
| 346 | } |
||
| 347 | 8 | } |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @psalm-assert scalar $value |
||
| 351 | * |
||
| 352 | * @param mixed $value |
||
| 353 | * @param string $message |
||
| 354 | * |
||
| 355 | * @throws InvalidArgumentException |
||
| 356 | */ |
||
| 357 | 23 | public static function scalar($value, $message = '') |
|
| 358 | { |
||
| 359 | 23 | if (!\is_scalar($value)) { |
|
| 360 | 11 | static::reportInvalidArgument(\sprintf( |
|
| 361 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
| 362 | 11 | static::typeToString($value) |
|
| 363 | )); |
||
| 364 | } |
||
| 365 | 12 | } |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @psalm-assert object $value |
||
| 369 | * |
||
| 370 | * @param mixed $value |
||
| 371 | * @param string $message |
||
| 372 | * |
||
| 373 | * @throws InvalidArgumentException |
||
| 374 | */ |
||
| 375 | 23 | public static function object($value, $message = '') |
|
| 376 | { |
||
| 377 | 23 | if (!\is_object($value)) { |
|
| 378 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 379 | 15 | $message ?: 'Expected an object. Got: %s', |
|
| 380 | 15 | static::typeToString($value) |
|
| 381 | )); |
||
| 382 | } |
||
| 383 | 8 | } |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @psalm-assert resource $value |
||
| 387 | * |
||
| 388 | * @param mixed $value |
||
| 389 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
| 390 | * @param string $message |
||
| 391 | * |
||
| 392 | * @throws InvalidArgumentException |
||
| 393 | */ |
||
| 394 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 395 | { |
||
| 396 | 16 | if (!\is_resource($value)) { |
|
| 397 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 398 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
| 399 | 4 | static::typeToString($value) |
|
| 400 | )); |
||
| 401 | } |
||
| 402 | |||
| 403 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
| 404 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 405 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
| 406 | 4 | static::typeToString($value), |
|
| 407 | 4 | $type |
|
| 408 | )); |
||
| 409 | } |
||
| 410 | 8 | } |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @psalm-assert callable $value |
||
| 414 | * |
||
| 415 | * @param mixed $value |
||
| 416 | * @param string $message |
||
| 417 | * |
||
| 418 | * @throws InvalidArgumentException |
||
| 419 | */ |
||
| 420 | 20 | public static function isCallable($value, $message = '') |
|
| 421 | { |
||
| 422 | 20 | if (!\is_callable($value)) { |
|
| 423 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 424 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
| 425 | 8 | static::typeToString($value) |
|
| 426 | )); |
||
| 427 | } |
||
| 428 | 12 | } |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @psalm-assert array $value |
||
| 432 | * |
||
| 433 | * @param mixed $value |
||
| 434 | * @param string $message |
||
| 435 | * |
||
| 436 | * @throws InvalidArgumentException |
||
| 437 | */ |
||
| 438 | 20 | public static function isArray($value, $message = '') |
|
| 439 | { |
||
| 440 | 20 | if (!\is_array($value)) { |
|
| 441 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 442 | 12 | $message ?: 'Expected an array. Got: %s', |
|
| 443 | 12 | static::typeToString($value) |
|
| 444 | )); |
||
| 445 | } |
||
| 446 | 8 | } |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @psalm-assert iterable $value |
||
| 450 | * |
||
| 451 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
| 452 | * |
||
| 453 | * @param mixed $value |
||
| 454 | * @param string $message |
||
| 455 | * |
||
| 456 | * @throws InvalidArgumentException |
||
| 457 | */ |
||
| 458 | 20 | public static function isTraversable($value, $message = '') |
|
| 459 | { |
||
| 460 | 20 | @\trigger_error( |
|
| 461 | 20 | \sprintf( |
|
| 462 | 20 | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', |
|
| 463 | 20 | __METHOD__ |
|
| 464 | ), |
||
| 465 | 20 | \E_USER_DEPRECATED |
|
| 466 | ); |
||
| 467 | |||
| 468 | 20 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
| 469 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 470 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
| 471 | 8 | static::typeToString($value) |
|
| 472 | )); |
||
| 473 | } |
||
| 474 | 12 | } |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param mixed $value |
||
| 478 | * @param string $message |
||
| 479 | * |
||
| 480 | * @throws InvalidArgumentException |
||
| 481 | */ |
||
| 482 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 483 | { |
||
| 484 | 20 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
|
| 485 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 486 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
| 487 | 8 | static::typeToString($value) |
|
| 488 | )); |
||
| 489 | } |
||
| 490 | 12 | } |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @psalm-assert countable $value |
||
| 494 | * |
||
| 495 | * @param mixed $value |
||
| 496 | * @param string $message |
||
| 497 | * |
||
| 498 | * @throws InvalidArgumentException |
||
| 499 | */ |
||
| 500 | 28 | public static function isCountable($value, $message = '') |
|
| 501 | { |
||
| 502 | if ( |
||
| 503 | 28 | !\is_array($value) |
|
| 504 | 28 | && !($value instanceof Countable) |
|
| 505 | 28 | && !($value instanceof ResourceBundle) |
|
| 506 | 28 | && !($value instanceof SimpleXMLElement) |
|
| 507 | ) { |
||
| 508 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 509 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
| 510 | 12 | static::typeToString($value) |
|
| 511 | )); |
||
| 512 | } |
||
| 513 | 16 | } |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @psalm-assert iterable $value |
||
| 517 | * |
||
| 518 | * @param mixed $value |
||
| 519 | * @param string $message |
||
| 520 | * |
||
| 521 | * @throws InvalidArgumentException |
||
| 522 | */ |
||
| 523 | 992 | View Code Duplication | public static function isIterable($value, $message = '') |
| 524 | { |
||
| 525 | 992 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
| 526 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 527 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
| 528 | 8 | static::typeToString($value) |
|
| 529 | )); |
||
| 530 | } |
||
| 531 | 988 | } |
|
| 532 | |||
| 533 | /** |
||
| 534 | * @psalm-template ExpectedType of object |
||
| 535 | * @psalm-param class-string<ExpectedType> $class |
||
| 536 | * @psalm-assert ExpectedType $value |
||
| 537 | * |
||
| 538 | * @param mixed $value |
||
| 539 | * @param string|object $class |
||
| 540 | * @param string $message |
||
| 541 | * |
||
| 542 | * @throws InvalidArgumentException |
||
| 543 | */ |
||
| 544 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
| 545 | { |
||
| 546 | 19 | if (!($value instanceof $class)) { |
|
| 547 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 548 | 15 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
| 549 | 15 | static::typeToString($value), |
|
| 550 | 15 | $class |
|
| 551 | )); |
||
| 552 | } |
||
| 553 | 4 | } |
|
| 554 | |||
| 555 | /** |
||
| 556 | * @psalm-template ExpectedType of object |
||
| 557 | * @psalm-param class-string<ExpectedType> $class |
||
| 558 | * @psalm-assert !ExpectedType $value |
||
| 559 | * |
||
| 560 | * @param mixed $value |
||
| 561 | * @param string|object $class |
||
| 562 | * @param string $message |
||
| 563 | * |
||
| 564 | * @throws InvalidArgumentException |
||
| 565 | */ |
||
| 566 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 567 | { |
||
| 568 | 16 | if ($value instanceof $class) { |
|
| 569 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 570 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
| 571 | 4 | static::typeToString($value), |
|
| 572 | 4 | $class |
|
| 573 | )); |
||
| 574 | } |
||
| 575 | 12 | } |
|
| 576 | |||
| 577 | /** |
||
| 578 | * @param mixed $value |
||
| 579 | * @param array<object|string> $classes |
||
| 580 | * @param string $message |
||
| 581 | * |
||
| 582 | * @throws InvalidArgumentException |
||
| 583 | */ |
||
| 584 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 585 | { |
||
| 586 | 20 | foreach ($classes as $class) { |
|
| 587 | 20 | if ($value instanceof $class) { |
|
| 588 | 8 | return; |
|
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 593 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
| 594 | 12 | static::typeToString($value), |
|
| 595 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
| 596 | )); |
||
| 597 | } |
||
| 598 | |||
| 599 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
| 600 | { |
||
| 601 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 602 | |||
| 603 | 16 | if (!is_a($value, $class, is_string($value))) { |
|
| 604 | 12 | static::reportInvalidArgument(sprintf( |
|
| 605 | 12 | $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', |
|
| 606 | 12 | static::typeToString($value), |
|
| 607 | $class |
||
| 608 | )); |
||
| 609 | } |
||
| 610 | 4 | } |
|
| 611 | |||
| 612 | 20 | View Code Duplication | public static function notAOf($value, $class, $message = '') |
| 613 | { |
||
| 614 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 615 | |||
| 616 | 16 | if (is_a($value, $class, is_string($value))) { |
|
| 617 | 4 | static::reportInvalidArgument(sprintf( |
|
| 618 | 4 | $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
| 619 | 4 | static::typeToString($value), |
|
| 620 | $class |
||
| 621 | )); |
||
| 622 | } |
||
| 623 | 12 | } |
|
| 624 | |||
| 625 | 24 | public static function isAOfAny($value, array $classes, $message = '') |
|
| 626 | { |
||
| 627 | 24 | foreach ($classes as $class) { |
|
| 628 | 24 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 629 | |||
| 630 | 20 | if (is_a($value, $class, is_string($value))) { |
|
| 631 | 8 | return; |
|
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | 12 | static::reportInvalidArgument(sprintf( |
|
| 636 | 12 | $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
| 637 | 12 | static::typeToString($value), |
|
| 638 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
| 639 | )); |
||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @psalm-assert empty $value |
||
| 644 | * |
||
| 645 | * @param mixed $value |
||
| 646 | * @param string $message |
||
| 647 | * |
||
| 648 | * @throws InvalidArgumentException |
||
| 649 | */ |
||
| 650 | 23 | public static function isEmpty($value, $message = '') |
|
| 651 | { |
||
| 652 | 23 | if (!empty($value)) { |
|
| 653 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 654 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
| 655 | 8 | static::valueToString($value) |
|
| 656 | )); |
||
| 657 | } |
||
| 658 | 15 | } |
|
| 659 | |||
| 660 | /** |
||
| 661 | * @psalm-assert !empty $value |
||
| 662 | * |
||
| 663 | * @param mixed $value |
||
| 664 | * @param string $message |
||
| 665 | * |
||
| 666 | * @throws InvalidArgumentException |
||
| 667 | */ |
||
| 668 | 55 | public static function notEmpty($value, $message = '') |
|
| 669 | { |
||
| 670 | 55 | if (empty($value)) { |
|
| 671 | 23 | static::reportInvalidArgument(\sprintf( |
|
| 672 | 23 | $message ?: 'Expected a non-empty value. Got: %s', |
|
| 673 | 23 | static::valueToString($value) |
|
| 674 | )); |
||
| 675 | } |
||
| 676 | 32 | } |
|
| 677 | |||
| 678 | /** |
||
| 679 | * @psalm-assert null $value |
||
| 680 | * |
||
| 681 | * @param mixed $value |
||
| 682 | * @param string $message |
||
| 683 | * |
||
| 684 | * @throws InvalidArgumentException |
||
| 685 | */ |
||
| 686 | 11 | public static function null($value, $message = '') |
|
| 687 | { |
||
| 688 | 11 | if (null !== $value) { |
|
| 689 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 690 | 8 | $message ?: 'Expected null. Got: %s', |
|
| 691 | 8 | static::valueToString($value) |
|
| 692 | )); |
||
| 693 | } |
||
| 694 | 3 | } |
|
| 695 | |||
| 696 | /** |
||
| 697 | * @psalm-assert !null $value |
||
| 698 | * |
||
| 699 | * @param mixed $value |
||
| 700 | * @param string $message |
||
| 701 | * |
||
| 702 | * @throws InvalidArgumentException |
||
| 703 | */ |
||
| 704 | 11 | public static function notNull($value, $message = '') |
|
| 705 | { |
||
| 706 | 11 | if (null === $value) { |
|
| 707 | 3 | static::reportInvalidArgument( |
|
| 708 | 3 | $message ?: 'Expected a value other than null.' |
|
| 709 | ); |
||
| 710 | } |
||
| 711 | 8 | } |
|
| 712 | |||
| 713 | /** |
||
| 714 | * @psalm-assert true $value |
||
| 715 | * |
||
| 716 | * @param mixed $value |
||
| 717 | * @param string $message |
||
| 718 | * |
||
| 719 | * @throws InvalidArgumentException |
||
| 720 | */ |
||
| 721 | 15 | public static function true($value, $message = '') |
|
| 722 | { |
||
| 723 | 15 | if (true !== $value) { |
|
| 724 | 11 | static::reportInvalidArgument(\sprintf( |
|
| 725 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
| 726 | 11 | static::valueToString($value) |
|
| 727 | )); |
||
| 728 | } |
||
| 729 | 4 | } |
|
| 730 | |||
| 731 | /** |
||
| 732 | * @psalm-assert false $value |
||
| 733 | * |
||
| 734 | * @param mixed $value |
||
| 735 | * @param string $message |
||
| 736 | * |
||
| 737 | * @throws InvalidArgumentException |
||
| 738 | */ |
||
| 739 | 19 | public static function false($value, $message = '') |
|
| 740 | { |
||
| 741 | 19 | if (false !== $value) { |
|
| 742 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 743 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
| 744 | 15 | static::valueToString($value) |
|
| 745 | )); |
||
| 746 | } |
||
| 747 | 4 | } |
|
| 748 | |||
| 749 | /** |
||
| 750 | * @psalm-assert !false $value |
||
| 751 | * |
||
| 752 | * @param mixed $value |
||
| 753 | * @param string $message |
||
| 754 | * |
||
| 755 | * @throws InvalidArgumentException |
||
| 756 | */ |
||
| 757 | 19 | public static function notFalse($value, $message = '') |
|
| 758 | { |
||
| 759 | 19 | if (false === $value) { |
|
| 760 | 4 | static::reportInvalidArgument( |
|
| 761 | 4 | $message ?: 'Expected a value other than false.' |
|
| 762 | ); |
||
| 763 | } |
||
| 764 | 15 | } |
|
| 765 | |||
| 766 | /** |
||
| 767 | * @param mixed $value |
||
| 768 | * @param string $message |
||
| 769 | * |
||
| 770 | * @throws InvalidArgumentException |
||
| 771 | */ |
||
| 772 | 51 | View Code Duplication | public static function ip($value, $message = '') |
| 773 | { |
||
| 774 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
|
| 775 | 19 | static::reportInvalidArgument(\sprintf( |
|
| 776 | 19 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
| 777 | 19 | static::valueToString($value) |
|
| 778 | )); |
||
| 779 | } |
||
| 780 | 32 | } |
|
| 781 | |||
| 782 | /** |
||
| 783 | * @param mixed $value |
||
| 784 | * @param string $message |
||
| 785 | * |
||
| 786 | * @throws InvalidArgumentException |
||
| 787 | */ |
||
| 788 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
| 789 | { |
||
| 790 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
| 791 | 35 | static::reportInvalidArgument(\sprintf( |
|
| 792 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
| 793 | 35 | static::valueToString($value) |
|
| 794 | )); |
||
| 795 | } |
||
| 796 | 16 | } |
|
| 797 | |||
| 798 | /** |
||
| 799 | * @param mixed $value |
||
| 800 | * @param string $message |
||
| 801 | * |
||
| 802 | * @throws InvalidArgumentException |
||
| 803 | */ |
||
| 804 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
| 805 | { |
||
| 806 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
| 807 | 31 | static::reportInvalidArgument(\sprintf( |
|
| 808 | 31 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
| 809 | 31 | static::valueToString($value) |
|
| 810 | )); |
||
| 811 | } |
||
| 812 | 20 | } |
|
| 813 | |||
| 814 | /** |
||
| 815 | * @param mixed $value |
||
| 816 | * @param string $message |
||
| 817 | * |
||
| 818 | * @throws InvalidArgumentException |
||
| 819 | */ |
||
| 820 | 20 | View Code Duplication | public static function email($value, $message = '') |
| 821 | { |
||
| 822 | 20 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 823 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 824 | 12 | $message ?: 'Expected a value to be a valid e-mail address. Got %s', |
|
| 825 | 12 | static::valueToString($value) |
|
| 826 | )); |
||
| 827 | } |
||
| 828 | 8 | } |
|
| 829 | |||
| 830 | /** |
||
| 831 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
| 832 | * |
||
| 833 | * @param array $values |
||
| 834 | * @param string $message |
||
| 835 | * |
||
| 836 | * @throws InvalidArgumentException |
||
| 837 | */ |
||
| 838 | 12 | public static function uniqueValues(array $values, $message = '') |
|
| 839 | { |
||
| 840 | 12 | $allValues = \count($values); |
|
| 841 | 12 | $uniqueValues = \count(\array_unique($values)); |
|
| 842 | |||
| 843 | 12 | if ($allValues !== $uniqueValues) { |
|
| 844 | 8 | $difference = $allValues - $uniqueValues; |
|
| 845 | |||
| 846 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 847 | 8 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
|
| 848 | 8 | $difference, |
|
| 849 | 8 | (1 === $difference ? 'is' : 'are') |
|
| 850 | )); |
||
| 851 | } |
||
| 852 | 4 | } |
|
| 853 | |||
| 854 | /** |
||
| 855 | * @param mixed $value |
||
| 856 | * @param mixed $expect |
||
| 857 | * @param string $message |
||
| 858 | * |
||
| 859 | * @throws InvalidArgumentException |
||
| 860 | */ |
||
| 861 | 33 | public static function eq($value, $expect, $message = '') |
|
| 862 | { |
||
| 863 | 33 | if ($expect != $value) { |
|
| 864 | 17 | static::reportInvalidArgument(\sprintf( |
|
| 865 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 866 | 17 | static::valueToString($value), |
|
| 867 | 17 | static::valueToString($expect) |
|
| 868 | )); |
||
| 869 | } |
||
| 870 | 16 | } |
|
| 871 | |||
| 872 | /** |
||
| 873 | * @param mixed $value |
||
| 874 | * @param mixed $expect |
||
| 875 | * @param string $message |
||
| 876 | * |
||
| 877 | * @throws InvalidArgumentException |
||
| 878 | */ |
||
| 879 | 28 | public static function notEq($value, $expect, $message = '') |
|
| 880 | { |
||
| 881 | 28 | if ($expect == $value) { |
|
| 882 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 883 | 16 | $message ?: 'Expected a different value than %s.', |
|
| 884 | 16 | static::valueToString($expect) |
|
| 885 | )); |
||
| 886 | } |
||
| 887 | 12 | } |
|
| 888 | |||
| 889 | /** |
||
| 890 | * @param mixed $value |
||
| 891 | * @param mixed $expect |
||
| 892 | * @param string $message |
||
| 893 | * |
||
| 894 | * @throws InvalidArgumentException |
||
| 895 | */ |
||
| 896 | 16 | public static function same($value, $expect, $message = '') |
|
| 897 | { |
||
| 898 | 16 | if ($expect !== $value) { |
|
| 899 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 900 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 901 | 12 | static::valueToString($value), |
|
| 902 | 12 | static::valueToString($expect) |
|
| 903 | )); |
||
| 904 | } |
||
| 905 | 4 | } |
|
| 906 | |||
| 907 | /** |
||
| 908 | * @param mixed $value |
||
| 909 | * @param mixed $expect |
||
| 910 | * @param string $message |
||
| 911 | * |
||
| 912 | * @throws InvalidArgumentException |
||
| 913 | */ |
||
| 914 | 16 | public static function notSame($value, $expect, $message = '') |
|
| 915 | { |
||
| 916 | 16 | if ($expect === $value) { |
|
| 917 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 918 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
| 919 | 4 | static::valueToString($expect) |
|
| 920 | )); |
||
| 921 | } |
||
| 922 | 12 | } |
|
| 923 | |||
| 924 | /** |
||
| 925 | * @param mixed $value |
||
| 926 | * @param mixed $limit |
||
| 927 | * @param string $message |
||
| 928 | * |
||
| 929 | * @throws InvalidArgumentException |
||
| 930 | */ |
||
| 931 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 932 | { |
||
| 933 | 8 | if ($value <= $limit) { |
|
| 934 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 935 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 936 | 4 | static::valueToString($value), |
|
| 937 | 4 | static::valueToString($limit) |
|
| 938 | )); |
||
| 939 | } |
||
| 940 | 4 | } |
|
| 941 | |||
| 942 | /** |
||
| 943 | * @param mixed $value |
||
| 944 | * @param mixed $limit |
||
| 945 | * @param string $message |
||
| 946 | * |
||
| 947 | * @throws InvalidArgumentException |
||
| 948 | */ |
||
| 949 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 950 | { |
||
| 951 | 12 | if ($value < $limit) { |
|
| 952 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 953 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
| 954 | 4 | static::valueToString($value), |
|
| 955 | 4 | static::valueToString($limit) |
|
| 956 | )); |
||
| 957 | } |
||
| 958 | 8 | } |
|
| 959 | |||
| 960 | /** |
||
| 961 | * @param mixed $value |
||
| 962 | * @param mixed $limit |
||
| 963 | * @param string $message |
||
| 964 | * |
||
| 965 | * @throws InvalidArgumentException |
||
| 966 | */ |
||
| 967 | 8 | public static function lessThan($value, $limit, $message = '') |
|
| 968 | { |
||
| 969 | 8 | if ($value >= $limit) { |
|
| 970 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 971 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
| 972 | 4 | static::valueToString($value), |
|
| 973 | 4 | static::valueToString($limit) |
|
| 974 | )); |
||
| 975 | } |
||
| 976 | 4 | } |
|
| 977 | |||
| 978 | /** |
||
| 979 | * @param mixed $value |
||
| 980 | * @param mixed $limit |
||
| 981 | * @param string $message |
||
| 982 | * |
||
| 983 | * @throws InvalidArgumentException |
||
| 984 | */ |
||
| 985 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 986 | { |
||
| 987 | 12 | if ($value > $limit) { |
|
| 988 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 989 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
| 990 | 4 | static::valueToString($value), |
|
| 991 | 4 | static::valueToString($limit) |
|
| 992 | )); |
||
| 993 | } |
||
| 994 | 8 | } |
|
| 995 | |||
| 996 | /** |
||
| 997 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
| 998 | * |
||
| 999 | * @param mixed $value |
||
| 1000 | * @param mixed $min |
||
| 1001 | * @param mixed $max |
||
| 1002 | * @param string $message |
||
| 1003 | * |
||
| 1004 | * @throws InvalidArgumentException |
||
| 1005 | */ |
||
| 1006 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 1007 | { |
||
| 1008 | 16 | if ($value < $min || $value > $max) { |
|
| 1009 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1010 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 1011 | 8 | static::valueToString($value), |
|
| 1012 | 8 | static::valueToString($min), |
|
| 1013 | 8 | static::valueToString($max) |
|
| 1014 | )); |
||
| 1015 | } |
||
| 1016 | 8 | } |
|
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
| 1020 | * |
||
| 1021 | * @param mixed $value |
||
| 1022 | * @param array $values |
||
| 1023 | * @param string $message |
||
| 1024 | * |
||
| 1025 | * @throws InvalidArgumentException |
||
| 1026 | */ |
||
| 1027 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 1028 | { |
||
| 1029 | 8 | if (!\in_array($value, $values, true)) { |
|
| 1030 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1031 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
| 1032 | 4 | static::valueToString($value), |
|
| 1033 | 4 | \implode(', ', \array_map(array('static', 'valueToString'), $values)) |
|
| 1034 | )); |
||
| 1035 | } |
||
| 1036 | 4 | } |
|
| 1037 | |||
| 1038 | /** |
||
| 1039 | * @param mixed $value |
||
| 1040 | * @param string $subString |
||
| 1041 | * @param string $message |
||
| 1042 | * |
||
| 1043 | * @throws InvalidArgumentException |
||
| 1044 | */ |
||
| 1045 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 1046 | { |
||
| 1047 | 80 | if (false === \strpos($value, $subString)) { |
|
| 1048 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 1049 | 32 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
| 1050 | 32 | static::valueToString($value), |
|
| 1051 | 32 | static::valueToString($subString) |
|
| 1052 | )); |
||
| 1053 | } |
||
| 1054 | 48 | } |
|
| 1055 | |||
| 1056 | /** |
||
| 1057 | * @param mixed $value |
||
| 1058 | * @param string $subString |
||
| 1059 | * @param string $message |
||
| 1060 | * |
||
| 1061 | * @throws InvalidArgumentException |
||
| 1062 | */ |
||
| 1063 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 1064 | { |
||
| 1065 | 80 | if (false !== \strpos($value, $subString)) { |
|
| 1066 | 48 | static::reportInvalidArgument(\sprintf( |
|
| 1067 | 48 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
|
| 1068 | 48 | static::valueToString($value), |
|
| 1069 | 48 | static::valueToString($subString) |
|
| 1070 | )); |
||
| 1071 | } |
||
| 1072 | 32 | } |
|
| 1073 | |||
| 1074 | /** |
||
| 1075 | * @param mixed $value |
||
| 1076 | * @param string $message |
||
| 1077 | * |
||
| 1078 | * @throws InvalidArgumentException |
||
| 1079 | */ |
||
| 1080 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 1081 | { |
||
| 1082 | 40 | if (\preg_match('/^\s*$/', $value)) { |
|
| 1083 | 24 | static::reportInvalidArgument(\sprintf( |
|
| 1084 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
| 1085 | 24 | static::valueToString($value) |
|
| 1086 | )); |
||
| 1087 | } |
||
| 1088 | 16 | } |
|
| 1089 | |||
| 1090 | /** |
||
| 1091 | * @param mixed $value |
||
| 1092 | * @param string $prefix |
||
| 1093 | * @param string $message |
||
| 1094 | * |
||
| 1095 | * @throws InvalidArgumentException |
||
| 1096 | */ |
||
| 1097 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 1098 | { |
||
| 1099 | 48 | if (0 !== \strpos($value, $prefix)) { |
|
| 1100 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 1101 | 32 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
| 1102 | 32 | static::valueToString($value), |
|
| 1103 | 32 | static::valueToString($prefix) |
|
| 1104 | )); |
||
| 1105 | } |
||
| 1106 | 16 | } |
|
| 1107 | |||
| 1108 | /** |
||
| 1109 | * @param mixed $value |
||
| 1110 | * @param string $message |
||
| 1111 | * |
||
| 1112 | * @throws InvalidArgumentException |
||
| 1113 | */ |
||
| 1114 | 35 | public static function startsWithLetter($value, $message = '') |
|
| 1115 | { |
||
| 1116 | 35 | static::string($value); |
|
| 1117 | |||
| 1118 | 24 | $valid = isset($value[0]); |
|
| 1119 | |||
| 1120 | 24 | if ($valid) { |
|
| 1121 | 20 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1122 | 20 | \setlocale(LC_CTYPE, 'C'); |
|
| 1123 | 20 | $valid = \ctype_alpha($value[0]); |
|
| 1124 | 20 | \setlocale(LC_CTYPE, $locale); |
|
| 1125 | } |
||
| 1126 | |||
| 1127 | 24 | if (!$valid) { |
|
| 1128 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 1129 | 12 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
| 1130 | 12 | static::valueToString($value) |
|
| 1131 | )); |
||
| 1132 | } |
||
| 1133 | 12 | } |
|
| 1134 | |||
| 1135 | /** |
||
| 1136 | * @param mixed $value |
||
| 1137 | * @param string $suffix |
||
| 1138 | * @param string $message |
||
| 1139 | * |
||
| 1140 | * @throws InvalidArgumentException |
||
| 1141 | */ |
||
| 1142 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 1143 | { |
||
| 1144 | 48 | if ($suffix !== \substr($value, -\strlen($suffix))) { |
|
| 1145 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 1146 | 32 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
| 1147 | 32 | static::valueToString($value), |
|
| 1148 | 32 | static::valueToString($suffix) |
|
| 1149 | )); |
||
| 1150 | } |
||
| 1151 | 16 | } |
|
| 1152 | |||
| 1153 | /** |
||
| 1154 | * @param mixed $value |
||
| 1155 | * @param mixed $pattern |
||
| 1156 | * @param string $message |
||
| 1157 | * |
||
| 1158 | * @throws InvalidArgumentException |
||
| 1159 | */ |
||
| 1160 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 1161 | { |
||
| 1162 | 12 | if (!\preg_match($pattern, $value)) { |
|
| 1163 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1164 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
| 1165 | 8 | static::valueToString($value) |
|
| 1166 | )); |
||
| 1167 | } |
||
| 1168 | 4 | } |
|
| 1169 | |||
| 1170 | /** |
||
| 1171 | * @param mixed $value |
||
| 1172 | * @param mixed $pattern |
||
| 1173 | * @param string $message |
||
| 1174 | * |
||
| 1175 | * @throws InvalidArgumentException |
||
| 1176 | */ |
||
| 1177 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
| 1178 | { |
||
| 1179 | 12 | if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 1180 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1181 | 4 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
|
| 1182 | 4 | static::valueToString($value), |
|
| 1183 | 4 | static::valueToString($pattern), |
|
| 1184 | 4 | $matches[0][1] |
|
| 1185 | )); |
||
| 1186 | } |
||
| 1187 | 8 | } |
|
| 1188 | |||
| 1189 | /** |
||
| 1190 | * @param mixed $value |
||
| 1191 | * @param string $message |
||
| 1192 | * |
||
| 1193 | * @throws InvalidArgumentException |
||
| 1194 | */ |
||
| 1195 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
| 1206 | |||
| 1207 | /** |
||
| 1208 | * @param mixed $value |
||
| 1209 | * @param string $message |
||
| 1210 | * |
||
| 1211 | * @throws InvalidArgumentException |
||
| 1212 | */ |
||
| 1213 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
| 1214 | { |
||
| 1215 | 20 | static::string($value); |
|
| 1216 | |||
| 1217 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1218 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
| 1219 | 12 | $valid = !\ctype_alpha($value); |
|
| 1220 | 12 | \setlocale(LC_CTYPE, $locale); |
|
| 1221 | |||
| 1222 | 12 | if ($valid) { |
|
| 1223 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1224 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
| 1225 | 8 | static::valueToString($value) |
|
| 1226 | )); |
||
| 1227 | } |
||
| 1228 | 4 | } |
|
| 1229 | |||
| 1230 | /** |
||
| 1231 | * @param mixed $value |
||
| 1232 | * @param string $message |
||
| 1233 | * |
||
| 1234 | * @throws InvalidArgumentException |
||
| 1235 | */ |
||
| 1236 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 1237 | { |
||
| 1238 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1239 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
| 1240 | 12 | $valid = !\ctype_digit($value); |
|
| 1241 | 12 | \setlocale(LC_CTYPE, $locale); |
|
| 1242 | |||
| 1243 | 12 | if ($valid) { |
|
| 1244 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 1245 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
| 1246 | 8 | static::valueToString($value) |
|
| 1247 | )); |
||
| 1248 | } |
||
| 1249 | 4 | } |
|
| 1250 | |||
| 1251 | /** |
||
| 1252 | * @param mixed $value |
||
| 1253 | * @param string $message |
||
| 1254 | * |
||
| 1255 | * @throws InvalidArgumentException |
||
| 1256 | */ |
||
| 1257 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 1258 | { |
||
| 1259 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 1260 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
| 1271 | |||
| 1272 | /** |
||
| 1273 | * @param mixed $value |
||
| 1274 | * @param string $message |
||
| 1275 | * |
||
| 1276 | * @throws InvalidArgumentException |
||
| 1277 | */ |
||
| 1278 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 1292 | |||
| 1293 | /** |
||
| 1294 | * @param mixed $value |
||
| 1295 | * @param string $message |
||
| 1296 | * |
||
| 1297 | * @throws InvalidArgumentException |
||
| 1298 | */ |
||
| 1299 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 1313 | |||
| 1314 | /** |
||
| 1315 | * @param mixed $value |
||
| 1316 | * @param mixed $length |
||
| 1317 | * @param string $message |
||
| 1318 | * |
||
| 1319 | * @throws InvalidArgumentException |
||
| 1320 | */ |
||
| 1321 | 36 | public static function length($value, $length, $message = '') |
|
| 1331 | |||
| 1332 | /** |
||
| 1333 | * Inclusive min. |
||
| 1334 | * |
||
| 1335 | * @param mixed $value |
||
| 1336 | * @param mixed $min |
||
| 1337 | * @param string $message |
||
| 1338 | * |
||
| 1339 | * @throws InvalidArgumentException |
||
| 1340 | */ |
||
| 1341 | 36 | public static function minLength($value, $min, $message = '') |
|
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Inclusive max. |
||
| 1354 | * |
||
| 1355 | * @param mixed $value |
||
| 1356 | * @param mixed $max |
||
| 1357 | * @param string $message |
||
| 1358 | * |
||
| 1359 | * @throws InvalidArgumentException |
||
| 1360 | */ |
||
| 1361 | 36 | public static function maxLength($value, $max, $message = '') |
|
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
| 1374 | * |
||
| 1375 | * @param mixed $value |
||
| 1376 | * @param mixed $min |
||
| 1377 | * @param mixed $max |
||
| 1378 | * @param string $message |
||
| 1379 | * |
||
| 1380 | * @throws InvalidArgumentException |
||
| 1381 | */ |
||
| 1382 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
| 1398 | * |
||
| 1399 | * @param mixed $value |
||
| 1400 | * @param string $message |
||
| 1401 | * |
||
| 1402 | * @throws InvalidArgumentException |
||
| 1403 | */ |
||
| 1404 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
| 1415 | |||
| 1416 | /** |
||
| 1417 | * @param mixed $value |
||
| 1418 | * @param string $message |
||
| 1419 | * |
||
| 1420 | * @throws InvalidArgumentException |
||
| 1421 | */ |
||
| 1422 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 1433 | |||
| 1434 | /** |
||
| 1435 | * @param mixed $value |
||
| 1436 | * @param string $message |
||
| 1437 | * |
||
| 1438 | * @throws InvalidArgumentException |
||
| 1439 | */ |
||
| 1440 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 1451 | |||
| 1452 | /** |
||
| 1453 | * @param mixed $value |
||
| 1454 | * @param string $message |
||
| 1455 | * |
||
| 1456 | * @throws InvalidArgumentException |
||
| 1457 | */ |
||
| 1458 | public static function readable($value, $message = '') |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * @param mixed $value |
||
| 1470 | * @param string $message |
||
| 1471 | * |
||
| 1472 | * @throws InvalidArgumentException |
||
| 1473 | */ |
||
| 1474 | public static function writable($value, $message = '') |
||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * @psalm-assert class-string $value |
||
| 1486 | * |
||
| 1487 | * @param mixed $value |
||
| 1488 | * @param string $message |
||
| 1489 | * |
||
| 1490 | * @throws InvalidArgumentException |
||
| 1491 | */ |
||
| 1492 | 8 | public static function classExists($value, $message = '') |
|
| 1501 | |||
| 1502 | /** |
||
| 1503 | * @param mixed $value |
||
| 1504 | * @param string|object $class |
||
| 1505 | * @param string $message |
||
| 1506 | * |
||
| 1507 | * @throws InvalidArgumentException |
||
| 1508 | */ |
||
| 1509 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 1519 | |||
| 1520 | /** |
||
| 1521 | * @psalm-assert class-string $value |
||
| 1522 | * |
||
| 1523 | * @param mixed $value |
||
| 1524 | * @param string $message |
||
| 1525 | * |
||
| 1526 | * @throws InvalidArgumentException |
||
| 1527 | */ |
||
| 1528 | 8 | public static function interfaceExists($value, $message = '') |
|
| 1537 | |||
| 1538 | /** |
||
| 1539 | * @param mixed $value |
||
| 1540 | * @param mixed $interface |
||
| 1541 | * @param string $message |
||
| 1542 | * |
||
| 1543 | * @throws InvalidArgumentException |
||
| 1544 | */ |
||
| 1545 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 1555 | |||
| 1556 | /** |
||
| 1557 | * @param string|object $classOrObject |
||
| 1558 | * @param mixed $property |
||
| 1559 | * @param string $message |
||
| 1560 | * |
||
| 1561 | * @throws InvalidArgumentException |
||
| 1562 | */ |
||
| 1563 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 1572 | |||
| 1573 | /** |
||
| 1574 | * @param string|object $classOrObject |
||
| 1575 | * @param mixed $property |
||
| 1576 | * @param string $message |
||
| 1577 | * |
||
| 1578 | * @throws InvalidArgumentException |
||
| 1579 | */ |
||
| 1580 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 1589 | |||
| 1590 | /** |
||
| 1591 | * @param string|object $classOrObject |
||
| 1592 | * @param mixed $method |
||
| 1593 | * @param string $message |
||
| 1594 | * |
||
| 1595 | * @throws InvalidArgumentException |
||
| 1596 | */ |
||
| 1597 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 1606 | |||
| 1607 | /** |
||
| 1608 | * @param string|object $classOrObject |
||
| 1609 | * @param mixed $method |
||
| 1610 | * @param string $message |
||
| 1611 | * |
||
| 1612 | * @throws InvalidArgumentException |
||
| 1613 | */ |
||
| 1614 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 1623 | |||
| 1624 | /** |
||
| 1625 | * @param array $array |
||
| 1626 | * @param string|int $key |
||
| 1627 | * @param string $message |
||
| 1628 | * |
||
| 1629 | * @throws InvalidArgumentException |
||
| 1630 | */ |
||
| 1631 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 1640 | |||
| 1641 | /** |
||
| 1642 | * @param array $array |
||
| 1643 | * @param string|int $key |
||
| 1644 | * @param string $message |
||
| 1645 | * |
||
| 1646 | * @throws InvalidArgumentException |
||
| 1647 | */ |
||
| 1648 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 1657 | |||
| 1658 | /** |
||
| 1659 | * Checks if a value is a valid array key (int or string). |
||
| 1660 | * |
||
| 1661 | * @psalm-assert array-key $value |
||
| 1662 | * |
||
| 1663 | * @param mixed $value |
||
| 1664 | * @param string $message |
||
| 1665 | * |
||
| 1666 | * @throws InvalidArgumentException |
||
| 1667 | */ |
||
| 1668 | 28 | public static function validArrayKey($value, $message = '') |
|
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1680 | * |
||
| 1681 | * @param mixed $array |
||
| 1682 | * @param mixed $number |
||
| 1683 | * @param string $message |
||
| 1684 | * |
||
| 1685 | * @throws InvalidArgumentException |
||
| 1686 | */ |
||
| 1687 | 8 | public static function count($array, $number, $message = '') |
|
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1698 | * |
||
| 1699 | * @param mixed $array |
||
| 1700 | * @param mixed $min |
||
| 1701 | * @param string $message |
||
| 1702 | * |
||
| 1703 | * @throws InvalidArgumentException |
||
| 1704 | */ |
||
| 1705 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1718 | * |
||
| 1719 | * @param mixed $array |
||
| 1720 | * @param mixed $max |
||
| 1721 | * @param string $message |
||
| 1722 | * |
||
| 1723 | * @throws InvalidArgumentException |
||
| 1724 | */ |
||
| 1725 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 1735 | |||
| 1736 | /** |
||
| 1737 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1738 | * |
||
| 1739 | * @param mixed $array |
||
| 1740 | * @param mixed $min |
||
| 1741 | * @param mixed $max |
||
| 1742 | * @param string $message |
||
| 1743 | * |
||
| 1744 | * @throws InvalidArgumentException |
||
| 1745 | */ |
||
| 1746 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
| 1759 | |||
| 1760 | /** |
||
| 1761 | * @psalm-assert list $array |
||
| 1762 | * |
||
| 1763 | * @param mixed $array |
||
| 1764 | * @param string $message |
||
| 1765 | * |
||
| 1766 | * @throws InvalidArgumentException |
||
| 1767 | */ |
||
| 1768 | 80 | View Code Duplication | public static function isList($array, $message = '') |
| 1776 | |||
| 1777 | /** |
||
| 1778 | * @psalm-assert non-empty-list $array |
||
| 1779 | * |
||
| 1780 | * @param mixed $array |
||
| 1781 | * @param string $message |
||
| 1782 | * |
||
| 1783 | * @throws InvalidArgumentException |
||
| 1784 | */ |
||
| 1785 | 40 | public static function isNonEmptyList($array, $message = '') |
|
| 1790 | |||
| 1791 | /** |
||
| 1792 | * @param mixed $array |
||
| 1793 | * @param string $message |
||
| 1794 | * |
||
| 1795 | * @throws InvalidArgumentException |
||
| 1796 | */ |
||
| 1797 | 32 | public static function isMap($array, $message = '') |
|
| 1808 | |||
| 1809 | /** |
||
| 1810 | * @param mixed $array |
||
| 1811 | * @param string $message |
||
| 1812 | * |
||
| 1813 | * @throws InvalidArgumentException |
||
| 1814 | */ |
||
| 1815 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
| 1820 | |||
| 1821 | /** |
||
| 1822 | * @param mixed $value |
||
| 1823 | * @param string $message |
||
| 1824 | * |
||
| 1825 | * @throws InvalidArgumentException |
||
| 1826 | */ |
||
| 1827 | 56 | public static function uuid($value, $message = '') |
|
| 1844 | |||
| 1845 | /** |
||
| 1846 | * @param Closure $expression |
||
| 1847 | * @param string|object $class |
||
| 1848 | * @param string $message |
||
| 1849 | * |
||
| 1850 | * @throws InvalidArgumentException |
||
| 1851 | */ |
||
| 1852 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 1878 | |||
| 1879 | /** |
||
| 1880 | * @throws BadMethodCallException |
||
| 1881 | */ |
||
| 1882 | 1561 | public static function __callStatic($name, $arguments) |
|
| 1910 | |||
| 1911 | /** |
||
| 1912 | * @param mixed $value |
||
| 1913 | * |
||
| 1914 | * @return string |
||
| 1915 | */ |
||
| 1916 | 714 | protected static function valueToString($value) |
|
| 1952 | |||
| 1953 | /** |
||
| 1954 | * @param mixed $value |
||
| 1955 | * |
||
| 1956 | * @return string |
||
| 1957 | */ |
||
| 1958 | 251 | protected static function typeToString($value) |
|
| 1962 | |||
| 1963 | 168 | protected static function strlen($value) |
|
| 1975 | |||
| 1976 | /** |
||
| 1977 | * @param string $message |
||
| 1978 | * |
||
| 1979 | * @throws InvalidArgumentException |
||
| 1980 | */ |
||
| 1981 | 1028 | protected static function reportInvalidArgument($message) |
|
| 1985 | |||
| 1986 | private function __construct() |
||
| 1989 | } |
||
| 1990 |