Skip to content

[PHP8.4]private(set) properties should be skipped to avoid resetting values #11872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
use function strtr;
use function substr;
use function ucfirst;
use function version_compare;

use const DIRECTORY_SEPARATOR;
use const PHP_VERSION;

/**
* This factory is used to create proxy objects for entities at runtime.
Expand Down Expand Up @@ -263,7 +265,11 @@ private function getProxyFactory(string $className): Closure
$name = $property->name;

if ($property->isStatic() || (($class->hasField($name) || $class->hasAssociation($name)) && ! isset($identifiers[$name]))) {
continue;
if (version_compare(PHP_VERSION, '8.4', '<')) {
continue;
} elseif ($property->isPrivateSet() === false) {
continue;
}
Comment on lines +268 to +272
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic looks wrong, it only skips private set fields in PHP 8.4+ - but not others anymore.

Should maybe be:

if ($property->isStatic() || (($class->hasField($name) || $class->hasAssociation($name)) && ! isset($identifiers[$name])) || (PHP_VERSION_ID >= 80400 && $property->isPrivateSet())) {
    continue;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to write a test making sure which properties are skipped. I'll get back to you shortly.

}

$prefix = $property->isPrivate() ? "\0" . $property->class . "\0" : ($property->isProtected() ? "\0*\0" : '');
Expand Down
27 changes: 27 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/DDC11871/DDC11871Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\DDC11871;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'DDC11871_Order')]
class DDC11871Order
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inline the classes into the Test or create a new modelset.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlining classes makes tests with PHP < 8.4 failling. What do you mean by "create a new modelset"?

{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public int $id;

#[ORM\Column]
private(set) string $company;

Check failure on line 19 in tests/Tests/ORM/Functional/Ticket/DDC11871/DDC11871Order.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Visibility must be declared on property "$company"

public function __construct(
#[ORM\ManyToOne(targetEntity: DDC11871User::class, fetch: 'LAZY')]
private(set) DDC11871User $user,
) {
$this->company = $user->company;
}
}
39 changes: 39 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/DDC11871/DDC11871Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\DDC11871;

use Doctrine\Tests\OrmFunctionalTestCase;

use const PHP_VERSION_ID;

class DDC11871Test extends OrmFunctionalTestCase
{
public function setUp(): void
{
parent::setUp();

if (PHP_VERSION_ID < 80400) {
self::markTestSkipped('The ' . self::class . ' requires PHP 8.4.');
}

$this->setUpEntitySchema([
DDC11871User::class,
DDC11871Order::class,
]);
}

public function testEntityHydratation(): void
{
$user = new DDC11871User('Some company');
$this->_em->persist($user);
$order = new DDC11871Order($user);
$this->_em->persist($order);
$this->_em->flush();
$this->_em->clear();

$hydrated = $this->_em->getRepository(DDC11871Order::class)->findAll();
self::assertCount(1, $hydrated);
}
}
25 changes: 25 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/DDC11871/DDC11871User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\DDC11871;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'DDC11871_User')]
class DDC11871User
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public int $id;

#[ORM\Column(type: 'string')]
private(set) string $company;

Check failure on line 19 in tests/Tests/ORM/Functional/Ticket/DDC11871/DDC11871User.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Visibility must be declared on property "$company"

public function __construct(string $company)
{
$this->company = $company;
}
}
Loading