InvalidArgumentException.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Testing\Exception;
  12. use PHPUnit\Framework\Exception;
  13. class InvalidArgumentException extends Exception
  14. {
  15. /**
  16. * Creates a new exception for an invalid argument.
  17. */
  18. public static function create(int $argument, string $type): static
  19. {
  20. $stack = debug_backtrace();
  21. $function = $stack[1]['function'];
  22. if (isset($stack[1]['class'])) {
  23. $function = sprintf('%s::%s', $stack[1]['class'], $stack[1]['function']);
  24. }
  25. return new static(
  26. sprintf(
  27. 'Argument #%d of %s() must be %s %s',
  28. $argument,
  29. $function,
  30. in_array(lcfirst($type)[0], ['a', 'e', 'i', 'o', 'u'], true) ? 'an' : 'a',
  31. $type
  32. )
  33. );
  34. }
  35. }