From a65299546cd877e43002a9c257b522906a5cfcc1 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Sat, 18 Nov 2023 18:50:14 +0100 Subject: [PATCH] Use sprintf() for error messages --- src/DataStructures/StrictList.php | 48 +++++++++++++++++++++++++----- src/DataStructures/StrictQueue.php | 7 ++++- src/DataStructures/StrictStack.php | 7 ++++- src/Traits/Getter.php | 8 ++++- src/Traits/Setter.php | 8 ++++- src/Traits/Singleton.php | 2 +- 6 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/DataStructures/StrictList.php b/src/DataStructures/StrictList.php index 2634156..1a6ea8b 100644 --- a/src/DataStructures/StrictList.php +++ b/src/DataStructures/StrictList.php @@ -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,8 +101,14 @@ 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) { parent::push($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); } diff --git a/src/DataStructures/StrictQueue.php b/src/DataStructures/StrictQueue.php index 2fef611..f07bb05 100644 --- a/src/DataStructures/StrictQueue.php +++ b/src/DataStructures/StrictQueue.php @@ -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); } diff --git a/src/DataStructures/StrictStack.php b/src/DataStructures/StrictStack.php index ba076be..202dccc 100644 --- a/src/DataStructures/StrictStack.php +++ b/src/DataStructures/StrictStack.php @@ -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); } diff --git a/src/Traits/Getter.php b/src/Traits/Getter.php index dd0a2fa..b4adba5 100644 --- a/src/Traits/Getter.php +++ b/src/Traits/Getter.php @@ -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 + ) + ); } } diff --git a/src/Traits/Setter.php b/src/Traits/Setter.php index 3e0815b..933fc31 100644 --- a/src/Traits/Setter.php +++ b/src/Traits/Setter.php @@ -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 + ) + ); } } diff --git a/src/Traits/Singleton.php b/src/Traits/Singleton.php index 1bbba94..e1cdbb6 100644 --- a/src/Traits/Singleton.php +++ b/src/Traits/Singleton.php @@ -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.'); } }