Skip to content

Commit 34324d9

Browse files
committed
[fix/test] attributes
1. all attribute classes changed from abstract to final with private constructor (PHP 8.5 compatibility) 2. add reflection-based instantiation tests for attribute classes
1 parent eb9f347 commit 34324d9

8 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/Attributes/ArrayOf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class ArrayOf
1717
{
1818
/** @param class-string $class */
19-
public function __construct(
19+
private function __construct(
2020
public string $class
2121
) {}
2222
}

src/Attributes/KeepOnNull.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* Has no effect on its own.
1717
*/
1818
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY)]
19-
abstract class KeepOnNull
19+
final class KeepOnNull
2020
{
21+
private function __construct()
22+
{}
2123
}

src/Attributes/Lax.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* Takes precedence over both ImmutableBase::strict(true) and #[Strict].
1313
*/
1414
#[\Attribute(\Attribute::TARGET_CLASS)]
15-
abstract class Lax
15+
final class Lax
1616
{
17+
private function __construct()
18+
{}
1719
}

src/Attributes/SkipOnNull.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* Can be overridden per-property with #[KeepOnNull].
1313
*/
1414
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY)]
15-
abstract class SkipOnNull
15+
final class SkipOnNull
1616
{
17+
private function __construct()
18+
{}
1719
}

src/Attributes/Spec.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* @example #[Spec('Email must contain exactly one @ symbol')]
1414
*/
1515
#[\Attribute(\Attribute::TARGET_CLASS)]
16-
abstract class Spec
16+
final class Spec
1717
{
18+
private function __construct()
19+
{}
1820
}

src/Attributes/Strict.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* toggle achieves the same effect without requiring an attribute.
1414
*/
1515
#[\Attribute(\Attribute::TARGET_CLASS)]
16-
abstract class Strict
16+
final class Strict
1717
{
18+
private function __construct()
19+
{}
1820
}

src/Attributes/ValidateFromSelf.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* be checked before parent validators execute.
1414
*/
1515
#[\Attribute(\Attribute::TARGET_CLASS)]
16-
abstract class ValidateFromSelf
16+
final class ValidateFromSelf
1717
{
18+
private function __construct()
19+
{}
1820
}

tests/DefaultTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
use org\bovigo\vfs\vfsStreamDirectory;
77
use PHPUnit\Framework\TestCase;
88
use ReallifeKip\ImmutableBase\Attributes\ArrayOf;
9+
use ReallifeKip\ImmutableBase\Attributes\KeepOnNull;
10+
use ReallifeKip\ImmutableBase\Attributes\Lax;
11+
use ReallifeKip\ImmutableBase\Attributes\SkipOnNull;
12+
use ReallifeKip\ImmutableBase\Attributes\Spec;
13+
use ReallifeKip\ImmutableBase\Attributes\Strict;
14+
use ReallifeKip\ImmutableBase\Attributes\ValidateFromSelf;
915
use ReallifeKip\ImmutableBase\CLI\Cacher;
1016
use ReallifeKip\ImmutableBase\Exceptions\DefinitionExceptions\DebugLogDirectoryInvalidException;
1117
use ReallifeKip\ImmutableBase\Exceptions\DefinitionExceptions\InvalidArrayOfTargetException;
@@ -20,6 +26,7 @@
2026
use ReallifeKip\ImmutableBase\Exceptions\ValidationExceptions\StrictViolationException;
2127
use ReallifeKip\ImmutableBase\ImmutableBase;
2228
use ReallifeKip\ImmutableBase\StaticStatus;
29+
use ReflectionClass;
2330
use Tests\DataTransferObjects\DTO;
2431
use Tests\DataTransferObjects\EmptyArrayOfClassDTO;
2532
use Tests\DataTransferObjects\ExtraDTO;
@@ -206,7 +213,6 @@ public function testBasic()
206213
ImmutableBase::strict(true);
207214
LaxDTO::fromArray([]);
208215
ImmutableBase::strict(false);
209-
new ArrayOf('');
210216
$nestedDTO = NestedVO::fromArray([
211217
'nested2' => [
212218
'value' => 'svo',
@@ -216,6 +222,24 @@ public function testBasic()
216222
UnionWithImmutableBaseTypeDTO::fromArray([
217223
'mixed' => DTO::fromArray($this->array),
218224
]);
225+
226+
foreach (
227+
[
228+
ArrayOf::class,
229+
KeepOnNull::class,
230+
Lax::class,
231+
SkipOnNull::class,
232+
Spec::class,
233+
Strict::class,
234+
ValidateFromSelf::class,
235+
] as $class
236+
) {
237+
$ref = new ReflectionClass($class);
238+
$constructor = $ref->getConstructor();
239+
$constructor->setAccessible(true);
240+
$instance = $ref->newInstanceWithoutConstructor();
241+
$constructor->invoke($instance, '');
242+
}
219243
}
220244
public function testBasicWithCache()
221245
{

0 commit comments

Comments
 (0)