-
-
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.
Remove runtime reflection from FieldRutimeMeta
- Loading branch information
Showing
8 changed files
with
126 additions
and
30 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
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,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\ObjectMapper\Meta\Runtime; | ||
|
||
use Orisai\ObjectMapper\MappedObject; | ||
use ReflectionClass; | ||
use ReflectionProperty; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
final class PhpPropertyMeta | ||
{ | ||
|
||
/** @var class-string<MappedObject> */ | ||
public string $declaringClass; | ||
|
||
public string $name; | ||
|
||
public bool $isPublicSet; | ||
|
||
/** | ||
* @param class-string<MappedObject> $declaringClass | ||
*/ | ||
public function __construct( | ||
string $declaringClass, | ||
string $name, | ||
bool $isPublicSet | ||
) | ||
{ | ||
$this->declaringClass = $declaringClass; | ||
$this->name = $name; | ||
$this->isPublicSet = $isPublicSet; | ||
} | ||
|
||
public static function from(ReflectionProperty $property): self | ||
{ | ||
/** @var ReflectionClass<MappedObject> $class */ | ||
$class = $property->getDeclaringClass(); | ||
|
||
return new self( | ||
$class->getName(), | ||
$property->getName(), | ||
self::isPublicSet($property), | ||
); | ||
} | ||
|
||
private static function isPublicSet(ReflectionProperty $property): bool | ||
{ | ||
return $property->isPublic() | ||
&& (PHP_VERSION_ID < 8_01_00 || !$property->isReadOnly()); | ||
} | ||
|
||
} |
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,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\ObjectMapper\Unit\Meta\Runtime; | ||
|
||
use Generator; | ||
use Orisai\ObjectMapper\MappedObject; | ||
use Orisai\ObjectMapper\Meta\Runtime\PhpPropertyMeta; | ||
use PHPUnit\Framework\TestCase; | ||
use Tests\Orisai\ObjectMapper\Doubles\DefaultsVO; | ||
use Tests\Orisai\ObjectMapper\Doubles\NoDefaultsVO; | ||
|
||
final class PhpPropertyMetaTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @param class-string<MappedObject> $declaringClass | ||
* | ||
* @dataProvider provide | ||
*/ | ||
public function test( | ||
string $declaringClass, | ||
string $name, | ||
bool $isPublicSet | ||
): void | ||
{ | ||
$meta = new PhpPropertyMeta($declaringClass, $name, $isPublicSet); | ||
|
||
self::assertSame($declaringClass, $meta->declaringClass); | ||
self::assertSame($name, $meta->name); | ||
self::assertSame($isPublicSet, $meta->isPublicSet); | ||
} | ||
|
||
public function provide(): Generator | ||
{ | ||
yield [ | ||
NoDefaultsVO::class, | ||
'property', | ||
true, | ||
]; | ||
|
||
yield [ | ||
DefaultsVO::class, | ||
'anotherProperty', | ||
false, | ||
]; | ||
} | ||
|
||
} |
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