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 |
||
| 211 | class Assert |
||
|
|
|||
| 212 | { |
||
| 213 | /** |
||
| 214 | * @psalm-pure |
||
| 215 | * @psalm-assert string $value |
||
| 216 | * |
||
| 217 | * @param mixed $value |
||
| 218 | * @param string $message |
||
| 219 | * |
||
| 220 | * @throws InvalidArgumentException |
||
| 221 | */ |
||
| 222 | 241 | public static function string($value, $message = '') |
|
| 223 | { |
||
| 224 | 241 | if (!\is_string($value)) { |
|
| 225 | 45 | static::reportInvalidArgument(\sprintf( |
|
| 226 | 45 | $message ?: 'Expected a string. Got: %s', |
|
| 227 | 45 | static::typeToString($value) |
|
| 228 | )); |
||
| 229 | } |
||
| 230 | 196 | } |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @psalm-pure |
||
| 234 | * @psalm-assert string $value |
||
| 235 | * @psalm-assert !empty $value |
||
| 236 | * |
||
| 237 | * @param mixed $value |
||
| 238 | * @param string $message |
||
| 239 | * |
||
| 240 | * @throws InvalidArgumentException |
||
| 241 | */ |
||
| 242 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 243 | { |
||
| 244 | 16 | static::string($value, $message); |
|
| 245 | 12 | static::notEq($value, '', $message); |
|
| 246 | 8 | } |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @psalm-pure |
||
| 250 | * @psalm-assert int $value |
||
| 251 | * |
||
| 252 | * @param mixed $value |
||
| 253 | * @param string $message |
||
| 254 | * |
||
| 255 | * @throws InvalidArgumentException |
||
| 256 | */ |
||
| 257 | 17 | public static function integer($value, $message = '') |
|
| 258 | { |
||
| 259 | 17 | if (!\is_int($value)) { |
|
| 260 | 13 | static::reportInvalidArgument(\sprintf( |
|
| 261 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
| 262 | 13 | static::typeToString($value) |
|
| 263 | )); |
||
| 264 | } |
||
| 265 | 4 | } |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @psalm-pure |
||
| 269 | * @psalm-assert numeric $value |
||
| 270 | * |
||
| 271 | * @param mixed $value |
||
| 272 | * @param string $message |
||
| 273 | * |
||
| 274 | * @throws InvalidArgumentException |
||
| 275 | */ |
||
| 276 | 16 | public static function integerish($value, $message = '') |
|
| 277 | { |
||
| 278 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
| 279 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 280 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
| 281 | 4 | static::typeToString($value) |
|
| 282 | )); |
||
| 283 | } |
||
| 284 | 12 | } |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @psalm-pure |
||
| 288 | * @psalm-assert float $value |
||
| 289 | * |
||
| 290 | * @param mixed $value |
||
| 291 | * @param string $message |
||
| 292 | * |
||
| 293 | * @throws InvalidArgumentException |
||
| 294 | */ |
||
| 295 | 16 | public static function float($value, $message = '') |
|
| 296 | { |
||
| 297 | 16 | if (!\is_float($value)) { |
|
| 298 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 299 | 8 | $message ?: 'Expected a float. Got: %s', |
|
| 300 | 8 | static::typeToString($value) |
|
| 301 | )); |
||
| 302 | } |
||
| 303 | 8 | } |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @psalm-pure |
||
| 307 | * @psalm-assert numeric $value |
||
| 308 | * |
||
| 309 | * @param mixed $value |
||
| 310 | * @param string $message |
||
| 311 | * |
||
| 312 | * @throws InvalidArgumentException |
||
| 313 | */ |
||
| 314 | 20 | public static function numeric($value, $message = '') |
|
| 315 | { |
||
| 316 | 20 | if (!\is_numeric($value)) { |
|
| 317 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 318 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
| 319 | 4 | static::typeToString($value) |
|
| 320 | )); |
||
| 321 | } |
||
| 322 | 16 | } |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @psalm-pure |
||
| 326 | * @psalm-assert int $value |
||
| 327 | * |
||
| 328 | * @param mixed $value |
||
| 329 | * @param string $message |
||
| 330 | * |
||
| 331 | * @throws InvalidArgumentException |
||
| 332 | */ |
||
| 333 | 24 | public static function natural($value, $message = '') |
|
| 334 | { |
||
| 335 | 24 | if (!\is_int($value) || $value < 0) { |
|
| 336 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 337 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
| 338 | 16 | static::valueToString($value) |
|
| 339 | )); |
||
| 340 | } |
||
| 341 | 8 | } |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @psalm-pure |
||
| 345 | * @psalm-assert bool $value |
||
| 346 | * |
||
| 347 | * @param mixed $value |
||
| 348 | * @param string $message |
||
| 349 | * |
||
| 350 | * @throws InvalidArgumentException |
||
| 351 | */ |
||
| 352 | 16 | public static function boolean($value, $message = '') |
|
| 353 | { |
||
| 354 | 16 | if (!\is_bool($value)) { |
|
| 355 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 356 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
| 357 | 8 | static::typeToString($value) |
|
| 358 | )); |
||
| 359 | } |
||
| 360 | 8 | } |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @psalm-pure |
||
| 364 | * @psalm-assert scalar $value |
||
| 365 | * |
||
| 366 | * @param mixed $value |
||
| 367 | * @param string $message |
||
| 368 | * |
||
| 369 | * @throws InvalidArgumentException |
||
| 370 | */ |
||
| 371 | 23 | public static function scalar($value, $message = '') |
|
| 372 | { |
||
| 373 | 23 | if (!\is_scalar($value)) { |
|
| 374 | 11 | static::reportInvalidArgument(\sprintf( |
|
| 375 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
| 376 | 11 | static::typeToString($value) |
|
| 377 | )); |
||
| 378 | } |
||
| 379 | 12 | } |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @psalm-pure |
||
| 383 | * @psalm-assert object $value |
||
| 384 | * |
||
| 385 | * @param mixed $value |
||
| 386 | * @param string $message |
||
| 387 | * |
||
| 388 | * @throws InvalidArgumentException |
||
| 389 | */ |
||
| 390 | 23 | public static function object($value, $message = '') |
|
| 391 | { |
||
| 392 | 23 | if (!\is_object($value)) { |
|
| 393 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 394 | 15 | $message ?: 'Expected an object. Got: %s', |
|
| 395 | 15 | static::typeToString($value) |
|
| 396 | )); |
||
| 397 | } |
||
| 398 | 8 | } |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @psalm-pure |
||
| 402 | * @psalm-assert resource $value |
||
| 403 | * |
||
| 404 | * @param mixed $value |
||
| 405 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
| 406 | * @param string $message |
||
| 407 | * |
||
| 408 | * @throws InvalidArgumentException |
||
| 409 | */ |
||
| 410 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 411 | { |
||
| 412 | 16 | if (!\is_resource($value)) { |
|
| 413 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 414 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
| 415 | 4 | static::typeToString($value) |
|
| 416 | )); |
||
| 417 | } |
||
| 418 | |||
| 419 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
| 420 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 421 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
| 422 | 4 | static::typeToString($value), |
|
| 423 | 4 | $type |
|
| 424 | )); |
||
| 425 | } |
||
| 426 | 8 | } |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @psalm-pure |
||
| 430 | * @psalm-assert callable $value |
||
| 431 | * |
||
| 432 | * @param mixed $value |
||
| 433 | * @param string $message |
||
| 434 | * |
||
| 435 | * @throws InvalidArgumentException |
||
| 436 | */ |
||
| 437 | 20 | public static function isCallable($value, $message = '') |
|
| 438 | { |
||
| 439 | 20 | if (!\is_callable($value)) { |
|
| 440 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 441 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
| 442 | 8 | static::typeToString($value) |
|
| 443 | )); |
||
| 444 | } |
||
| 445 | 12 | } |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @psalm-pure |
||
| 449 | * @psalm-assert array $value |
||
| 450 | * |
||
| 451 | * @param mixed $value |
||
| 452 | * @param string $message |
||
| 453 | * |
||
| 454 | * @throws InvalidArgumentException |
||
| 455 | */ |
||
| 456 | 20 | public static function isArray($value, $message = '') |
|
| 457 | { |
||
| 458 | 20 | if (!\is_array($value)) { |
|
| 459 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 460 | 12 | $message ?: 'Expected an array. Got: %s', |
|
| 461 | 12 | static::typeToString($value) |
|
| 462 | )); |
||
| 463 | } |
||
| 464 | 8 | } |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @psalm-pure |
||
| 468 | * @psalm-assert iterable $value |
||
| 469 | * |
||
| 470 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
| 471 | * |
||
| 472 | * @param mixed $value |
||
| 473 | * @param string $message |
||
| 474 | * |
||
| 475 | * @throws InvalidArgumentException |
||
| 476 | */ |
||
| 477 | 20 | public static function isTraversable($value, $message = '') |
|
| 478 | { |
||
| 479 | 20 | @\trigger_error( |
|
| 480 | 20 | \sprintf( |
|
| 481 | 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.', |
|
| 482 | 20 | __METHOD__ |
|
| 483 | ), |
||
| 484 | 20 | \E_USER_DEPRECATED |
|
| 485 | ); |
||
| 486 | |||
| 487 | 20 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
| 488 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 489 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
| 490 | 8 | static::typeToString($value) |
|
| 491 | )); |
||
| 492 | } |
||
| 493 | 12 | } |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @psalm-pure |
||
| 497 | * @psalm-assert array|ArrayAccess $value |
||
| 498 | * |
||
| 499 | * @param mixed $value |
||
| 500 | * @param string $message |
||
| 501 | * |
||
| 502 | * @throws InvalidArgumentException |
||
| 503 | */ |
||
| 504 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 505 | { |
||
| 506 | 20 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
|
| 507 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 508 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
| 509 | 8 | static::typeToString($value) |
|
| 510 | )); |
||
| 511 | } |
||
| 512 | 12 | } |
|
| 513 | |||
| 514 | /** |
||
| 515 | * @psalm-pure |
||
| 516 | * @psalm-assert countable $value |
||
| 517 | * |
||
| 518 | * @param mixed $value |
||
| 519 | * @param string $message |
||
| 520 | * |
||
| 521 | * @throws InvalidArgumentException |
||
| 522 | */ |
||
| 523 | 28 | public static function isCountable($value, $message = '') |
|
| 524 | { |
||
| 525 | if ( |
||
| 526 | 28 | !\is_array($value) |
|
| 527 | 28 | && !($value instanceof Countable) |
|
| 528 | 28 | && !($value instanceof ResourceBundle) |
|
| 529 | 28 | && !($value instanceof SimpleXMLElement) |
|
| 530 | ) { |
||
| 531 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 532 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
| 533 | 12 | static::typeToString($value) |
|
| 534 | )); |
||
| 535 | } |
||
| 536 | 16 | } |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @psalm-pure |
||
| 540 | * @psalm-assert iterable $value |
||
| 541 | * |
||
| 542 | * @param mixed $value |
||
| 543 | * @param string $message |
||
| 544 | * |
||
| 545 | * @throws InvalidArgumentException |
||
| 546 | */ |
||
| 547 | 996 | View Code Duplication | public static function isIterable($value, $message = '') |
| 548 | { |
||
| 549 | 996 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
| 550 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 551 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
| 552 | 8 | static::typeToString($value) |
|
| 553 | )); |
||
| 554 | } |
||
| 555 | 992 | } |
|
| 556 | |||
| 557 | /** |
||
| 558 | * @psalm-pure |
||
| 559 | * @psalm-template ExpectedType of object |
||
| 560 | * @psalm-param class-string<ExpectedType> $class |
||
| 561 | * @psalm-assert ExpectedType $value |
||
| 562 | * |
||
| 563 | * @param mixed $value |
||
| 564 | * @param string|object $class |
||
| 565 | * @param string $message |
||
| 566 | * |
||
| 567 | * @throws InvalidArgumentException |
||
| 568 | */ |
||
| 569 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
| 570 | { |
||
| 571 | 19 | if (!($value instanceof $class)) { |
|
| 572 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 573 | 15 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
| 574 | 15 | static::typeToString($value), |
|
| 575 | 15 | $class |
|
| 576 | )); |
||
| 577 | } |
||
| 578 | 4 | } |
|
| 579 | |||
| 580 | /** |
||
| 581 | * @psalm-pure |
||
| 582 | * @psalm-template ExpectedType of object |
||
| 583 | * @psalm-param class-string<ExpectedType> $class |
||
| 584 | * @psalm-assert !ExpectedType $value |
||
| 585 | * |
||
| 586 | * @param mixed $value |
||
| 587 | * @param string|object $class |
||
| 588 | * @param string $message |
||
| 589 | * |
||
| 590 | * @throws InvalidArgumentException |
||
| 591 | */ |
||
| 592 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 593 | { |
||
| 594 | 16 | if ($value instanceof $class) { |
|
| 595 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 596 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
| 597 | 4 | static::typeToString($value), |
|
| 598 | 4 | $class |
|
| 599 | )); |
||
| 600 | } |
||
| 601 | 12 | } |
|
| 602 | |||
| 603 | /** |
||
| 604 | * @psalm-pure |
||
| 605 | * @psalm-param array<class-string> $classes |
||
| 606 | * |
||
| 607 | * @param mixed $value |
||
| 608 | * @param array<object|string> $classes |
||
| 609 | * @param string $message |
||
| 610 | * |
||
| 611 | * @throws InvalidArgumentException |
||
| 612 | */ |
||
| 613 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 614 | { |
||
| 615 | 20 | foreach ($classes as $class) { |
|
| 616 | 20 | if ($value instanceof $class) { |
|
| 617 | 8 | return; |
|
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 622 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
| 623 | 12 | static::typeToString($value), |
|
| 624 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
| 625 | )); |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @psalm-pure |
||
| 630 | * @psalm-template ExpectedType of object |
||
| 631 | * @psalm-param class-string<ExpectedType> $class |
||
| 632 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
||
| 633 | * |
||
| 634 | * @param object|string $value |
||
| 635 | * @param string $class |
||
| 636 | * @param string $message |
||
| 637 | * |
||
| 638 | * @throws InvalidArgumentException |
||
| 639 | */ |
||
| 640 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
| 641 | { |
||
| 642 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 643 | |||
| 644 | 16 | if (!\is_a($value, $class, \is_string($value))) { |
|
| 645 | 12 | static::reportInvalidArgument(sprintf( |
|
| 646 | 12 | $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', |
|
| 647 | 12 | static::typeToString($value), |
|
| 648 | $class |
||
| 649 | )); |
||
| 650 | } |
||
| 651 | 4 | } |
|
| 652 | |||
| 653 | /** |
||
| 654 | * @psalm-pure |
||
| 655 | * @psalm-template UnexpectedType of object |
||
| 656 | * @psalm-param class-string<UnexpectedType> $class |
||
| 657 | * @psalm-assert !UnexpectedType $value |
||
| 658 | * @psalm-assert !class-string<UnexpectedType> $value |
||
| 659 | * |
||
| 660 | * @param object|string $value |
||
| 661 | * @param string $class |
||
| 662 | * @param string $message |
||
| 663 | * |
||
| 664 | * @throws InvalidArgumentException |
||
| 665 | */ |
||
| 666 | 20 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
| 667 | { |
||
| 668 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 669 | |||
| 670 | 16 | if (\is_a($value, $class, \is_string($value))) { |
|
| 671 | 4 | static::reportInvalidArgument(sprintf( |
|
| 672 | 4 | $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
| 673 | 4 | static::typeToString($value), |
|
| 674 | $class |
||
| 675 | )); |
||
| 676 | } |
||
| 677 | 12 | } |
|
| 678 | |||
| 679 | /** |
||
| 680 | * @psalm-pure |
||
| 681 | * @psalm-param array<class-string> $classes |
||
| 682 | * |
||
| 683 | * @param object|string $value |
||
| 684 | * @param string[] $classes |
||
| 685 | * @param string $message |
||
| 686 | * |
||
| 687 | * @throws InvalidArgumentException |
||
| 688 | */ |
||
| 689 | 24 | public static function isAnyOf($value, array $classes, $message = '') |
|
| 690 | { |
||
| 691 | 24 | foreach ($classes as $class) { |
|
| 692 | 24 | static::string($class, 'Expected class as a string. Got: %s'); |
|
| 693 | |||
| 694 | 20 | if (\is_a($value, $class, \is_string($value))) { |
|
| 695 | 8 | return; |
|
| 696 | } |
||
| 697 | } |
||
| 698 | |||
| 699 | 12 | static::reportInvalidArgument(sprintf( |
|
| 700 | 12 | $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
| 701 | 12 | static::typeToString($value), |
|
| 702 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
| 703 | )); |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @psalm-pure |
||
| 708 | * @psalm-assert empty $value |
||
| 709 | * |
||
| 710 | * @param mixed $value |
||
| 711 | * @param string $message |
||
| 712 | * |
||
| 713 | * @throws InvalidArgumentException |
||
| 714 | */ |
||
| 715 | 23 | public static function isEmpty($value, $message = '') |
|
| 716 | { |
||
| 717 | 23 | if (!empty($value)) { |
|
| 718 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 719 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
| 720 | 8 | static::valueToString($value) |
|
| 721 | )); |
||
| 722 | } |
||
| 723 | 15 | } |
|
| 724 | |||
| 725 | /** |
||
| 726 | * @psalm-pure |
||
| 727 | * @psalm-assert !empty $value |
||
| 728 | * |
||
| 729 | * @param mixed $value |
||
| 730 | * @param string $message |
||
| 731 | * |
||
| 732 | * @throws InvalidArgumentException |
||
| 733 | */ |
||
| 734 | 55 | public static function notEmpty($value, $message = '') |
|
| 735 | { |
||
| 736 | 55 | if (empty($value)) { |
|
| 737 | 23 | static::reportInvalidArgument(\sprintf( |
|
| 738 | 23 | $message ?: 'Expected a non-empty value. Got: %s', |
|
| 739 | 23 | static::valueToString($value) |
|
| 740 | )); |
||
| 741 | } |
||
| 742 | 32 | } |
|
| 743 | |||
| 744 | /** |
||
| 745 | * @psalm-pure |
||
| 746 | * @psalm-assert null $value |
||
| 747 | * |
||
| 748 | * @param mixed $value |
||
| 749 | * @param string $message |
||
| 750 | * |
||
| 751 | * @throws InvalidArgumentException |
||
| 752 | */ |
||
| 753 | 11 | public static function null($value, $message = '') |
|
| 754 | { |
||
| 755 | 11 | if (null !== $value) { |
|
| 756 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 757 | 8 | $message ?: 'Expected null. Got: %s', |
|
| 758 | 8 | static::valueToString($value) |
|
| 759 | )); |
||
| 760 | } |
||
| 761 | 3 | } |
|
| 762 | |||
| 763 | /** |
||
| 764 | * @psalm-pure |
||
| 765 | * @psalm-assert !null $value |
||
| 766 | * |
||
| 767 | * @param mixed $value |
||
| 768 | * @param string $message |
||
| 769 | * |
||
| 770 | * @throws InvalidArgumentException |
||
| 771 | */ |
||
| 772 | 11 | public static function notNull($value, $message = '') |
|
| 773 | { |
||
| 774 | 11 | if (null === $value) { |
|
| 775 | 3 | static::reportInvalidArgument( |
|
| 776 | 3 | $message ?: 'Expected a value other than null.' |
|
| 777 | ); |
||
| 778 | } |
||
| 779 | 8 | } |
|
| 780 | |||
| 781 | /** |
||
| 782 | * @psalm-pure |
||
| 783 | * @psalm-assert true $value |
||
| 784 | * |
||
| 785 | * @param mixed $value |
||
| 786 | * @param string $message |
||
| 787 | * |
||
| 788 | * @throws InvalidArgumentException |
||
| 789 | */ |
||
| 790 | 15 | public static function true($value, $message = '') |
|
| 791 | { |
||
| 792 | 15 | if (true !== $value) { |
|
| 793 | 11 | static::reportInvalidArgument(\sprintf( |
|
| 794 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
| 795 | 11 | static::valueToString($value) |
|
| 796 | )); |
||
| 797 | } |
||
| 798 | 4 | } |
|
| 799 | |||
| 800 | /** |
||
| 801 | * @psalm-pure |
||
| 802 | * @psalm-assert false $value |
||
| 803 | * |
||
| 804 | * @param mixed $value |
||
| 805 | * @param string $message |
||
| 806 | * |
||
| 807 | * @throws InvalidArgumentException |
||
| 808 | */ |
||
| 809 | 19 | public static function false($value, $message = '') |
|
| 810 | { |
||
| 811 | 19 | if (false !== $value) { |
|
| 812 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 813 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
| 814 | 15 | static::valueToString($value) |
|
| 815 | )); |
||
| 816 | } |
||
| 817 | 4 | } |
|
| 818 | |||
| 819 | /** |
||
| 820 | * @psalm-assert !false $value |
||
| 821 | * |
||
| 822 | * @param mixed $value |
||
| 823 | * @param string $message |
||
| 824 | * |
||
| 825 | * @throws InvalidArgumentException |
||
| 826 | */ |
||
| 827 | 19 | public static function notFalse($value, $message = '') |
|
| 828 | { |
||
| 829 | 19 | if (false === $value) { |
|
| 830 | 4 | static::reportInvalidArgument( |
|
| 831 | 4 | $message ?: 'Expected a value other than false.' |
|
| 832 | ); |
||
| 833 | } |
||
| 834 | 15 | } |
|
| 835 | |||
| 836 | /** |
||
| 837 | * @param mixed $value |
||
| 838 | * @param string $message |
||
| 839 | * |
||
| 840 | * @throws InvalidArgumentException |
||
| 841 | */ |
||
| 842 | 51 | View Code Duplication | public static function ip($value, $message = '') |
| 843 | { |
||
| 844 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
|
| 845 | 19 | static::reportInvalidArgument(\sprintf( |
|
| 846 | 19 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
| 847 | 19 | static::valueToString($value) |
|
| 848 | )); |
||
| 849 | } |
||
| 850 | 32 | } |
|
| 851 | |||
| 852 | /** |
||
| 853 | * @param mixed $value |
||
| 854 | * @param string $message |
||
| 855 | * |
||
| 856 | * @throws InvalidArgumentException |
||
| 857 | */ |
||
| 858 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
| 859 | { |
||
| 860 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
| 861 | 35 | static::reportInvalidArgument(\sprintf( |
|
| 862 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
| 863 | 35 | static::valueToString($value) |
|
| 864 | )); |
||
| 865 | } |
||
| 866 | 16 | } |
|
| 867 | |||
| 868 | /** |
||
| 869 | * @param mixed $value |
||
| 870 | * @param string $message |
||
| 871 | * |
||
| 872 | * @throws InvalidArgumentException |
||
| 873 | */ |
||
| 874 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
| 875 | { |
||
| 876 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
| 877 | 31 | static::reportInvalidArgument(\sprintf( |
|
| 878 | 31 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
| 879 | 31 | static::valueToString($value) |
|
| 880 | )); |
||
| 881 | } |
||
| 882 | 20 | } |
|
| 883 | |||
| 884 | /** |
||
| 885 | * @param mixed $value |
||
| 886 | * @param string $message |
||
| 887 | * |
||
| 888 | * @throws InvalidArgumentException |
||
| 889 | */ |
||
| 890 | 20 | View Code Duplication | public static function email($value, $message = '') |
| 891 | { |
||
| 892 | 20 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 893 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 894 | 12 | $message ?: 'Expected a value to be a valid e-mail address. Got %s', |
|
| 895 | 12 | static::valueToString($value) |
|
| 896 | )); |
||
| 897 | } |
||
| 898 | 8 | } |
|
| 899 | |||
| 900 | /** |
||
| 901 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
| 902 | * |
||
| 903 | * @param array $values |
||
| 904 | * @param string $message |
||
| 905 | * |
||
| 906 | * @throws InvalidArgumentException |
||
| 907 | */ |
||
| 908 | 12 | public static function uniqueValues(array $values, $message = '') |
|
| 923 | |||
| 924 | /** |
||
| 925 | * @param mixed $value |
||
| 926 | * @param mixed $expect |
||
| 927 | * @param string $message |
||
| 928 | * |
||
| 929 | * @throws InvalidArgumentException |
||
| 930 | */ |
||
| 931 | 33 | public static function eq($value, $expect, $message = '') |
|
| 932 | { |
||
| 933 | 33 | if ($expect != $value) { |
|
| 934 | 17 | static::reportInvalidArgument(\sprintf( |
|
| 935 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 936 | 17 | static::valueToString($value), |
|
| 937 | 17 | static::valueToString($expect) |
|
| 938 | )); |
||
| 939 | } |
||
| 940 | 16 | } |
|
| 941 | |||
| 942 | /** |
||
| 943 | * @param mixed $value |
||
| 944 | * @param mixed $expect |
||
| 945 | * @param string $message |
||
| 946 | * |
||
| 947 | * @throws InvalidArgumentException |
||
| 948 | */ |
||
| 949 | 28 | public static function notEq($value, $expect, $message = '') |
|
| 950 | { |
||
| 951 | 28 | if ($expect == $value) { |
|
| 952 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 953 | 16 | $message ?: 'Expected a different value than %s.', |
|
| 954 | 16 | static::valueToString($expect) |
|
| 955 | )); |
||
| 956 | } |
||
| 957 | 12 | } |
|
| 958 | |||
| 959 | /** |
||
| 960 | * @psalm-pure |
||
| 961 | * |
||
| 962 | * @param mixed $value |
||
| 963 | * @param mixed $expect |
||
| 964 | * @param string $message |
||
| 965 | * |
||
| 966 | * @throws InvalidArgumentException |
||
| 967 | */ |
||
| 968 | 16 | public static function same($value, $expect, $message = '') |
|
| 978 | |||
| 979 | /** |
||
| 980 | * @psalm-pure |
||
| 981 | * |
||
| 982 | * @param mixed $value |
||
| 983 | * @param mixed $expect |
||
| 984 | * @param string $message |
||
| 985 | * |
||
| 986 | * @throws InvalidArgumentException |
||
| 987 | */ |
||
| 988 | 16 | public static function notSame($value, $expect, $message = '') |
|
| 997 | |||
| 998 | /** |
||
| 999 | * @psalm-pure |
||
| 1000 | * |
||
| 1001 | * @param mixed $value |
||
| 1002 | * @param mixed $limit |
||
| 1003 | * @param string $message |
||
| 1004 | * |
||
| 1005 | * @throws InvalidArgumentException |
||
| 1006 | */ |
||
| 1007 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 1017 | |||
| 1018 | /** |
||
| 1019 | * @psalm-pure |
||
| 1020 | * |
||
| 1021 | * @param mixed $value |
||
| 1022 | * @param mixed $limit |
||
| 1023 | * @param string $message |
||
| 1024 | * |
||
| 1025 | * @throws InvalidArgumentException |
||
| 1026 | */ |
||
| 1027 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 1037 | |||
| 1038 | /** |
||
| 1039 | * @psalm-pure |
||
| 1040 | * |
||
| 1041 | * @param mixed $value |
||
| 1042 | * @param mixed $limit |
||
| 1043 | * @param string $message |
||
| 1044 | * |
||
| 1045 | * @throws InvalidArgumentException |
||
| 1046 | */ |
||
| 1047 | 9 | public static function lessThan($value, $limit, $message = '') |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * @psalm-pure |
||
| 1060 | * |
||
| 1061 | * @param mixed $value |
||
| 1062 | * @param mixed $limit |
||
| 1063 | * @param string $message |
||
| 1064 | * |
||
| 1065 | * @throws InvalidArgumentException |
||
| 1066 | */ |
||
| 1067 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
| 1080 | * |
||
| 1081 | * @psalm-pure |
||
| 1082 | * |
||
| 1083 | * @param mixed $value |
||
| 1084 | * @param mixed $min |
||
| 1085 | * @param mixed $max |
||
| 1086 | * @param string $message |
||
| 1087 | * |
||
| 1088 | * @throws InvalidArgumentException |
||
| 1089 | */ |
||
| 1090 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 1101 | |||
| 1102 | /** |
||
| 1103 | * A more human-readable alias of Assert::inArray(). |
||
| 1104 | * |
||
| 1105 | * @psalm-pure |
||
| 1106 | * |
||
| 1107 | * @param mixed $value |
||
| 1108 | * @param array $values |
||
| 1109 | * @param string $message |
||
| 1110 | * |
||
| 1111 | * @throws InvalidArgumentException |
||
| 1112 | */ |
||
| 1113 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion. |
||
| 1120 | * |
||
| 1121 | * @psalm-pure |
||
| 1122 | * |
||
| 1123 | * @param mixed $value |
||
| 1124 | * @param array $values |
||
| 1125 | * @param string $message |
||
| 1126 | * |
||
| 1127 | * @throws InvalidArgumentException |
||
| 1128 | */ |
||
| 1129 | 16 | public static function inArray($value, array $values, $message = '') |
|
| 1139 | |||
| 1140 | /** |
||
| 1141 | * @psalm-pure |
||
| 1142 | * |
||
| 1143 | * @param string $value |
||
| 1144 | * @param string $subString |
||
| 1145 | * @param string $message |
||
| 1146 | * |
||
| 1147 | * @throws InvalidArgumentException |
||
| 1148 | */ |
||
| 1149 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 1159 | |||
| 1160 | /** |
||
| 1161 | * @psalm-pure |
||
| 1162 | * |
||
| 1163 | * @param string $value |
||
| 1164 | * @param string $subString |
||
| 1165 | * @param string $message |
||
| 1166 | * |
||
| 1167 | * @throws InvalidArgumentException |
||
| 1168 | */ |
||
| 1169 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 1179 | |||
| 1180 | /** |
||
| 1181 | * @psalm-pure |
||
| 1182 | * |
||
| 1183 | * @param string $value |
||
| 1184 | * @param string $message |
||
| 1185 | * |
||
| 1186 | * @throws InvalidArgumentException |
||
| 1187 | */ |
||
| 1188 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 1197 | |||
| 1198 | /** |
||
| 1199 | * @psalm-pure |
||
| 1200 | * |
||
| 1201 | * @param string $value |
||
| 1202 | * @param string $prefix |
||
| 1203 | * @param string $message |
||
| 1204 | * |
||
| 1205 | * @throws InvalidArgumentException |
||
| 1206 | */ |
||
| 1207 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 1217 | |||
| 1218 | /** |
||
| 1219 | * @psalm-pure |
||
| 1220 | * |
||
| 1221 | * @param mixed $value |
||
| 1222 | * @param string $message |
||
| 1223 | * |
||
| 1224 | * @throws InvalidArgumentException |
||
| 1225 | */ |
||
| 1226 | 35 | public static function startsWithLetter($value, $message = '') |
|
| 1246 | |||
| 1247 | /** |
||
| 1248 | * @psalm-pure |
||
| 1249 | * |
||
| 1250 | * @param string $value |
||
| 1251 | * @param string $suffix |
||
| 1252 | * @param string $message |
||
| 1253 | * |
||
| 1254 | * @throws InvalidArgumentException |
||
| 1255 | */ |
||
| 1256 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 1266 | |||
| 1267 | /** |
||
| 1268 | * @psalm-pure |
||
| 1269 | * |
||
| 1270 | * @param string $value |
||
| 1271 | * @param string $pattern |
||
| 1272 | * @param string $message |
||
| 1273 | * |
||
| 1274 | * @throws InvalidArgumentException |
||
| 1275 | */ |
||
| 1276 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 1285 | |||
| 1286 | /** |
||
| 1287 | * @psalm-pure |
||
| 1288 | * |
||
| 1289 | * @param string $value |
||
| 1290 | * @param string $pattern |
||
| 1291 | * @param string $message |
||
| 1292 | * |
||
| 1293 | * @throws InvalidArgumentException |
||
| 1294 | */ |
||
| 1295 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
| 1306 | |||
| 1307 | /** |
||
| 1308 | * @psalm-pure |
||
| 1309 | * |
||
| 1310 | * @param mixed $value |
||
| 1311 | * @param string $message |
||
| 1312 | * |
||
| 1313 | * @throws InvalidArgumentException |
||
| 1314 | */ |
||
| 1315 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
| 1326 | |||
| 1327 | /** |
||
| 1328 | * @psalm-pure |
||
| 1329 | * |
||
| 1330 | * @param mixed $value |
||
| 1331 | * @param string $message |
||
| 1332 | * |
||
| 1333 | * @throws InvalidArgumentException |
||
| 1334 | */ |
||
| 1335 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
| 1351 | |||
| 1352 | /** |
||
| 1353 | * @psalm-pure |
||
| 1354 | * |
||
| 1355 | * @param string $value |
||
| 1356 | * @param string $message |
||
| 1357 | * |
||
| 1358 | * @throws InvalidArgumentException |
||
| 1359 | */ |
||
| 1360 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 1374 | |||
| 1375 | /** |
||
| 1376 | * @psalm-pure |
||
| 1377 | * |
||
| 1378 | * @param string $value |
||
| 1379 | * @param string $message |
||
| 1380 | * |
||
| 1381 | * @throws InvalidArgumentException |
||
| 1382 | */ |
||
| 1383 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 1397 | |||
| 1398 | /** |
||
| 1399 | * @psalm-pure |
||
| 1400 | * @psalm-assert lowercase-string $value |
||
| 1401 | * |
||
| 1402 | * @param string $value |
||
| 1403 | * @param string $message |
||
| 1404 | * |
||
| 1405 | * @throws InvalidArgumentException |
||
| 1406 | */ |
||
| 1407 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 1421 | |||
| 1422 | /** |
||
| 1423 | * @psalm-pure |
||
| 1424 | * @psalm-assert !lowercase-string $value |
||
| 1425 | * |
||
| 1426 | * @param string $value |
||
| 1427 | * @param string $message |
||
| 1428 | * |
||
| 1429 | * @throws InvalidArgumentException |
||
| 1430 | */ |
||
| 1431 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 1445 | |||
| 1446 | /** |
||
| 1447 | * @psalm-pure |
||
| 1448 | * |
||
| 1449 | * @param string $value |
||
| 1450 | * @param int $length |
||
| 1451 | * @param string $message |
||
| 1452 | * |
||
| 1453 | * @throws InvalidArgumentException |
||
| 1454 | */ |
||
| 1455 | 36 | public static function length($value, $length, $message = '') |
|
| 1465 | |||
| 1466 | /** |
||
| 1467 | * Inclusive min. |
||
| 1468 | * |
||
| 1469 | * @psalm-pure |
||
| 1470 | * |
||
| 1471 | * @param string $value |
||
| 1472 | * @param int|float $min |
||
| 1473 | * @param string $message |
||
| 1474 | * |
||
| 1475 | * @throws InvalidArgumentException |
||
| 1476 | */ |
||
| 1477 | 36 | public static function minLength($value, $min, $message = '') |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Inclusive max. |
||
| 1490 | * |
||
| 1491 | * @psalm-pure |
||
| 1492 | * |
||
| 1493 | * @param string $value |
||
| 1494 | * @param int|float $max |
||
| 1495 | * @param string $message |
||
| 1496 | * |
||
| 1497 | * @throws InvalidArgumentException |
||
| 1498 | */ |
||
| 1499 | 36 | public static function maxLength($value, $max, $message = '') |
|
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
| 1512 | * |
||
| 1513 | * @psalm-pure |
||
| 1514 | * |
||
| 1515 | * @param string $value |
||
| 1516 | * @param int|float $min |
||
| 1517 | * @param int|float $max |
||
| 1518 | * @param string $message |
||
| 1519 | * |
||
| 1520 | * @throws InvalidArgumentException |
||
| 1521 | */ |
||
| 1522 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 1535 | |||
| 1536 | /** |
||
| 1537 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
| 1538 | * |
||
| 1539 | * @param mixed $value |
||
| 1540 | * @param string $message |
||
| 1541 | * |
||
| 1542 | * @throws InvalidArgumentException |
||
| 1543 | */ |
||
| 1544 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
| 1555 | |||
| 1556 | /** |
||
| 1557 | * @param mixed $value |
||
| 1558 | * @param string $message |
||
| 1559 | * |
||
| 1560 | * @throws InvalidArgumentException |
||
| 1561 | */ |
||
| 1562 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 1573 | |||
| 1574 | /** |
||
| 1575 | * @param mixed $value |
||
| 1576 | * @param string $message |
||
| 1577 | * |
||
| 1578 | * @throws InvalidArgumentException |
||
| 1579 | */ |
||
| 1580 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 1591 | |||
| 1592 | /** |
||
| 1593 | * @param string $value |
||
| 1594 | * @param string $message |
||
| 1595 | * |
||
| 1596 | * @throws InvalidArgumentException |
||
| 1597 | */ |
||
| 1598 | public static function readable($value, $message = '') |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * @param string $value |
||
| 1610 | * @param string $message |
||
| 1611 | * |
||
| 1612 | * @throws InvalidArgumentException |
||
| 1613 | */ |
||
| 1614 | public static function writable($value, $message = '') |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * @param mixed $value |
||
| 1626 | * @param string $message |
||
| 1627 | * |
||
| 1628 | * @throws InvalidArgumentException |
||
| 1629 | */ |
||
| 1630 | 8 | public static function classExists($value, $message = '') |
|
| 1639 | |||
| 1640 | /** |
||
| 1641 | * @psalm-pure |
||
| 1642 | * @psalm-template ExpectedType of object |
||
| 1643 | * @psalm-param class-string<ExpectedType> $class |
||
| 1644 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
| 1645 | * |
||
| 1646 | * @param mixed $value |
||
| 1647 | * @param string|object $class |
||
| 1648 | * @param string $message |
||
| 1649 | * |
||
| 1650 | * @throws InvalidArgumentException |
||
| 1651 | */ |
||
| 1652 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 1662 | |||
| 1663 | /** |
||
| 1664 | * @param mixed $value |
||
| 1665 | * @param string $message |
||
| 1666 | * |
||
| 1667 | * @throws InvalidArgumentException |
||
| 1668 | */ |
||
| 1669 | 8 | public static function interfaceExists($value, $message = '') |
|
| 1678 | |||
| 1679 | /** |
||
| 1680 | * @psalm-pure |
||
| 1681 | * @psalm-template ExpectedType of object |
||
| 1682 | * @psalm-param class-string<ExpectedType> $interface |
||
| 1683 | * @psalm-assert class-string<ExpectedType> $value |
||
| 1684 | * |
||
| 1685 | * @param mixed $value |
||
| 1686 | * @param mixed $interface |
||
| 1687 | * @param string $message |
||
| 1688 | * |
||
| 1689 | * @throws InvalidArgumentException |
||
| 1690 | */ |
||
| 1691 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 1701 | |||
| 1702 | /** |
||
| 1703 | * @psalm-pure |
||
| 1704 | * @psalm-param class-string|object $classOrObject |
||
| 1705 | * |
||
| 1706 | * @param string|object $classOrObject |
||
| 1707 | * @param mixed $property |
||
| 1708 | * @param string $message |
||
| 1709 | * |
||
| 1710 | * @throws InvalidArgumentException |
||
| 1711 | */ |
||
| 1712 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 1721 | |||
| 1722 | /** |
||
| 1723 | * @psalm-pure |
||
| 1724 | * @psalm-param class-string|object $classOrObject |
||
| 1725 | * |
||
| 1726 | * @param string|object $classOrObject |
||
| 1727 | * @param mixed $property |
||
| 1728 | * @param string $message |
||
| 1729 | * |
||
| 1730 | * @throws InvalidArgumentException |
||
| 1731 | */ |
||
| 1732 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 1741 | |||
| 1742 | /** |
||
| 1743 | * @psalm-pure |
||
| 1744 | * @psalm-param class-string|object $classOrObject |
||
| 1745 | * |
||
| 1746 | * @param string|object $classOrObject |
||
| 1747 | * @param mixed $method |
||
| 1748 | * @param string $message |
||
| 1749 | * |
||
| 1750 | * @throws InvalidArgumentException |
||
| 1751 | */ |
||
| 1752 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 1761 | |||
| 1762 | /** |
||
| 1763 | * @psalm-pure |
||
| 1764 | * @psalm-param class-string|object $classOrObject |
||
| 1765 | * |
||
| 1766 | * @param string|object $classOrObject |
||
| 1767 | * @param mixed $method |
||
| 1768 | * @param string $message |
||
| 1769 | * |
||
| 1770 | * @throws InvalidArgumentException |
||
| 1771 | */ |
||
| 1772 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 1781 | |||
| 1782 | /** |
||
| 1783 | * @psalm-pure |
||
| 1784 | * |
||
| 1785 | * @param array $array |
||
| 1786 | * @param string|int $key |
||
| 1787 | * @param string $message |
||
| 1788 | * |
||
| 1789 | * @throws InvalidArgumentException |
||
| 1790 | */ |
||
| 1791 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 1800 | |||
| 1801 | /** |
||
| 1802 | * @psalm-pure |
||
| 1803 | * |
||
| 1804 | * @param array $array |
||
| 1805 | * @param string|int $key |
||
| 1806 | * @param string $message |
||
| 1807 | * |
||
| 1808 | * @throws InvalidArgumentException |
||
| 1809 | */ |
||
| 1810 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 1819 | |||
| 1820 | /** |
||
| 1821 | * Checks if a value is a valid array key (int or string). |
||
| 1822 | * |
||
| 1823 | * @psalm-pure |
||
| 1824 | * @psalm-assert array-key $value |
||
| 1825 | * |
||
| 1826 | * @param mixed $value |
||
| 1827 | * @param string $message |
||
| 1828 | * |
||
| 1829 | * @throws InvalidArgumentException |
||
| 1830 | */ |
||
| 1831 | 28 | public static function validArrayKey($value, $message = '') |
|
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1843 | * |
||
| 1844 | * @param Countable|array $array |
||
| 1845 | * @param int $number |
||
| 1846 | * @param string $message |
||
| 1847 | * |
||
| 1848 | * @throws InvalidArgumentException |
||
| 1849 | */ |
||
| 1850 | 8 | public static function count($array, $number, $message = '') |
|
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1865 | * |
||
| 1866 | * @param Countable|array $array |
||
| 1867 | * @param int|float $min |
||
| 1868 | * @param string $message |
||
| 1869 | * |
||
| 1870 | * @throws InvalidArgumentException |
||
| 1871 | */ |
||
| 1872 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 1882 | |||
| 1883 | /** |
||
| 1884 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1885 | * |
||
| 1886 | * @param Countable|array $array |
||
| 1887 | * @param int|float $max |
||
| 1888 | * @param string $message |
||
| 1889 | * |
||
| 1890 | * @throws InvalidArgumentException |
||
| 1891 | */ |
||
| 1892 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 1902 | |||
| 1903 | /** |
||
| 1904 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1905 | * |
||
| 1906 | * @param Countable|array $array |
||
| 1907 | * @param int|float $min |
||
| 1908 | * @param int|float $max |
||
| 1909 | * @param string $message |
||
| 1910 | * |
||
| 1911 | * @throws InvalidArgumentException |
||
| 1912 | */ |
||
| 1913 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
| 1926 | |||
| 1927 | /** |
||
| 1928 | * @psalm-pure |
||
| 1929 | * @psalm-assert list $array |
||
| 1930 | * |
||
| 1931 | * @param mixed $array |
||
| 1932 | * @param string $message |
||
| 1933 | * |
||
| 1934 | * @throws InvalidArgumentException |
||
| 1935 | */ |
||
| 1936 | 80 | View Code Duplication | public static function isList($array, $message = '') |
| 1944 | |||
| 1945 | /** |
||
| 1946 | * @psalm-pure |
||
| 1947 | * @psalm-assert list $array |
||
| 1948 | * @psalm-assert !empty $array |
||
| 1949 | * |
||
| 1950 | * @param mixed $array |
||
| 1951 | * @param string $message |
||
| 1952 | * |
||
| 1953 | * @throws InvalidArgumentException |
||
| 1954 | */ |
||
| 1955 | 40 | public static function isNonEmptyList($array, $message = '') |
|
| 1960 | |||
| 1961 | /** |
||
| 1962 | * @psalm-pure |
||
| 1963 | * @psalm-template T |
||
| 1964 | * @psalm-param mixed|array<T> $array |
||
| 1965 | * @psalm-assert array<string, T> $array |
||
| 1966 | * |
||
| 1967 | * @param mixed $array |
||
| 1968 | * @param string $message |
||
| 1969 | * |
||
| 1970 | * @throws InvalidArgumentException |
||
| 1971 | */ |
||
| 1972 | 32 | public static function isMap($array, $message = '') |
|
| 1983 | |||
| 1984 | /** |
||
| 1985 | * @psalm-pure |
||
| 1986 | * @psalm-template T |
||
| 1987 | * @psalm-param mixed|array<T> $array |
||
| 1988 | * @psalm-assert array<string, T> $array |
||
| 1989 | * @psalm-assert !empty $array |
||
| 1990 | * |
||
| 1991 | * @param mixed $array |
||
| 1992 | * @param string $message |
||
| 1993 | * |
||
| 1994 | * @throws InvalidArgumentException |
||
| 1995 | */ |
||
| 1996 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
| 2001 | |||
| 2002 | /** |
||
| 2003 | * @psalm-pure |
||
| 2004 | * |
||
| 2005 | * @param string $value |
||
| 2006 | * @param string $message |
||
| 2007 | * |
||
| 2008 | * @throws InvalidArgumentException |
||
| 2009 | */ |
||
| 2010 | 56 | public static function uuid($value, $message = '') |
|
| 2027 | |||
| 2028 | /** |
||
| 2029 | * @psalm-param class-string<Throwable> |
||
| 2030 | * |
||
| 2031 | * @param Closure $expression |
||
| 2032 | * @param string $class |
||
| 2033 | * @param string $message |
||
| 2034 | * |
||
| 2035 | * @throws InvalidArgumentException |
||
| 2036 | */ |
||
| 2037 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 2063 | |||
| 2064 | /** |
||
| 2065 | * @throws BadMethodCallException |
||
| 2066 | */ |
||
| 2067 | 1568 | public static function __callStatic($name, $arguments) |
|
| 2095 | |||
| 2096 | /** |
||
| 2097 | * @param mixed $value |
||
| 2098 | * |
||
| 2099 | * @return string |
||
| 2100 | */ |
||
| 2101 | 719 | protected static function valueToString($value) |
|
| 2141 | |||
| 2142 | /** |
||
| 2143 | * @param mixed $value |
||
| 2144 | * |
||
| 2145 | * @return string |
||
| 2146 | */ |
||
| 2147 | 251 | protected static function typeToString($value) |
|
| 2151 | |||
| 2152 | 168 | protected static function strlen($value) |
|
| 2164 | |||
| 2165 | /** |
||
| 2166 | * @param string $message |
||
| 2167 | * |
||
| 2168 | * @throws InvalidArgumentException |
||
| 2169 | * |
||
| 2170 | * @psalm-pure this method is not supposed to perform side-effects |
||
| 2171 | */ |
||
| 2172 | 1033 | protected static function reportInvalidArgument($message) |
|
| 2176 | |||
| 2177 | private function __construct() |
||
| 2180 | } |
||
| 2181 |