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 |
||
| 193 | class Assert |
||
|
|
|||
| 194 | { |
||
| 195 | /** |
||
| 196 | * @psalm-assert string $value |
||
| 197 | * |
||
| 198 | * @param mixed $value |
||
| 199 | * @param string $message |
||
| 200 | */ |
||
| 201 | 122 | public static function string($value, $message = '') |
|
| 202 | { |
||
| 203 | 122 | if (!\is_string($value)) { |
|
| 204 | 14 | static::reportInvalidArgument(\sprintf( |
|
| 205 | 14 | $message ?: 'Expected a string. Got: %s', |
|
| 206 | 14 | static::typeToString($value) |
|
| 207 | )); |
||
| 208 | } |
||
| 209 | 108 | } |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @psalm-assert string $value |
||
| 213 | * |
||
| 214 | * @param mixed $value |
||
| 215 | * @param string $message |
||
| 216 | */ |
||
| 217 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 218 | { |
||
| 219 | 16 | static::string($value, $message); |
|
| 220 | 12 | static::notEq($value, '', $message); |
|
| 221 | 8 | } |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @psalm-assert int $value |
||
| 225 | * |
||
| 226 | * @param mixed $value |
||
| 227 | * @param string $message |
||
| 228 | */ |
||
| 229 | 17 | public static function integer($value, $message = '') |
|
| 230 | { |
||
| 231 | 17 | if (!\is_int($value)) { |
|
| 232 | 13 | static::reportInvalidArgument(\sprintf( |
|
| 233 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
| 234 | 13 | static::typeToString($value) |
|
| 235 | )); |
||
| 236 | } |
||
| 237 | 4 | } |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @psalm-assert numeric $value |
||
| 241 | * |
||
| 242 | * @param mixed $value |
||
| 243 | * @param string $message |
||
| 244 | */ |
||
| 245 | 16 | public static function integerish($value, $message = '') |
|
| 246 | { |
||
| 247 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
| 248 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 249 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
| 250 | 4 | static::typeToString($value) |
|
| 251 | )); |
||
| 252 | } |
||
| 253 | 12 | } |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @psalm-assert float $value |
||
| 257 | * |
||
| 258 | * @param mixed $value |
||
| 259 | * @param string $message |
||
| 260 | */ |
||
| 261 | 16 | public static function float($value, $message = '') |
|
| 262 | { |
||
| 263 | 16 | if (!\is_float($value)) { |
|
| 264 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 265 | 8 | $message ?: 'Expected a float. Got: %s', |
|
| 266 | 8 | static::typeToString($value) |
|
| 267 | )); |
||
| 268 | } |
||
| 269 | 8 | } |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @psalm-assert numeric $value |
||
| 273 | * |
||
| 274 | * @param mixed $value |
||
| 275 | * @param string $message |
||
| 276 | */ |
||
| 277 | 20 | public static function numeric($value, $message = '') |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @psalm-assert int $value |
||
| 289 | * |
||
| 290 | * @param mixed $value |
||
| 291 | * @param string $message |
||
| 292 | */ |
||
| 293 | 24 | public static function natural($value, $message = '') |
|
| 294 | { |
||
| 295 | 24 | if (!\is_int($value) || $value < 0) { |
|
| 296 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 297 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
| 298 | 16 | static::valueToString($value) |
|
| 299 | )); |
||
| 300 | } |
||
| 301 | 8 | } |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @psalm-assert bool $value |
||
| 305 | * |
||
| 306 | * @param mixed $value |
||
| 307 | * @param string $message |
||
| 308 | */ |
||
| 309 | 16 | public static function boolean($value, $message = '') |
|
| 310 | { |
||
| 311 | 16 | if (!\is_bool($value)) { |
|
| 312 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 313 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
| 314 | 8 | static::typeToString($value) |
|
| 315 | )); |
||
| 316 | } |
||
| 317 | 8 | } |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @psalm-assert scalar $value |
||
| 321 | * |
||
| 322 | * @param mixed $value |
||
| 323 | * @param string $message |
||
| 324 | */ |
||
| 325 | 23 | public static function scalar($value, $message = '') |
|
| 326 | { |
||
| 327 | 23 | if (!\is_scalar($value)) { |
|
| 328 | 11 | static::reportInvalidArgument(\sprintf( |
|
| 329 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
| 330 | 11 | static::typeToString($value) |
|
| 331 | )); |
||
| 332 | } |
||
| 333 | 12 | } |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @psalm-assert object $value |
||
| 337 | * |
||
| 338 | * @param mixed $value |
||
| 339 | * @param string $message |
||
| 340 | */ |
||
| 341 | 23 | public static function object($value, $message = '') |
|
| 342 | { |
||
| 343 | 23 | if (!\is_object($value)) { |
|
| 344 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 345 | 15 | $message ?: 'Expected an object. Got: %s', |
|
| 346 | 15 | static::typeToString($value) |
|
| 347 | )); |
||
| 348 | } |
||
| 349 | 8 | } |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @psalm-assert resource $value |
||
| 353 | * |
||
| 354 | * @param mixed $value |
||
| 355 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
| 356 | * @param string $message |
||
| 357 | */ |
||
| 358 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 359 | { |
||
| 360 | 16 | if (!\is_resource($value)) { |
|
| 361 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 362 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
| 363 | 4 | static::typeToString($value) |
|
| 364 | )); |
||
| 365 | } |
||
| 366 | |||
| 367 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
| 368 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 369 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
| 370 | 4 | static::typeToString($value), |
|
| 371 | 4 | $type |
|
| 372 | )); |
||
| 373 | } |
||
| 374 | 8 | } |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @psalm-assert callable $value |
||
| 378 | * |
||
| 379 | * @param mixed $value |
||
| 380 | * @param string $message |
||
| 381 | */ |
||
| 382 | 20 | public static function isCallable($value, $message = '') |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @psalm-assert array $value |
||
| 394 | * |
||
| 395 | * @param mixed $value |
||
| 396 | * @param string $message |
||
| 397 | */ |
||
| 398 | 20 | public static function isArray($value, $message = '') |
|
| 399 | { |
||
| 400 | 20 | if (!\is_array($value)) { |
|
| 401 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 402 | 12 | $message ?: 'Expected an array. Got: %s', |
|
| 403 | 12 | static::typeToString($value) |
|
| 404 | )); |
||
| 405 | } |
||
| 406 | 8 | } |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @psalm-assert iterable $value |
||
| 410 | * |
||
| 411 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
| 412 | * |
||
| 413 | * @param mixed $value |
||
| 414 | * @param string $message |
||
| 415 | */ |
||
| 416 | 20 | public static function isTraversable($value, $message = '') |
|
| 417 | { |
||
| 418 | 20 | @\trigger_error( |
|
| 419 | 20 | \sprintf( |
|
| 420 | 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.', |
|
| 421 | 20 | __METHOD__ |
|
| 422 | ), |
||
| 423 | 20 | \E_USER_DEPRECATED |
|
| 424 | ); |
||
| 425 | |||
| 426 | 20 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
| 427 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 428 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
| 429 | 8 | static::typeToString($value) |
|
| 430 | )); |
||
| 431 | } |
||
| 432 | 12 | } |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @param mixed $value |
||
| 436 | * @param string $message |
||
| 437 | */ |
||
| 438 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 439 | { |
||
| 440 | 20 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
|
| 441 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 442 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
| 443 | 8 | static::typeToString($value) |
|
| 444 | )); |
||
| 445 | } |
||
| 446 | 12 | } |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @psalm-assert countable $value |
||
| 450 | * |
||
| 451 | * @param mixed $value |
||
| 452 | * @param string $message |
||
| 453 | */ |
||
| 454 | 24 | View Code Duplication | public static function isCountable($value, $message = '') |
| 455 | { |
||
| 456 | 24 | if (!\is_array($value) && !($value instanceof Countable)) { |
|
| 457 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 458 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
| 459 | 12 | static::typeToString($value) |
|
| 460 | )); |
||
| 461 | } |
||
| 462 | 12 | } |
|
| 463 | |||
| 464 | /** |
||
| 465 | * @psalm-assert iterable $value |
||
| 466 | * |
||
| 467 | * @param mixed $value |
||
| 468 | * @param string $message |
||
| 469 | */ |
||
| 470 | 878 | View Code Duplication | public static function isIterable($value, $message = '') |
| 471 | { |
||
| 472 | 878 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
| 473 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 474 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
| 475 | 8 | static::typeToString($value) |
|
| 476 | )); |
||
| 477 | } |
||
| 478 | 874 | } |
|
| 479 | |||
| 480 | /** |
||
| 481 | * @psalm-template ExpectedType of object |
||
| 482 | * @psalm-param class-string<ExpectedType> $class |
||
| 483 | * @psalm-assert ExpectedType $value |
||
| 484 | * |
||
| 485 | * @param mixed $value |
||
| 486 | * @param string|object $class |
||
| 487 | * @param string $message |
||
| 488 | */ |
||
| 489 | 16 | public static function isInstanceOf($value, $class, $message = '') |
|
| 490 | { |
||
| 491 | 16 | if (!($value instanceof $class)) { |
|
| 492 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 493 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
| 494 | 12 | static::typeToString($value), |
|
| 495 | 12 | $class |
|
| 496 | )); |
||
| 497 | } |
||
| 498 | 4 | } |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @psalm-template ExpectedType of object |
||
| 502 | * @psalm-param class-string<ExpectedType> $class |
||
| 503 | * @psalm-assert !ExpectedType $value |
||
| 504 | * |
||
| 505 | * @param mixed $value |
||
| 506 | * @param string|object $class |
||
| 507 | * @param string $message |
||
| 508 | */ |
||
| 509 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 510 | { |
||
| 511 | 16 | if ($value instanceof $class) { |
|
| 512 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 513 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
| 514 | 4 | static::typeToString($value), |
|
| 515 | 4 | $class |
|
| 516 | )); |
||
| 517 | } |
||
| 518 | 12 | } |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @param mixed $value |
||
| 522 | * @param array<object|string> $classes |
||
| 523 | * @param string $message |
||
| 524 | */ |
||
| 525 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 526 | { |
||
| 527 | 20 | foreach ($classes as $class) { |
|
| 528 | 20 | if ($value instanceof $class) { |
|
| 529 | 8 | return; |
|
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 534 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
| 535 | 12 | static::typeToString($value), |
|
| 536 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
| 537 | )); |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @psalm-assert empty $value |
||
| 542 | * |
||
| 543 | * @param mixed $value |
||
| 544 | * @param string $message |
||
| 545 | */ |
||
| 546 | 23 | public static function isEmpty($value, $message = '') |
|
| 547 | { |
||
| 548 | 23 | if (!empty($value)) { |
|
| 549 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 550 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
| 551 | 8 | static::valueToString($value) |
|
| 552 | )); |
||
| 553 | } |
||
| 554 | 15 | } |
|
| 555 | |||
| 556 | /** |
||
| 557 | * @psalm-assert !empty $value |
||
| 558 | * |
||
| 559 | * @param mixed $value |
||
| 560 | * @param string $message |
||
| 561 | */ |
||
| 562 | 23 | public static function notEmpty($value, $message = '') |
|
| 563 | { |
||
| 564 | 23 | if (empty($value)) { |
|
| 565 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 566 | 15 | $message ?: 'Expected a non-empty value. Got: %s', |
|
| 567 | 15 | static::valueToString($value) |
|
| 568 | )); |
||
| 569 | } |
||
| 570 | 8 | } |
|
| 571 | |||
| 572 | /** |
||
| 573 | * @psalm-assert null $value |
||
| 574 | * |
||
| 575 | * @param mixed $value |
||
| 576 | * @param string $message |
||
| 577 | */ |
||
| 578 | 11 | public static function null($value, $message = '') |
|
| 579 | { |
||
| 580 | 11 | if (null !== $value) { |
|
| 581 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 582 | 8 | $message ?: 'Expected null. Got: %s', |
|
| 583 | 8 | static::valueToString($value) |
|
| 584 | )); |
||
| 585 | } |
||
| 586 | 3 | } |
|
| 587 | |||
| 588 | /** |
||
| 589 | * @psalm-assert !null $value |
||
| 590 | * |
||
| 591 | * @param mixed $value |
||
| 592 | * @param string $message |
||
| 593 | */ |
||
| 594 | 11 | public static function notNull($value, $message = '') |
|
| 595 | { |
||
| 596 | 11 | if (null === $value) { |
|
| 597 | 3 | static::reportInvalidArgument( |
|
| 598 | 3 | $message ?: 'Expected a value other than null.' |
|
| 599 | ); |
||
| 600 | } |
||
| 601 | 8 | } |
|
| 602 | |||
| 603 | /** |
||
| 604 | * @psalm-assert true $value |
||
| 605 | * |
||
| 606 | * @param mixed $value |
||
| 607 | * @param string $message |
||
| 608 | */ |
||
| 609 | 15 | public static function true($value, $message = '') |
|
| 610 | { |
||
| 611 | 15 | if (true !== $value) { |
|
| 612 | 11 | static::reportInvalidArgument(\sprintf( |
|
| 613 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
| 614 | 11 | static::valueToString($value) |
|
| 615 | )); |
||
| 616 | } |
||
| 617 | 4 | } |
|
| 618 | |||
| 619 | /** |
||
| 620 | * @psalm-assert false $value |
||
| 621 | * |
||
| 622 | * @param mixed $value |
||
| 623 | * @param string $message |
||
| 624 | */ |
||
| 625 | 19 | public static function false($value, $message = '') |
|
| 626 | { |
||
| 627 | 19 | if (false !== $value) { |
|
| 628 | 15 | static::reportInvalidArgument(\sprintf( |
|
| 629 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
| 630 | 15 | static::valueToString($value) |
|
| 631 | )); |
||
| 632 | } |
||
| 633 | 4 | } |
|
| 634 | |||
| 635 | /** |
||
| 636 | * @param mixed $value |
||
| 637 | * @param string $message |
||
| 638 | */ |
||
| 639 | 47 | View Code Duplication | public static function ip($value, $message = '') |
| 640 | { |
||
| 641 | 47 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
|
| 642 | 19 | static::reportInvalidArgument(\sprintf( |
|
| 643 | 19 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
| 644 | 19 | static::valueToString($value) |
|
| 645 | )); |
||
| 646 | } |
||
| 647 | 28 | } |
|
| 648 | |||
| 649 | /** |
||
| 650 | * @param mixed $value |
||
| 651 | * @param string $message |
||
| 652 | */ |
||
| 653 | 47 | View Code Duplication | public static function ipv4($value, $message = '') |
| 654 | { |
||
| 655 | 47 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
| 656 | 35 | static::reportInvalidArgument(\sprintf( |
|
| 657 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
| 658 | 35 | static::valueToString($value) |
|
| 659 | )); |
||
| 660 | } |
||
| 661 | 12 | } |
|
| 662 | |||
| 663 | /** |
||
| 664 | * @param mixed $value |
||
| 665 | * @param string $message |
||
| 666 | */ |
||
| 667 | 47 | View Code Duplication | public static function ipv6($value, $message = '') |
| 668 | { |
||
| 669 | 47 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
| 670 | 31 | static::reportInvalidArgument(\sprintf( |
|
| 671 | 31 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
| 672 | 31 | static::valueToString($value) |
|
| 673 | )); |
||
| 674 | } |
||
| 675 | 16 | } |
|
| 676 | |||
| 677 | /** |
||
| 678 | * @param mixed $value |
||
| 679 | * @param string $message |
||
| 680 | */ |
||
| 681 | 16 | View Code Duplication | public static function email($value, $message = '') |
| 682 | { |
||
| 683 | 16 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
| 684 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 685 | 12 | $message ?: 'Expected a value to be a valid e-mail address. Got %s', |
|
| 686 | 12 | static::valueToString($value) |
|
| 687 | )); |
||
| 688 | } |
||
| 689 | 4 | } |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
| 693 | * |
||
| 694 | * @param array $values |
||
| 695 | * @param string $message |
||
| 696 | */ |
||
| 697 | 12 | public static function uniqueValues(array $values, $message = '') |
|
| 698 | { |
||
| 699 | 12 | $allValues = \count($values); |
|
| 700 | 12 | $uniqueValues = \count(\array_unique($values)); |
|
| 701 | |||
| 702 | 12 | if ($allValues !== $uniqueValues) { |
|
| 703 | 8 | $difference = $allValues - $uniqueValues; |
|
| 704 | |||
| 705 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 706 | 8 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
|
| 707 | 8 | $difference, |
|
| 708 | 8 | (1 === $difference ? 'is' : 'are') |
|
| 709 | )); |
||
| 710 | } |
||
| 711 | 4 | } |
|
| 712 | |||
| 713 | /** |
||
| 714 | * @param mixed $value |
||
| 715 | * @param mixed $expect |
||
| 716 | * @param string $message |
||
| 717 | */ |
||
| 718 | 33 | public static function eq($value, $expect, $message = '') |
|
| 719 | { |
||
| 720 | 33 | if ($expect != $value) { |
|
| 721 | 17 | static::reportInvalidArgument(\sprintf( |
|
| 722 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 723 | 17 | static::valueToString($value), |
|
| 724 | 17 | static::valueToString($expect) |
|
| 725 | )); |
||
| 726 | } |
||
| 727 | 16 | } |
|
| 728 | |||
| 729 | /** |
||
| 730 | * @param mixed $value |
||
| 731 | * @param mixed $expect |
||
| 732 | * @param string $message |
||
| 733 | */ |
||
| 734 | 28 | public static function notEq($value, $expect, $message = '') |
|
| 735 | { |
||
| 736 | 28 | if ($expect == $value) { |
|
| 737 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 738 | 16 | $message ?: 'Expected a different value than %s.', |
|
| 739 | 16 | static::valueToString($expect) |
|
| 740 | )); |
||
| 741 | } |
||
| 742 | 12 | } |
|
| 743 | |||
| 744 | /** |
||
| 745 | * @psalm-template ExpectedType |
||
| 746 | * @psalm-param ExpectedType $expect |
||
| 747 | * @psalm-assert =ExpectedType $value |
||
| 748 | * |
||
| 749 | * @param mixed $value |
||
| 750 | * @param mixed $expect |
||
| 751 | * @param string $message |
||
| 752 | */ |
||
| 753 | 16 | public static function same($value, $expect, $message = '') |
|
| 754 | { |
||
| 755 | 16 | if ($expect !== $value) { |
|
| 756 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 757 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 758 | 12 | static::valueToString($value), |
|
| 759 | 12 | static::valueToString($expect) |
|
| 760 | )); |
||
| 761 | } |
||
| 762 | 4 | } |
|
| 763 | |||
| 764 | /** |
||
| 765 | * @param mixed $value |
||
| 766 | * @param mixed $expect |
||
| 767 | * @param string $message |
||
| 768 | */ |
||
| 769 | 16 | public static function notSame($value, $expect, $message = '') |
|
| 770 | { |
||
| 771 | 16 | if ($expect === $value) { |
|
| 772 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 773 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
| 774 | 4 | static::valueToString($expect) |
|
| 775 | )); |
||
| 776 | } |
||
| 777 | 12 | } |
|
| 778 | |||
| 779 | /** |
||
| 780 | * @param mixed $value |
||
| 781 | * @param mixed $limit |
||
| 782 | * @param string $message |
||
| 783 | */ |
||
| 784 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 785 | { |
||
| 786 | 8 | if ($value <= $limit) { |
|
| 787 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 788 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 789 | 4 | static::valueToString($value), |
|
| 790 | 4 | static::valueToString($limit) |
|
| 791 | )); |
||
| 792 | } |
||
| 793 | 4 | } |
|
| 794 | |||
| 795 | /** |
||
| 796 | * @param mixed $value |
||
| 797 | * @param mixed $limit |
||
| 798 | * @param string $message |
||
| 799 | */ |
||
| 800 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 801 | { |
||
| 802 | 12 | if ($value < $limit) { |
|
| 803 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 804 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
| 805 | 4 | static::valueToString($value), |
|
| 806 | 4 | static::valueToString($limit) |
|
| 807 | )); |
||
| 808 | } |
||
| 809 | 8 | } |
|
| 810 | |||
| 811 | /** |
||
| 812 | * @param mixed $value |
||
| 813 | * @param mixed $limit |
||
| 814 | * @param string $message |
||
| 815 | */ |
||
| 816 | 8 | public static function lessThan($value, $limit, $message = '') |
|
| 817 | { |
||
| 818 | 8 | if ($value >= $limit) { |
|
| 819 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 820 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
| 821 | 4 | static::valueToString($value), |
|
| 822 | 4 | static::valueToString($limit) |
|
| 823 | )); |
||
| 824 | } |
||
| 825 | 4 | } |
|
| 826 | |||
| 827 | /** |
||
| 828 | * @param mixed $value |
||
| 829 | * @param mixed $limit |
||
| 830 | * @param string $message |
||
| 831 | */ |
||
| 832 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 833 | { |
||
| 834 | 12 | if ($value > $limit) { |
|
| 835 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 836 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
| 837 | 4 | static::valueToString($value), |
|
| 838 | 4 | static::valueToString($limit) |
|
| 839 | )); |
||
| 840 | } |
||
| 841 | 8 | } |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
| 845 | * |
||
| 846 | * @param mixed $value |
||
| 847 | * @param mixed min |
||
| 848 | * @param mixed max |
||
| 849 | * @param string $message |
||
| 850 | */ |
||
| 851 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 852 | { |
||
| 853 | 16 | if ($value < $min || $value > $max) { |
|
| 854 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 855 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
| 856 | 8 | static::valueToString($value), |
|
| 857 | 8 | static::valueToString($min), |
|
| 858 | 8 | static::valueToString($max) |
|
| 859 | )); |
||
| 860 | } |
||
| 861 | 8 | } |
|
| 862 | |||
| 863 | /** |
||
| 864 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
| 865 | * |
||
| 866 | * @psalm-template ExpectedType |
||
| 867 | * @psalm-param array<ExpectedType> $values |
||
| 868 | * @psalm-assert ExpectedType $value |
||
| 869 | * |
||
| 870 | * @param mixed $value |
||
| 871 | * @param array $values |
||
| 872 | * @param string $message |
||
| 873 | */ |
||
| 874 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 875 | { |
||
| 876 | 8 | if (!\in_array($value, $values, true)) { |
|
| 877 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 878 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
| 879 | 4 | static::valueToString($value), |
|
| 880 | 4 | \implode(', ', \array_map(array('static', 'valueToString'), $values)) |
|
| 881 | )); |
||
| 882 | } |
||
| 883 | 4 | } |
|
| 884 | |||
| 885 | /** |
||
| 886 | * @param mixed $value |
||
| 887 | * @param string $subString |
||
| 888 | * @param string $message |
||
| 889 | */ |
||
| 890 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 891 | { |
||
| 892 | 80 | if (false === \strpos($value, $subString)) { |
|
| 893 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 894 | 32 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
| 895 | 32 | static::valueToString($value), |
|
| 896 | 32 | static::valueToString($subString) |
|
| 897 | )); |
||
| 898 | } |
||
| 899 | 48 | } |
|
| 900 | |||
| 901 | /** |
||
| 902 | * @param mixed $value |
||
| 903 | * @param string $subString |
||
| 904 | * @param string $message |
||
| 905 | */ |
||
| 906 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 907 | { |
||
| 908 | 80 | if (false !== \strpos($value, $subString)) { |
|
| 909 | 48 | static::reportInvalidArgument(\sprintf( |
|
| 910 | 48 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
|
| 911 | 48 | static::valueToString($value), |
|
| 912 | 48 | static::valueToString($subString) |
|
| 913 | )); |
||
| 914 | } |
||
| 915 | 32 | } |
|
| 916 | |||
| 917 | /** |
||
| 918 | * @param mixed $value |
||
| 919 | * @param string $message |
||
| 920 | */ |
||
| 921 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 922 | { |
||
| 923 | 40 | if (\preg_match('/^\s*$/', $value)) { |
|
| 924 | 24 | static::reportInvalidArgument(\sprintf( |
|
| 925 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
| 926 | 24 | static::valueToString($value) |
|
| 927 | )); |
||
| 928 | } |
||
| 929 | 16 | } |
|
| 930 | |||
| 931 | /** |
||
| 932 | * @param mixed $value |
||
| 933 | * @param string $prefix |
||
| 934 | * @param string $message |
||
| 935 | */ |
||
| 936 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 937 | { |
||
| 938 | 48 | if (0 !== \strpos($value, $prefix)) { |
|
| 939 | 32 | static::reportInvalidArgument(\sprintf( |
|
| 940 | 32 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
| 941 | 32 | static::valueToString($value), |
|
| 942 | 32 | static::valueToString($prefix) |
|
| 943 | )); |
||
| 944 | } |
||
| 945 | 16 | } |
|
| 946 | |||
| 947 | /** |
||
| 948 | * @param mixed $value |
||
| 949 | * @param string $message |
||
| 950 | */ |
||
| 951 | 24 | public static function startsWithLetter($value, $message = '') |
|
| 952 | { |
||
| 953 | 24 | $valid = isset($value[0]); |
|
| 954 | |||
| 955 | 24 | if ($valid) { |
|
| 956 | 20 | $locale = \setlocale(LC_CTYPE, 0); |
|
| 957 | 20 | \setlocale(LC_CTYPE, 'C'); |
|
| 958 | 20 | $valid = \ctype_alpha($value[0]); |
|
| 959 | 20 | \setlocale(LC_CTYPE, $locale); |
|
| 960 | } |
||
| 961 | |||
| 962 | 24 | if (!$valid) { |
|
| 963 | 12 | static::reportInvalidArgument(\sprintf( |
|
| 964 | 12 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
| 965 | 12 | static::valueToString($value) |
|
| 966 | )); |
||
| 967 | } |
||
| 968 | 12 | } |
|
| 969 | |||
| 970 | /** |
||
| 971 | * @param mixed $value |
||
| 972 | * @param string $suffix |
||
| 973 | * @param string $message |
||
| 974 | */ |
||
| 975 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 985 | |||
| 986 | /** |
||
| 987 | * @param mixed $value |
||
| 988 | * @param mixed $pattern |
||
| 989 | * @param string $message |
||
| 990 | */ |
||
| 991 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 992 | { |
||
| 993 | 12 | if (!\preg_match($pattern, $value)) { |
|
| 994 | 8 | static::reportInvalidArgument(\sprintf( |
|
| 995 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
| 996 | 8 | static::valueToString($value) |
|
| 997 | )); |
||
| 998 | } |
||
| 999 | 4 | } |
|
| 1000 | |||
| 1001 | /** |
||
| 1002 | * @param mixed $value |
||
| 1003 | * @param mixed $pattern |
||
| 1004 | * @param string $message |
||
| 1005 | */ |
||
| 1006 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
| 1007 | { |
||
| 1008 | 12 | if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
|
| 1009 | 4 | static::reportInvalidArgument(\sprintf( |
|
| 1010 | 4 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
|
| 1011 | 4 | static::valueToString($value), |
|
| 1012 | 4 | static::valueToString($pattern), |
|
| 1013 | 4 | $matches[0][1] |
|
| 1014 | )); |
||
| 1015 | } |
||
| 1016 | 8 | } |
|
| 1017 | |||
| 1018 | /** |
||
| 1019 | * @psalm-assert !numeric $value |
||
| 1020 | * |
||
| 1021 | * @param mixed $value |
||
| 1022 | * @param string $message |
||
| 1023 | */ |
||
| 1024 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
| 1025 | { |
||
| 1026 | 28 | static::string($value); |
|
| 1027 | |||
| 1028 | 28 | if (!\preg_match('/^\p{L}+$/u', $value)) { |
|
| 1029 | 16 | static::reportInvalidArgument(\sprintf( |
|
| 1030 | 16 | $message ?: 'Expected a value to contain only Unicode letters. Got: %s', |
|
| 1031 | 16 | static::valueToString($value) |
|
| 1035 | |||
| 1036 | /** |
||
| 1037 | * @param mixed $value |
||
| 1038 | * @param string $message |
||
| 1039 | */ |
||
| 1040 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
| 1054 | |||
| 1055 | /** |
||
| 1056 | * @param mixed $value |
||
| 1057 | * @param string $message |
||
| 1058 | */ |
||
| 1059 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 1073 | |||
| 1074 | /** |
||
| 1075 | * @param mixed $value |
||
| 1076 | * @param string $message |
||
| 1077 | */ |
||
| 1078 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 1092 | |||
| 1093 | /** |
||
| 1094 | * @param mixed $value |
||
| 1095 | * @param string $message |
||
| 1096 | */ |
||
| 1097 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 1111 | |||
| 1112 | /** |
||
| 1113 | * @param mixed $value |
||
| 1114 | * @param string $message |
||
| 1115 | */ |
||
| 1116 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 1130 | |||
| 1131 | /** |
||
| 1132 | * @param mixed $value |
||
| 1133 | * @param mixed $length |
||
| 1134 | * @param string $message |
||
| 1135 | */ |
||
| 1136 | 36 | public static function length($value, $length, $message = '') |
|
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Inclusive min. |
||
| 1149 | * |
||
| 1150 | * @param mixed $value |
||
| 1151 | * @param mixed $min |
||
| 1152 | * @param string $message |
||
| 1153 | */ |
||
| 1154 | 36 | public static function minLength($value, $min, $message = '') |
|
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Inclusive max. |
||
| 1167 | * |
||
| 1168 | * @param mixed $value |
||
| 1169 | * @param mixed $max |
||
| 1170 | * @param string $message |
||
| 1171 | */ |
||
| 1172 | 36 | public static function maxLength($value, $max, $message = '') |
|
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
| 1185 | * |
||
| 1186 | * @param mixed $value |
||
| 1187 | * @param mixed $min |
||
| 1188 | * @param mixed $max |
||
| 1189 | * @param string $message |
||
| 1190 | */ |
||
| 1191 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
| 1207 | * |
||
| 1208 | * @param mixed $value |
||
| 1209 | * @param string $message |
||
| 1210 | */ |
||
| 1211 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
| 1222 | |||
| 1223 | /** |
||
| 1224 | * @param mixed $value |
||
| 1225 | * @param string $message |
||
| 1226 | */ |
||
| 1227 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 1238 | |||
| 1239 | /** |
||
| 1240 | * @param mixed $value |
||
| 1241 | * @param string $message |
||
| 1242 | */ |
||
| 1243 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 1254 | |||
| 1255 | /** |
||
| 1256 | * @param mixed $value |
||
| 1257 | * @param string $message |
||
| 1258 | */ |
||
| 1259 | public static function readable($value, $message = '') |
||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * @param mixed $value |
||
| 1271 | * @param string $message |
||
| 1272 | */ |
||
| 1273 | public static function writable($value, $message = '') |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * @psalm-assert class-string $value |
||
| 1285 | * |
||
| 1286 | * @param mixed $value |
||
| 1287 | * @param string $message |
||
| 1288 | */ |
||
| 1289 | 8 | public static function classExists($value, $message = '') |
|
| 1298 | |||
| 1299 | /** |
||
| 1300 | * @param mixed $value |
||
| 1301 | * @param string|object $class |
||
| 1302 | * @param string $message |
||
| 1303 | */ |
||
| 1304 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 1314 | |||
| 1315 | /** |
||
| 1316 | * @psalm-assert class-string $value |
||
| 1317 | * |
||
| 1318 | * @param mixed $value |
||
| 1319 | * @param string $message |
||
| 1320 | */ |
||
| 1321 | 8 | public static function interfaceExists($value, $message = '') |
|
| 1330 | |||
| 1331 | /** |
||
| 1332 | * @param mixed $value |
||
| 1333 | * @param mixed $interface |
||
| 1334 | * @param string $message |
||
| 1335 | */ |
||
| 1336 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 1346 | |||
| 1347 | /** |
||
| 1348 | * @param string|object $classOrObject |
||
| 1349 | * @param mixed $property |
||
| 1350 | * @param string $message |
||
| 1351 | */ |
||
| 1352 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 1361 | |||
| 1362 | /** |
||
| 1363 | * @param string|object $classOrObject |
||
| 1364 | * @param mixed $property |
||
| 1365 | * @param string $message |
||
| 1366 | */ |
||
| 1367 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 1376 | |||
| 1377 | /** |
||
| 1378 | * @param string|object $classOrObject |
||
| 1379 | * @param mixed $method |
||
| 1380 | * @param string $message |
||
| 1381 | */ |
||
| 1382 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 1391 | |||
| 1392 | /** |
||
| 1393 | * @param string|object $classOrObject |
||
| 1394 | * @param mixed $method |
||
| 1395 | * @param string $message |
||
| 1396 | */ |
||
| 1397 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 1406 | |||
| 1407 | /** |
||
| 1408 | * @param array $array |
||
| 1409 | * @param string|int $key |
||
| 1410 | * @param string $message |
||
| 1411 | */ |
||
| 1412 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 1421 | |||
| 1422 | /** |
||
| 1423 | * @param array $array |
||
| 1424 | * @param string|int $key |
||
| 1425 | * @param string $message |
||
| 1426 | */ |
||
| 1427 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1439 | * |
||
| 1440 | * @param mixed $array |
||
| 1441 | * @param mixed $number |
||
| 1442 | * @param string $message |
||
| 1443 | */ |
||
| 1444 | 8 | public static function count($array, $number, $message = '') |
|
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1455 | * |
||
| 1456 | * @param mixed $array |
||
| 1457 | * @param mixed $min |
||
| 1458 | * @param string $message |
||
| 1459 | */ |
||
| 1460 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 1470 | |||
| 1471 | /** |
||
| 1472 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1473 | * |
||
| 1474 | * @param mixed $array |
||
| 1475 | * @param mixed $max |
||
| 1476 | * @param string $message |
||
| 1477 | */ |
||
| 1478 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 1488 | |||
| 1489 | /** |
||
| 1490 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
| 1491 | * |
||
| 1492 | * @param mixed $array |
||
| 1493 | * @param mixed $min |
||
| 1494 | * @param mixed $max |
||
| 1495 | * @param string $message |
||
| 1496 | */ |
||
| 1497 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
| 1510 | |||
| 1511 | /** |
||
| 1512 | * @param mixed $array |
||
| 1513 | * @param string $message |
||
| 1514 | */ |
||
| 1515 | 24 | public static function isList($array, $message = '') |
|
| 1523 | |||
| 1524 | /** |
||
| 1525 | * @param mixed $array |
||
| 1526 | * @param string $message |
||
| 1527 | */ |
||
| 1528 | 16 | public static function isMap($array, $message = '') |
|
| 1542 | |||
| 1543 | /** |
||
| 1544 | * @param mixed $value |
||
| 1545 | * @param string $message |
||
| 1546 | */ |
||
| 1547 | 56 | public static function uuid($value, $message = '') |
|
| 1564 | |||
| 1565 | /** |
||
| 1566 | * @param Closure $expression |
||
| 1567 | * @param string|object $class |
||
| 1568 | * @param string $message |
||
| 1569 | */ |
||
| 1570 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 1596 | |||
| 1597 | 1383 | public static function __callStatic($name, $arguments) |
|
| 1625 | |||
| 1626 | /** |
||
| 1627 | * @param mixed $value |
||
| 1628 | * |
||
| 1629 | * @return string |
||
| 1630 | */ |
||
| 1631 | 694 | protected static function valueToString($value) |
|
| 1667 | |||
| 1668 | /** |
||
| 1669 | * @param mixed $value |
||
| 1670 | * |
||
| 1671 | * @return string |
||
| 1672 | */ |
||
| 1673 | 169 | protected static function typeToString($value) |
|
| 1677 | |||
| 1678 | 168 | protected static function strlen($value) |
|
| 1690 | |||
| 1691 | /** |
||
| 1692 | * @param string $message |
||
| 1693 | */ |
||
| 1694 | 918 | protected static function reportInvalidArgument($message) |
|
| 1698 | |||
| 1699 | private function __construct() |
||
| 1702 | } |
||
| 1703 |