Skip to content

Commit

Permalink
Add RawValuePropertyAccessor to see how it will look in 8.4, pre supp…
Browse files Browse the repository at this point in the history
…ort for lazy objects.
  • Loading branch information
beberlei committed Dec 6, 2024
1 parent 65bfdcf commit ee39bd0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\ORM\Mapping\PropertyAccessors\EnumPropertyAccessor;
use Doctrine\ORM\Mapping\PropertyAccessors\ObjectCastPropertyAccessor;
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor;
use Doctrine\ORM\Mapping\PropertyAccessors\RawValuePropertyAccessor;
use Doctrine\ORM\Mapping\PropertyAccessors\ReadonlyAccessor;
use Doctrine\ORM\Mapping\PropertyAccessors\TypedNoDefaultPropertyAccessor;
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
Expand Down Expand Up @@ -62,6 +63,8 @@
use function trait_exists;
use function trim;

use const PHP_VERSION_ID;

/**
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
* of an entity and its associations.
Expand Down Expand Up @@ -2689,7 +2692,10 @@ public function getSequencePrefix(AbstractPlatform $platform): string
private function createPropertyAccessor(string $className, string $propertyName): PropertyAccessor
{
$reflectionProperty = new ReflectionProperty($className, $propertyName);
$accessor = ObjectCastPropertyAccessor::fromReflectionProperty($reflectionProperty);

$accessor = PHP_VERSION_ID >= 80400
? RawValuePropertyAccessor::fromReflectionProperty($reflectionProperty)
: ObjectCastPropertyAccessor::fromReflectionProperty($reflectionProperty);

if ($reflectionProperty->hasType() && ! $reflectionProperty->getType()->allowsNull()) {
$accessor = new TypedNoDefaultPropertyAccessor($accessor, $reflectionProperty);
Expand Down
52 changes: 52 additions & 0 deletions src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping\PropertyAccessors;

use Doctrine\ORM\Proxy\InternalProxy;
use ReflectionProperty;

use function ltrim;

/**
* This is a PHP 8.4 and up only class and replaces ObjectCastPropertyAccessor.
*
* It works based on the raw values of a property, which for a case of property hooks
* is the backed value. If we kept using setValue/getValue, this would go through the hooks,
* which potentially change the data.
*/
class RawValuePropertyAccessor implements PropertyAccessor
{
public static function fromReflectionProperty(ReflectionProperty $reflectionProperty): self
{
$name = $reflectionProperty->getName();
$key = $reflectionProperty->isPrivate() ? "\0" . ltrim($reflectionProperty->getDeclaringClass()->getName(), '\\') . "\0" . $name : ($reflectionProperty->isProtected() ? "\0*\0" . $name : $name);

return new self($reflectionProperty, $key);
}

private function __construct(private ReflectionProperty $reflectionProperty, private string $key)
{
}

public function setValue(object $object, mixed $value): void
{
if (! ($object instanceof InternalProxy && ! $object->__isInitialized())) {
$this->reflectionProperty->setRawValue($object, $value);

Check failure on line 36 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

UndefinedMethod

src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php:36:40: UndefinedMethod: Method ReflectionProperty::setRawValue does not exist (see https://psalm.dev/022)

Check failure on line 36 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

UndefinedMethod

src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php:36:40: UndefinedMethod: Method ReflectionProperty::setRawValue does not exist (see https://psalm.dev/022)

Check failure on line 36 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.8.2, phpstan-dbal3.neon)

Call to an undefined method ReflectionProperty::setRawValue().

return;
}

$object->__setInitialized(true);

$this->reflectionProperty->setRawValue($object, $value);

Check failure on line 43 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.8.2)

UndefinedMethod

src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php:43:36: UndefinedMethod: Method ReflectionProperty::setRawValue does not exist (see https://psalm.dev/022)

Check failure on line 43 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (default)

UndefinedMethod

src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php:43:36: UndefinedMethod: Method ReflectionProperty::setRawValue does not exist (see https://psalm.dev/022)

Check failure on line 43 in src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.8.2, phpstan-dbal3.neon)

Call to an undefined method ReflectionProperty::setRawValue().

$object->__setInitialized(false);
}

public function getValue(object $object): mixed
{
return ((array) $object)[$this->key] ?? null;
}
}

0 comments on commit ee39bd0

Please sign in to comment.