Skip to content

Commit

Permalink
Merge pull request #94 from doctrine/add_typed_no_default_reflection_…
Browse files Browse the repository at this point in the history
…property_class_usage

Add typed no default reflection property class usage
  • Loading branch information
beberlei authored Jan 16, 2020
2 parents be70c01 + f584465 commit 5dd3ac5
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 36 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- 7.4

cache:
directories:
Expand All @@ -23,9 +23,6 @@ script:
- ./vendor/bin/phpunit

jobs:
allow_failures:
- php: 7.4snapshot

include:
- stage: Test
env: DEPENDENCIES=low
Expand All @@ -50,6 +47,7 @@ jobs:
script: ./vendor/bin/phpcs

- stage: Code Quality
php: 7.4
env: STATIC_ANALYSIS
install: travis_retry composer install --prefer-dist
script: vendor/bin/phpstan analyze
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"doctrine/cache": "^1.0",
"doctrine/collections": "^1.0",
"doctrine/event-manager": "^1.0",
"doctrine/reflection": "^1.0"
"doctrine/reflection": "^1.1"
},
"require-dev": {
"phpstan/phpstan": "^0.11",
Expand All @@ -43,7 +43,8 @@
},
"autoload-dev": {
"psr-4": {
"Doctrine\\Tests\\": "tests/Doctrine/Tests"
"Doctrine\\Tests\\": "tests/Doctrine/Tests",
"Doctrine\\Tests_PHP74\\": "tests/Doctrine/Tests_PHP74"
}
},
"extra": {
Expand Down
61 changes: 32 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/Doctrine/Persistence/Mapping/RuntimeReflectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
namespace Doctrine\Persistence\Mapping;

use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
use function array_key_exists;
use function class_exists;
use function class_parents;
use function phpversion;
use function version_compare;

/**
* PHP Runtime Reflection Service.
*/
class RuntimeReflectionService implements ReflectionService
{
/** @var bool */
private $supportsTypedPropertiesWorkaround;

public function __construct()
{
$this->supportsTypedPropertiesWorkaround = version_compare((string) phpversion(), '7.4.0') >= 0;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -64,6 +76,8 @@ public function getAccessibleProperty($class, $property)

if ($reflectionProperty->isPublic()) {
$reflectionProperty = new RuntimePublicReflectionProperty($class, $property);
} elseif ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) {
$reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property);
}

$reflectionProperty->setAccessible(true);
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
>
<testsuites>
<testsuite name="Doctrine Persistence Test Suite">
<directory>./tests/Doctrine/</directory>
<directory>./tests/Doctrine/Tests</directory>
<directory phpVersion="7.4" phpVersionOperator=">=">./tests/Doctrine/Tests_PHP74</directory>
</testsuite>
</testsuites>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests_PHP74\Persistence\Mapping;

use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

/**
* @group DCOM-93
*/
class RuntimeReflectionServiceTest extends TestCase
{
/** @var RuntimeReflectionService */
private $reflectionService;

/** @var string */
private string $typedNoDefaultProperty;
/** @var string */
private string $typedDefaultProperty = '';

/** @var string */
public string $typedNoDefaultPublicProperty;

protected function setUp() : void
{
$this->reflectionService = new RuntimeReflectionService();
}

public function testGetTypedNoDefaultReflectionProperty() : void
{
$reflProp = $this->reflectionService->getAccessibleProperty(self::class, 'typedNoDefaultProperty');
self::assertInstanceOf(TypedNoDefaultReflectionProperty::class, $reflProp);
}

public function testGetTypedDefaultReflectionProperty() : void
{
$reflProp = $this->reflectionService->getAccessibleProperty(self::class, 'typedDefaultProperty');
self::assertInstanceOf(ReflectionProperty::class, $reflProp);
self::assertNotInstanceOf(TypedNoDefaultReflectionProperty::class, $reflProp);
}

public function testGetTypedPublicNoDefaultPropertyWorksWithGetValue() : void
{
$reflProp = $this->reflectionService->getAccessibleProperty(self::class, 'typedNoDefaultPublicProperty');
self::assertInstanceOf(RuntimePublicReflectionProperty::class, $reflProp);
self::assertNull($reflProp->getValue($this));
}
}

0 comments on commit 5dd3ac5

Please sign in to comment.