InvalidArgumentException.php
TLDR
The file InvalidArgumentException.php
is a part of the Illuminate\Testing\Exceptions
namespace. It contains a single class InvalidArgumentException
that extends the PHPUnit\Framework\Exception
class. This class is used to create a new exception for invalid arguments. The create
method within this class is responsible for creating the exception.
Methods
create
The create
method is a static method that creates a new exception for an invalid argument. It takes two parameters: $argument
(an integer) and $type
(a string). It returns a new instance of the InvalidArgumentException
class.
The method performs the following steps:
- It retrieves the current backtrace using
debug_backtrace()
. - It extracts the name of the function or method that called the
create
method from the backtrace. - If the calling function is a method within a class, it formats the function name in the format
ClassName::methodName
. - It constructs the error message by using the argument index, the calling function name, and the type of the argument.
- It creates a new instance of the
InvalidArgumentException
class by passing the constructed error message as the argument.
Class
InvalidArgumentException
The InvalidArgumentException
class extends the PHPUnit\Framework\Exception
class. It is used to create a new exception for an invalid argument. The class has a single static method create
, which is responsible for creating the exception.
<?php
namespace Illuminate\Testing\Exceptions;
use PHPUnit\Framework\Exception;
class InvalidArgumentException extends Exception
{
/**
* Creates a new exception for an invalid argument.
*
* @return static
*/
public static function create(int $argument, string $type): static
{
$stack = debug_backtrace();
$function = $stack[1]['function'];
if (isset($stack[1]['class'])) {
$function = sprintf('%s::%s', $stack[1]['class'], $stack[1]['function']);
}
return new static(
sprintf(
'Argument #%d of %s() must be %s %s',
$argument,
$function,
in_array(lcfirst($type)[0], ['a', 'e', 'i', 'o', 'u'], true) ? 'an' : 'a',
$type
)
);
}
}