Use sprintf() for error messages

This commit is contained in:
Sebastian Meyer 2023-11-18 18:50:14 +01:00
parent 7456e56266
commit a65299546c
6 changed files with 67 additions and 13 deletions

View File

@ -78,7 +78,12 @@ class StrictList extends SplDoublyLinkedList
public function add(int $index, mixed $item): void
{
if (!$this->isAllowedType($item)) {
throw new InvalidArgumentException('Parameter 2 must be an allowed type, ' . get_debug_type($item) . ' given.');
throw new InvalidArgumentException(
sprintf(
'Parameter 2 must be an allowed type, %s given.',
get_debug_type($item)
)
);
}
parent::add($index, $item);
}
@ -96,7 +101,13 @@ class StrictList extends SplDoublyLinkedList
{
foreach ($items as $count => $item) {
if (!$this->isAllowedType($item)) {
throw new InvalidArgumentException('Parameter ' . $count + 1 . ' must be an allowed type, ' . get_debug_type($item) . ' given.');
throw new InvalidArgumentException(
sprintf(
'Parameter %d must be an allowed type, %s given.',
$count + 1,
get_debug_type($item)
)
);
}
}
foreach ($items as $item) {
@ -128,10 +139,10 @@ class StrictList extends SplDoublyLinkedList
}
foreach ($this->allowedTypes as $type) {
$function = 'is_' . $type;
$fqcn = '\\' . ltrim($type, '\\');
if (function_exists($function) && $function($item)) {
return true;
}
$fqcn = '\\' . ltrim($type, '\\');
if (is_object($item) && is_a($item, $fqcn)) {
return true;
}
@ -164,7 +175,12 @@ class StrictList extends SplDoublyLinkedList
public function offsetSet(mixed $index, mixed $item): void
{
if (!$this->isAllowedType($item)) {
throw new InvalidArgumentException('Parameter 2 must be an allowed type, ' . get_debug_type($item) . ' given.');
throw new InvalidArgumentException(
sprintf(
'Parameter 2 must be an allowed type, %s given.',
get_debug_type($item)
)
);
}
parent::offsetSet($index, $item);
}
@ -182,7 +198,13 @@ class StrictList extends SplDoublyLinkedList
{
foreach ($items as $count => $item) {
if (!$this->isAllowedType($item)) {
throw new InvalidArgumentException('Parameter ' . $count + 1 . ' must be an allowed type, ' . get_debug_type($item) . ' given.');
throw new InvalidArgumentException(
sprintf(
'Parameter %d must be an allowed type, %s given.',
$count + 1,
get_debug_type($item)
)
);
}
}
foreach ($items as $item) {
@ -203,7 +225,12 @@ class StrictList extends SplDoublyLinkedList
public function push(mixed $item): void
{
if (!$this->isAllowedType($item)) {
throw new InvalidArgumentException('Parameter 1 must be an allowed type, ' . get_debug_type($item) . ' given.');
throw new InvalidArgumentException(
sprintf(
'Parameter 1 must be an allowed type, %s given.',
get_debug_type($item)
)
);
}
parent::push($item);
}
@ -245,7 +272,12 @@ class StrictList extends SplDoublyLinkedList
public function unshift(mixed $item): void
{
if (!$this->isAllowedType($item)) {
throw new InvalidArgumentException('Parameter 1 must be an allowed type, ' . get_debug_type($item) . ' given.');
throw new InvalidArgumentException(
sprintf(
'Parameter 1 must be an allowed type, %s given.',
get_debug_type($item)
)
);
}
parent::unshift($item);
}

View File

@ -50,7 +50,12 @@ class StrictQueue extends StrictList
final public function setIteratorMode(int $mode): int
{
if ($mode > 1) {
throw new RuntimeException('Changing the iterator direction of ' . static::class . ' is prohibited.');
throw new RuntimeException(
sprintf(
'Changing the iterator direction of %s is prohibited.',
static::class
)
);
}
return parent::setIteratorMode($mode);
}

View File

@ -50,7 +50,12 @@ class StrictStack extends StrictList
final public function setIteratorMode(int $mode): int
{
if ($mode < 2) {
throw new RuntimeException('Changing the iterator direction of ' . static::class . ' is prohibited.');
throw new RuntimeException(
sprintf(
'Changing the iterator direction of %s is prohibited.',
static::class
)
);
}
return parent::setIteratorMode($mode);
}

View File

@ -50,7 +50,13 @@ trait Getter
) {
return $this->$method();
} else {
throw new InvalidArgumentException('Invalid property or missing getter method for property: ' . static::class . '->' . $property . '.');
throw new InvalidArgumentException(
sprintf(
'Invalid property or missing getter method for property: %s->%s.',
static::class,
$property
)
);
}
}

View File

@ -51,7 +51,13 @@ trait Setter
) {
$this->$method($value);
} else {
throw new InvalidArgumentException('Invalid property or missing setter method for property: ' . static::class . '->' . $property . '.');
throw new InvalidArgumentException(
sprintf(
'Invalid property or missing setter method for property: %s->%s.',
static::class,
$property
)
);
}
}

View File

@ -66,6 +66,6 @@ trait Singleton
*/
final public function __clone(): void
{
throw new LogicException('Cloning of a Singleton is prohibited.');
throw new LogicException('Cloning a singleton is prohibited.');
}
}