-
-
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 #4 from veewee/class-attributes
Split up Reflect into object_ and class_ specific functions
- Loading branch information
Showing
8 changed files
with
262 additions
and
0 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,35 @@ | ||
<?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 $className | ||
* @param class-string<T>|null $attributeClassName | ||
* @return (T is null ? list<object> : list<T>) | ||
* | ||
* @throws UnreflectableException | ||
*/ | ||
function class_attributes(string $className, ?string $attributeClassName = null): array | ||
{ | ||
$propertyInfo = reflect_class($className); | ||
|
||
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,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Reflect; | ||
|
||
use ReflectionAttribute; | ||
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException; | ||
|
||
/** | ||
* @throws UnreflectableException | ||
* | ||
* @param class-string $className | ||
* @param class-string $attributeClassName | ||
*/ | ||
function class_has_attribute(string $className, string $attributeClassName): bool | ||
{ | ||
$propertyInfo = reflect_class($className); | ||
|
||
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,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace VeeWee\Reflecta\Reflect; | ||
|
||
use AllowDynamicProperties; | ||
use VeeWee\Reflecta\Reflect\Exception\UnreflectableException; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @throws UnreflectableException | ||
* | ||
* @param class-string $className | ||
*/ | ||
function class_is_dynamic(string $className): 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 class_has_attribute($className, 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
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\class_attributes; | ||
|
||
final class ClassAttributesTest extends TestCase | ||
{ | ||
public function test_it_can_get_attributes(): void | ||
{ | ||
$x = new #[InheritedCustomAttribute, CustomAttribute] class {}; | ||
|
||
$actual = class_attributes(get_class($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 = class_attributes(get_class($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 = class_attributes(get_class($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.'); | ||
|
||
class_attributes(get_class($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,31 @@ | ||
<?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\class_has_attribute; | ||
|
||
final class ClassHasAttributeTest extends TestCase | ||
{ | ||
public function test_it_can_check_for_attribute(): void | ||
{ | ||
$x = new #[CustomAttribute] class {}; | ||
$className = get_class($x); | ||
|
||
static::assertTrue(class_has_attribute($className, CustomAttribute::class)); | ||
static::assertFalse(class_has_attribute($className, InheritedCustomAttribute::class)); | ||
} | ||
|
||
public function test_it_can_check_for_attributes_of_subtype(): void | ||
{ | ||
$x = new #[InheritedCustomAttribute] class {}; | ||
$className = get_class($x); | ||
|
||
static::assertTrue(class_has_attribute($className, AbstractAttribute::class)); | ||
static::assertTrue(class_has_attribute($className, 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\class_is_dynamic; | ||
use const PHP_VERSION_ID; | ||
|
||
final class ClassIsDynamicTest 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(class_is_dynamic(get_class($x))); | ||
static::assertFalse(class_is_dynamic(get_class($y))); | ||
static::assertTrue(class_is_dynamic(get_class(($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(class_is_dynamic(get_class($x))); | ||
static::assertTrue(class_is_dynamic(get_class($y))); | ||
static::assertTrue(class_is_dynamic(get_class($s))); | ||
} | ||
} |