-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from veewee/support-dynamic-object
Support dynamic objects and class attributes
- Loading branch information
Showing
17 changed files
with
366 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Reflect; | ||
|
||
use ReflectionAttribute; | ||
use Throwable; | ||
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException; | ||
use function Psl\Result\wrap; | ||
use function Psl\Vec\map; | ||
|
||
/** | ||
* @template T extends object | ||
* | ||
* @param class-string<T>|null $attributeClassName | ||
* @return (T is null ? list<object> : list<T>) | ||
* | ||
* @throws UnreflectableException | ||
*/ | ||
function object_attributes(object $object, ?string $attributeClassName = null): array | ||
{ | ||
$propertyInfo = reflect_object($object); | ||
|
||
return map( | ||
$propertyInfo->getAttributes($attributeClassName, ReflectionAttribute::IS_INSTANCEOF), | ||
static fn (ReflectionAttribute $attribute): object => wrap(static fn () => $attribute->newInstance()) | ||
->catch( | ||
static fn (Throwable $error) => throw UnreflectableException::nonInstantiatable( | ||
$attribute->getName(), | ||
$error | ||
) | ||
) | ||
->getResult() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Reflect; | ||
|
||
use ReflectionAttribute; | ||
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException; | ||
|
||
/** | ||
* @throws UnreflectableException | ||
* | ||
* @param class-string $attributeClassName | ||
*/ | ||
function object_has_attribute(object $object, string $attributeClassName): bool | ||
{ | ||
$propertyInfo = reflect_object($object); | ||
|
||
return (bool) $propertyInfo->getAttributes($attributeClassName, ReflectionAttribute::IS_INSTANCEOF); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Reflect; | ||
|
||
use AllowDynamicProperties; | ||
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @throws UnreflectableException | ||
*/ | ||
function object_is_dynamic(object $object): bool | ||
{ | ||
// Dynamic props is a 80200 feature. | ||
// IN previous versions, all objects are dynamic (without any warning). | ||
if (PHP_VERSION_ID < 80200) { | ||
return true; | ||
} | ||
|
||
return object_has_attribute($object, AllowDynamicProperties::class); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\TestFixtures; | ||
|
||
#[Attribute(Attribute::TARGET_ALL)] | ||
abstract class AbstractAttribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\TestFixtures; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_ALL)] | ||
final class CustomAttribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\TestFixtures; | ||
|
||
use AllowDynamicProperties; | ||
|
||
#[AllowDynamicProperties] | ||
final class Dynamic | ||
{ | ||
public string $x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\TestFixtures; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_ALL)] | ||
final class InheritedCustomAttribute extends AbstractAttribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\SaTests\Reflect; | ||
|
||
use AllowDynamicProperties; | ||
use stdClass; | ||
use function VeeWee\Reflecta\Reflect\object_attributes; | ||
|
||
/** | ||
* @return list<object> | ||
*/ | ||
function test_get_all(): array | ||
{ | ||
$std = new stdClass(); | ||
|
||
return object_attributes($std); | ||
} | ||
|
||
/** | ||
* @return list<AllowDynamicProperties> | ||
*/ | ||
function test_get_specific(): array | ||
{ | ||
$std = new stdClass(); | ||
|
||
return object_attributes($std, AllowDynamicProperties::class); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\UnitTests\Reflect; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ThisIsAnUnknownAttribute; | ||
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException; | ||
use VeeWee\Reflecta\TestFixtures\AbstractAttribute; | ||
use VeeWee\Reflecta\TestFixtures\CustomAttribute; | ||
use VeeWee\Reflecta\TestFixtures\InheritedCustomAttribute; | ||
use function VeeWee\Reflecta\Reflect\object_attributes; | ||
|
||
final class ObjectAttributesTest extends TestCase | ||
{ | ||
public function test_it_can_get_attributes(): void | ||
{ | ||
$x = new #[InheritedCustomAttribute, CustomAttribute] class {}; | ||
|
||
$actual = object_attributes($x); | ||
|
||
static::assertCount(2, $actual); | ||
static::assertInstanceOf(InheritedCustomAttribute::class, $actual[0]); | ||
static::assertInstanceOf(CustomAttribute::class, $actual[1]); | ||
} | ||
|
||
public function test_it_can_get_attributes_of_type(): void | ||
{ | ||
$x = new #[InheritedCustomAttribute, CustomAttribute] class {}; | ||
|
||
$actual = object_attributes($x, InheritedCustomAttribute::class); | ||
|
||
static::assertCount(1, $actual); | ||
static::assertInstanceOf(InheritedCustomAttribute::class, $actual[0]); | ||
} | ||
|
||
public function test_it_can_get_attributes_of_subtype(): void | ||
{ | ||
$x = new #[InheritedCustomAttribute] class {}; | ||
|
||
$actual = object_attributes($x, AbstractAttribute::class); | ||
|
||
static::assertCount(1, $actual); | ||
static::assertInstanceOf(InheritedCustomAttribute::class, $actual[0]); | ||
} | ||
|
||
public function test_it_can_fail_on_attribute_instantiation(): void | ||
{ | ||
$x = new #[ThisIsAnUnknownAttribute] class {}; | ||
|
||
$this->expectException(UnreflectableException::class); | ||
$this->expectExceptionMessage('Unable to instantiate class ThisIsAnUnknownAttribute.'); | ||
|
||
object_attributes($x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\UnitTests\Reflect; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Reflecta\TestFixtures\AbstractAttribute; | ||
use VeeWee\Reflecta\TestFixtures\CustomAttribute; | ||
use VeeWee\Reflecta\TestFixtures\InheritedCustomAttribute; | ||
use function VeeWee\Reflecta\Reflect\object_has_attribute; | ||
|
||
final class ObjectHasAttributeTest extends TestCase | ||
{ | ||
public function test_it_can_check_for_attribute(): void | ||
{ | ||
$x = new #[CustomAttribute] class {}; | ||
|
||
static::assertTrue(object_has_attribute($x, CustomAttribute::class)); | ||
static::assertFalse(object_has_attribute($x, InheritedCustomAttribute::class)); | ||
} | ||
|
||
public function test_it_can_check_for_attributes_of_subtype(): void | ||
{ | ||
$x = new #[InheritedCustomAttribute] class {}; | ||
|
||
static::assertTrue(object_has_attribute($x, AbstractAttribute::class)); | ||
static::assertTrue(object_has_attribute($x, InheritedCustomAttribute::class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\UnitTests\Reflect; | ||
|
||
use AllowDynamicProperties; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use function VeeWee\Reflecta\Reflect\object_is_dynamic; | ||
use const PHP_VERSION_ID; | ||
|
||
final class ObjectIsDynamicTest extends TestCase | ||
{ | ||
public function test_it_can_check_for_dynamic_objects(): void | ||
{ | ||
if (PHP_VERSION_ID < 80200) { | ||
static::markTestSkipped('On PHP 8.2, all classes are safely dynamic'); | ||
} | ||
|
||
$x = new #[AllowDynamicProperties] class {}; | ||
$y = new class {}; | ||
$s = new stdClass(); | ||
|
||
static::assertTrue(object_is_dynamic($x)); | ||
static::assertFalse(object_is_dynamic($y)); | ||
static::assertTrue(object_is_dynamic($s)); | ||
} | ||
|
||
public function test_it_can_check_for_dynamic_objects_in_php_81(): void | ||
{ | ||
if (PHP_VERSION_ID >= 80200) { | ||
static::markTestSkipped('On PHP 8.2, all classes are safely dynamic'); | ||
} | ||
|
||
$x = new #[AllowDynamicProperties] class {}; | ||
$y = new class {}; | ||
$s = new stdClass(); | ||
|
||
static::assertTrue(object_is_dynamic($x)); | ||
static::assertTrue(object_is_dynamic($y)); | ||
static::assertTrue(object_is_dynamic($s)); | ||
} | ||
} |
Oops, something went wrong.