Skip to content

Commit

Permalink
Use named parameters for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Oct 3, 2023
1 parent e2fed84 commit 1feee90
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 85 deletions.
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

### Backward Incompatible Changes

None
- Changed the constructors of the following exceptions: `OffsetNotDefined`, `OffsetNotReadable`, `OffsetNotWritable`, `PropertyNotDefined`, `PropertyNotReadable`, and `PropertyNotWritable`.

### Deprecated Features

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class A
return $this->id;
}

throw new PropertyNotDefined([ $property, $this ]);
throw new PropertyNotDefined(property: $property, container: $this);
}
}
```
Expand Down
16 changes: 9 additions & 7 deletions lib/OffsetNotDefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
class OffsetNotDefined extends LogicException implements OffsetError
{
/**
* @param string|array{string|int, array<int|string, mixed>|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,
Expand All @@ -46,6 +48,6 @@ public function __construct(string|array $message, Throwable $previous = null)
}
}

parent::__construct($message, 0, $previous);
parent::__construct($message, previous: $previous);
}
}
16 changes: 9 additions & 7 deletions lib/OffsetNotReadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
class OffsetNotReadable extends LogicException implements OffsetError
{
/**
* @param string|array{string|int, array<int|string, mixed>|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,
Expand All @@ -44,6 +46,6 @@ public function __construct(string|array $message, Throwable $previous = null)
}
}

parent::__construct($message, 0, $previous);
parent::__construct($message, previous: $previous);
}
}
16 changes: 9 additions & 7 deletions lib/OffsetNotWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
class OffsetNotWritable extends LogicException implements OffsetError
{
/**
* @param string|array{string|int, array<int|string, mixed>|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,
Expand All @@ -44,6 +46,6 @@ public function __construct(string|array $message, Throwable $previous = null)
}
}

parent::__construct($message, 0, $previous);
parent::__construct($message, previous: $previous);
}
}
20 changes: 6 additions & 14 deletions lib/PropertyIsReserved.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]);
}
}
26 changes: 12 additions & 14 deletions lib/PropertyNotDefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 12 additions & 14 deletions lib/PropertyNotReadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
26 changes: 12 additions & 14 deletions lib/PropertyNotWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 7 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
Expand All @@ -9,14 +9,15 @@
displayDetailsOnTestsThatTriggerDeprecations="true"
executionOrder="depends,defects"
>
<coverage>
<include>
<directory suffix=".php">lib</directory>
</include>
</coverage>
<coverage/>
<testsuites>
<testsuite name="icanboogie/common">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>lib</directory>
</include>
</source>
</phpunit>

0 comments on commit 1feee90

Please sign in to comment.