From 1feee906354a3a593a670fc9146bbb1b4404cfb8 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Tue, 3 Oct 2023 22:19:32 +0200 Subject: [PATCH] Use named parameters for exceptions --- MIGRATION.md | 2 +- README.md | 2 +- lib/OffsetNotDefined.php | 16 +++++++++------- lib/OffsetNotReadable.php | 16 +++++++++------- lib/OffsetNotWritable.php | 16 +++++++++------- lib/PropertyIsReserved.php | 20 ++++++-------------- lib/PropertyNotDefined.php | 26 ++++++++++++-------------- lib/PropertyNotReadable.php | 26 ++++++++++++-------------- lib/PropertyNotWritable.php | 26 ++++++++++++-------------- phpunit.xml | 13 +++++++------ 10 files changed, 78 insertions(+), 85 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index abae738..acb5486 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -17,7 +17,7 @@ ### Backward Incompatible Changes -None +- Changed the constructors of the following exceptions: `OffsetNotDefined`, `OffsetNotReadable`, `OffsetNotWritable`, `PropertyNotDefined`, `PropertyNotReadable`, and `PropertyNotWritable`. ### Deprecated Features diff --git a/README.md b/README.md index b2c379d..f04ebeb 100755 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ class A return $this->id; } - throw new PropertyNotDefined([ $property, $this ]); + throw new PropertyNotDefined(property: $property, container: $this); } } ``` diff --git a/lib/OffsetNotDefined.php b/lib/OffsetNotDefined.php index d857644..394ffe9 100755 --- a/lib/OffsetNotDefined.php +++ b/lib/OffsetNotDefined.php @@ -22,13 +22,15 @@ class OffsetNotDefined extends LogicException implements OffsetError { /** - * @param string|array{string|int, array|object} $message + * @phpstan-ignore-next-line */ - public function __construct(string|array $message, Throwable $previous = null) - { - if (is_array($message)) { - [ $offset, $container ] = $message + [ 1 => null ]; - + public function __construct( + public readonly string|int $offset, + public readonly array|object|null $container = null, + string $message = null, + Throwable $previous = null + ) { + if (!$message) { if (is_object($container)) { $message = format('Undefined offset %offset for object of class %class.', [ '%offset' => $offset, @@ -46,6 +48,6 @@ public function __construct(string|array $message, Throwable $previous = null) } } - parent::__construct($message, 0, $previous); + parent::__construct($message, previous: $previous); } } diff --git a/lib/OffsetNotReadable.php b/lib/OffsetNotReadable.php index de60e26..3a51d86 100755 --- a/lib/OffsetNotReadable.php +++ b/lib/OffsetNotReadable.php @@ -20,13 +20,15 @@ class OffsetNotReadable extends LogicException implements OffsetError { /** - * @param string|array{string|int, array|object} $message + * @phpstan-ignore-next-line */ - public function __construct(string|array $message, Throwable $previous = null) - { - if (is_array($message)) { - [ $offset, $container ] = $message + [ 1 => null ]; - + public function __construct( + public readonly string|int $offset, + public readonly array|object|null $container = null, + string $message = null, + Throwable $previous = null + ) { + if (!$message) { if (is_object($container)) { $message = format('The offset %offset for object of class %class is not readable.', [ 'offset' => $offset, @@ -44,6 +46,6 @@ public function __construct(string|array $message, Throwable $previous = null) } } - parent::__construct($message, 0, $previous); + parent::__construct($message, previous: $previous); } } diff --git a/lib/OffsetNotWritable.php b/lib/OffsetNotWritable.php index 0688bf3..fb9adec 100755 --- a/lib/OffsetNotWritable.php +++ b/lib/OffsetNotWritable.php @@ -20,13 +20,15 @@ class OffsetNotWritable extends LogicException implements OffsetError { /** - * @param string|array{string|int, array|object} $message + * @phpstan-ignore-next-line */ - public function __construct(string|array $message, Throwable $previous = null) - { - if (is_array($message)) { - [ $offset, $container ] = $message + [ 1 => null ]; - + public function __construct( + public readonly string|int $offset, + public readonly array|object|null $container = null, + string $message = null, + Throwable $previous = null + ) { + if (!$message) { if (is_object($container)) { $message = format('The offset %offset for object of class %class is not writable.', [ 'offset' => $offset, @@ -44,6 +46,6 @@ public function __construct(string|array $message, Throwable $previous = null) } } - parent::__construct($message, 0, $previous); + parent::__construct($message, previous: $previous); } } diff --git a/lib/PropertyIsReserved.php b/lib/PropertyIsReserved.php index 20aa613..2992faf 100755 --- a/lib/PropertyIsReserved.php +++ b/lib/PropertyIsReserved.php @@ -21,21 +21,13 @@ */ class PropertyIsReserved extends LogicException implements PropertyError { - public function __construct(private string $property, Throwable $previous = null) - { + public function __construct( + public readonly string $property, + Throwable $previous = null + ) { parent::__construct( - format('Property %property is reserved.', [ '%property' => $property ]), - 0, - $previous + "Property '$property' is reserved", + previous: $previous ); } - - public function __get(string $property): string - { - if ($property === 'property') { - return $this->property; - } - - throw new PropertyNotDefined([ $property, $this ]); - } } diff --git a/lib/PropertyNotDefined.php b/lib/PropertyNotDefined.php index e6e8993..8f52f36 100755 --- a/lib/PropertyNotDefined.php +++ b/lib/PropertyNotDefined.php @@ -21,20 +21,18 @@ */ class PropertyNotDefined extends LogicException implements PropertyError { - /** - * @phpstan-param string|array{0: string, 1: object} $message - */ - public function __construct(string|array $message, Throwable $previous = null) - { - if (is_array($message)) { - [ $property, $container ] = $message + [ 1 => null ]; + public function __construct( + public readonly string $property, + public readonly object $container, + string $message = null, + Throwable $previous = null + ) { + $message ??= sprintf( + "Undefined property '%s' for object of class '%s'", + $property, + $container::class + ); - $message = format('Undefined property %property for object of class %class.', [ - '%property' => $property, - '%class' => get_class($container) - ]); - } - - parent::__construct($message, 0, $previous); + parent::__construct($message, previous: $previous); } } diff --git a/lib/PropertyNotReadable.php b/lib/PropertyNotReadable.php index e86f878..13b7a85 100755 --- a/lib/PropertyNotReadable.php +++ b/lib/PropertyNotReadable.php @@ -21,20 +21,18 @@ */ class PropertyNotReadable extends LogicException implements PropertyError { - /** - * @phpstan-param string|array{0: string, 1: object} $message - */ - public function __construct(string|array $message, Throwable $previous = null) - { - if (is_array($message)) { - [ $property, $container ] = $message + [ 1 => null ]; + public function __construct( + public readonly string $property, + public readonly object $container, + string $message = null, + Throwable $previous = null + ) { + $message ??= sprintf( + "The property '%s' for object of class '%s is not readable.", + $property, + $container::class + ); - $message = format('The property %property for object of class %class is not readable.', [ - '%property' => $property, - '%class' => get_class($container) - ]); - } - - parent::__construct($message, 0, $previous); + parent::__construct($message, previous: $previous); } } diff --git a/lib/PropertyNotWritable.php b/lib/PropertyNotWritable.php index 58aac46..39ee937 100755 --- a/lib/PropertyNotWritable.php +++ b/lib/PropertyNotWritable.php @@ -21,20 +21,18 @@ */ class PropertyNotWritable extends LogicException implements PropertyError { - /** - * @phpstan-param string|array{0: string, 1: object} $message - */ - public function __construct(string|array $message, Throwable $previous = null) - { - if (is_array($message)) { - [ $property, $container ] = $message + [ 1 => null ]; + public function __construct( + public readonly string $property, + public readonly object $container, + string $message = null, + Throwable $previous = null + ) { + $message ??= sprintf( + "The property '%s' for object of class '%s' is not writable", + $property, + $container::class, + ); - $message = format('The property %property for object of class %class is not writable.', [ - '%property' => $property, - '%class' => get_class($container) - ]); - } - - parent::__construct($message, 0, $previous); + parent::__construct($message, previous: $previous); } } diff --git a/phpunit.xml b/phpunit.xml index 50aa7b7..9d31fd6 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,6 +1,6 @@ - - - lib - - + tests + + + lib + +