Skip to content

Commit 5dd3ac5

Browse files
authored
Merge pull request #94 from doctrine/add_typed_no_default_reflection_property_class_usage
Add typed no default reflection property class usage
2 parents be70c01 + f584465 commit 5dd3ac5

File tree

6 files changed

+106
-36
lines changed

6 files changed

+106
-36
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ php:
66
- 7.1
77
- 7.2
88
- 7.3
9-
- 7.4snapshot
9+
- 7.4
1010

1111
cache:
1212
directories:
@@ -23,9 +23,6 @@ script:
2323
- ./vendor/bin/phpunit
2424

2525
jobs:
26-
allow_failures:
27-
- php: 7.4snapshot
28-
2926
include:
3027
- stage: Test
3128
env: DEPENDENCIES=low
@@ -50,6 +47,7 @@ jobs:
5047
script: ./vendor/bin/phpcs
5148

5249
- stage: Code Quality
50+
php: 7.4
5351
env: STATIC_ANALYSIS
5452
install: travis_retry composer install --prefer-dist
5553
script: vendor/bin/phpstan analyze

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"doctrine/cache": "^1.0",
2626
"doctrine/collections": "^1.0",
2727
"doctrine/event-manager": "^1.0",
28-
"doctrine/reflection": "^1.0"
28+
"doctrine/reflection": "^1.1"
2929
},
3030
"require-dev": {
3131
"phpstan/phpstan": "^0.11",
@@ -43,7 +43,8 @@
4343
},
4444
"autoload-dev": {
4545
"psr-4": {
46-
"Doctrine\\Tests\\": "tests/Doctrine/Tests"
46+
"Doctrine\\Tests\\": "tests/Doctrine/Tests",
47+
"Doctrine\\Tests_PHP74\\": "tests/Doctrine/Tests_PHP74"
4748
}
4849
},
4950
"extra": {

composer.lock

Lines changed: 32 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Doctrine/Persistence/Mapping/RuntimeReflectionService.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33
namespace Doctrine\Persistence\Mapping;
44

55
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
6+
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
67
use ReflectionClass;
78
use ReflectionException;
89
use ReflectionMethod;
910
use ReflectionProperty;
11+
use function array_key_exists;
1012
use function class_exists;
1113
use function class_parents;
14+
use function phpversion;
15+
use function version_compare;
1216

1317
/**
1418
* PHP Runtime Reflection Service.
1519
*/
1620
class RuntimeReflectionService implements ReflectionService
1721
{
22+
/** @var bool */
23+
private $supportsTypedPropertiesWorkaround;
24+
25+
public function __construct()
26+
{
27+
$this->supportsTypedPropertiesWorkaround = version_compare((string) phpversion(), '7.4.0') >= 0;
28+
}
29+
1830
/**
1931
* {@inheritDoc}
2032
*/
@@ -64,6 +76,8 @@ public function getAccessibleProperty($class, $property)
6476

6577
if ($reflectionProperty->isPublic()) {
6678
$reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
79+
} elseif ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
80+
$reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
6781
}
6882

6983
$reflectionProperty->setAccessible(true);

phpunit.xml.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
>
88
<testsuites>
99
<testsuite name="Doctrine Persistence Test Suite">
10-
<directory>./tests/Doctrine/</directory>
10+
<directory>./tests/Doctrine/Tests</directory>
11+
<directory phpVersion="7.4" phpVersionOperator=">=">./tests/Doctrine/Tests_PHP74</directory>
1112
</testsuite>
1213
</testsuites>
1314

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests_PHP74\Persistence\Mapping;
6+
7+
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
8+
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
9+
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
10+
use PHPUnit\Framework\TestCase;
11+
use ReflectionProperty;
12+
13+
/**
14+
* @group DCOM-93
15+
*/
16+
class RuntimeReflectionServiceTest extends TestCase
17+
{
18+
/** @var RuntimeReflectionService */
19+
private $reflectionService;
20+
21+
/** @var string */
22+
private string $typedNoDefaultProperty;
23+
/** @var string */
24+
private string $typedDefaultProperty = '';
25+
26+
/** @var string */
27+
public string $typedNoDefaultPublicProperty;
28+
29+
protected function setUp() : void
30+
{
31+
$this->reflectionService = new RuntimeReflectionService();
32+
}
33+
34+
public function testGetTypedNoDefaultReflectionProperty() : void
35+
{
36+
$reflProp = $this->reflectionService->getAccessibleProperty(self::class, 'typedNoDefaultProperty');
37+
self::assertInstanceOf(TypedNoDefaultReflectionProperty::class, $reflProp);
38+
}
39+
40+
public function testGetTypedDefaultReflectionProperty() : void
41+
{
42+
$reflProp = $this->reflectionService->getAccessibleProperty(self::class, 'typedDefaultProperty');
43+
self::assertInstanceOf(ReflectionProperty::class, $reflProp);
44+
self::assertNotInstanceOf(TypedNoDefaultReflectionProperty::class, $reflProp);
45+
}
46+
47+
public function testGetTypedPublicNoDefaultPropertyWorksWithGetValue() : void
48+
{
49+
$reflProp = $this->reflectionService->getAccessibleProperty(self::class, 'typedNoDefaultPublicProperty');
50+
self::assertInstanceOf(RuntimePublicReflectionProperty::class, $reflProp);
51+
self::assertNull($reflProp->getValue($this));
52+
}
53+
}

0 commit comments

Comments
 (0)