Skip to content

Commit 631c3a3

Browse files
committed
Import useful reflection classes
Other reflection classes do not seem to be used anywhere.
1 parent ebb6c32 commit 631c3a3

9 files changed

+509
-86
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
"doctrine/annotations": "^1.0",
2525
"doctrine/cache": "^1.0",
2626
"doctrine/collections": "^1.0",
27-
"doctrine/event-manager": "^1.0",
28-
"doctrine/reflection": "^1.2"
27+
"doctrine/event-manager": "^1.0"
2928
},
3029
"require-dev": {
3130
"phpstan/phpstan": "^0.11",
3231
"doctrine/coding-standard": "^6.0",
32+
"doctrine/common": "^3.0",
3333
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
3434
"vimeo/psalm": "^3.11"
3535
},

composer.lock

+91-79
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Doctrine/Persistence/Mapping/RuntimeReflectionService.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Doctrine\Persistence\Mapping;
44

5-
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
6-
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
5+
use Doctrine\Persistence\Reflection\RuntimePublicReflectionProperty;
6+
use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionProperty;
77
use ReflectionClass;
88
use ReflectionException;
99
use ReflectionMethod;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Doctrine\Persistence\Reflection;
4+
5+
use Doctrine\Common\Proxy\Proxy;
6+
use ReflectionProperty;
7+
8+
/**
9+
* PHP Runtime Reflection Public Property - special overrides for public properties.
10+
*/
11+
class RuntimePublicReflectionProperty extends ReflectionProperty
12+
{
13+
/**
14+
* {@inheritDoc}
15+
*
16+
* Checks is the value actually exist before fetching it.
17+
* This is to avoid calling `__get` on the provided $object if it
18+
* is a {@see \Doctrine\Common\Proxy\Proxy}.
19+
*/
20+
public function getValue($object = null)
21+
{
22+
$name = $this->getName();
23+
24+
if ($object instanceof Proxy && ! $object->__isInitialized()) {
25+
$originalInitializer = $object->__getInitializer();
26+
$object->__setInitializer(null);
27+
$val = $object->$name ?? null;
28+
$object->__setInitializer($originalInitializer);
29+
30+
return $val;
31+
}
32+
33+
return isset($object->$name) ? parent::getValue($object) : null;
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*
39+
* Avoids triggering lazy loading via `__set` if the provided object
40+
* is a {@see \Doctrine\Common\Proxy\Proxy}.
41+
*
42+
* @link https://bugs.php.net/bug.php?id=63463
43+
*/
44+
public function setValue($object, $value = null)
45+
{
46+
if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
47+
parent::setValue($object, $value);
48+
49+
return;
50+
}
51+
52+
$originalInitializer = $object->__getInitializer();
53+
$object->__setInitializer(null);
54+
parent::setValue($object, $value);
55+
$object->__setInitializer($originalInitializer);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Doctrine\Persistence\Reflection;
4+
5+
use ReflectionProperty;
6+
7+
/**
8+
* PHP Typed No Default Reflection Property - special override for typed properties without a default value.
9+
*/
10+
class TypedNoDefaultReflectionProperty extends ReflectionProperty
11+
{
12+
/**
13+
* {@inheritDoc}
14+
*
15+
* Checks that a typed property is initialized before accessing its value.
16+
* This is neccessary to avoid PHP error "Error: Typed property must not be accessed before initialization".
17+
* Should be used only for reflecting typed properties without a default value.
18+
*/
19+
public function getValue($object = null)
20+
{
21+
return $object !== null && $this->isInitialized($object) ? parent::getValue($object) : null;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*
27+
* Works around the problem with setting typed no default properties to
28+
* NULL which is not supported, instead unset() to uninitialize.
29+
*
30+
* @link https://github.com/doctrine/orm/issues/7999
31+
*/
32+
public function setValue($object, $value = null)
33+
{
34+
if ($value === null) {
35+
$propertyName = $this->getName();
36+
37+
$unsetter = function () use ($propertyName) {
38+
unset($this->$propertyName);
39+
};
40+
$unsetter = $unsetter->bindTo($object, $this->getDeclaringClass()->getName());
41+
$unsetter();
42+
43+
return;
44+
}
45+
46+
parent::setValue($object, $value);
47+
}
48+
}

tests/Doctrine/Tests/Persistence/Mapping/RuntimeReflectionServiceTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Doctrine\Tests\Persistence\Mapping;
44

5-
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
65
use Doctrine\Persistence\Mapping\MappingException;
76
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
7+
use Doctrine\Persistence\Reflection\RuntimePublicReflectionProperty;
88
use PHPUnit\Framework\TestCase;
99
use ReflectionProperty;
1010
use function count;

0 commit comments

Comments
 (0)