Skip to content

Commit 2ef01eb

Browse files
committed
Skip writing readonly properties if the value did not change
1 parent ed3fdb4 commit 2ef01eb

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ class ClassMetadataInfo implements ClassMetadata
701701
/**
702702
* The ReflectionProperty instances of the mapped class.
703703
*
704-
* @var ReflectionProperty[]|null[]
704+
* @var array<string, ReflectionProperty|null>
705705
*/
706706
public $reflFields = [];
707707

lib/Doctrine/ORM/UnitOfWork.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Doctrine\Persistence\PropertyChangedListener;
4141
use Exception;
4242
use InvalidArgumentException;
43+
use LogicException;
4344
use RuntimeException;
4445
use Throwable;
4546
use UnexpectedValueException;
@@ -53,6 +54,7 @@
5354
use function array_pop;
5455
use function array_sum;
5556
use function array_values;
57+
use function assert;
5658
use function count;
5759
use function current;
5860
use function get_class;
@@ -66,6 +68,8 @@
6668
use function spl_object_id;
6769
use function sprintf;
6870

71+
use const PHP_VERSION_ID;
72+
6973
/**
7074
* The UnitOfWork is responsible for tracking changes to objects during an
7175
* "object-level" transaction and for writing out changes to the database
@@ -2723,9 +2727,25 @@ public function createEntity($className, array $data, &$hints = [])
27232727
}
27242728

27252729
foreach ($data as $field => $value) {
2726-
if (isset($class->fieldMappings[$field])) {
2727-
$class->reflFields[$field]->setValue($entity, $value);
2730+
if (! isset($class->fieldMappings[$field])) {
2731+
continue;
27282732
}
2733+
2734+
assert($class->reflFields[$field] !== null);
2735+
2736+
if (
2737+
PHP_VERSION_ID >= 80100
2738+
&& $class->reflFields[$field]->isReadOnly()
2739+
&& $class->reflFields[$field]->isInitialized($entity)
2740+
) {
2741+
if ($class->reflFields[$field]->getValue($entity) === $value) {
2742+
continue;
2743+
}
2744+
2745+
throw new LogicException(sprintf('Attempting to change readonly field %s.', $field));
2746+
}
2747+
2748+
$class->reflFields[$field]->setValue($entity, $value);
27292749
}
27302750

27312751
// Loading the entity right here, if its in the eager loading map get rid of it there.

phpstan-baseline.neon

+21-12
Original file line numberDiff line numberDiff line change
@@ -1001,18 +1001,17 @@ parameters:
10011001
path: lib/Doctrine/ORM/Query/Parser.php
10021002

10031003
-
1004-
message:
1005-
"""
1006-
#^PHPDoc tag @return has invalid value \\(AST\\\\BetweenExpression\\|
1007-
AST\\\\CollectionMemberExpression\\|
1008-
AST\\\\ComparisonExpression\\|
1009-
AST\\\\EmptyCollectionComparisonExpression\\|
1010-
AST\\\\ExistsExpression\\|
1011-
AST\\\\InExpression\\|
1012-
AST\\\\InstanceOfExpression\\|
1013-
AST\\\\LikeExpression\\|
1014-
AST\\\\NullComparisonExpression\\)\\: Unexpected token "\\\\n \\* ", expected type at offset 344$#
1015-
"""
1004+
message: """
1005+
#^PHPDoc tag @return has invalid value \\(AST\\\\BetweenExpression\\|
1006+
AST\\\\CollectionMemberExpression\\|
1007+
AST\\\\ComparisonExpression\\|
1008+
AST\\\\EmptyCollectionComparisonExpression\\|
1009+
AST\\\\ExistsExpression\\|
1010+
AST\\\\InExpression\\|
1011+
AST\\\\InstanceOfExpression\\|
1012+
AST\\\\LikeExpression\\|
1013+
AST\\\\NullComparisonExpression\\)\\: Unexpected token "\\\\n \\* ", expected type at offset 344$#
1014+
"""
10161015
count: 1
10171016
path: lib/Doctrine/ORM/Query/Parser.php
10181017

@@ -1876,6 +1875,16 @@ parameters:
18761875
count: 1
18771876
path: lib/Doctrine/ORM/UnitOfWork.php
18781877

1878+
-
1879+
message: "#^Call to an undefined method ReflectionProperty\\:\\:isInitialized\\(\\)\\.$#"
1880+
count: 1
1881+
path: lib/Doctrine/ORM/UnitOfWork.php
1882+
1883+
-
1884+
message: "#^Call to an undefined method ReflectionProperty\\:\\:isReadOnly\\(\\)\\.$#"
1885+
count: 1
1886+
path: lib/Doctrine/ORM/UnitOfWork.php
1887+
18791888
-
18801889
message: "#^Parameter \\#1 \\$collection of method Doctrine\\\\ORM\\\\Persisters\\\\Collection\\\\CollectionPersister\\:\\:delete\\(\\) expects Doctrine\\\\ORM\\\\PersistentCollection, Doctrine\\\\Common\\\\Collections\\\\Collection\\<\\(int\\|string\\), object\\> given\\.$#"
18811890
count: 1

psalm-baseline.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3559,7 +3559,7 @@
35593559
<code>$assoc['targetEntity']</code>
35603560
<code>$assoc['type']</code>
35613561
</PossiblyNullArrayAccess>
3562-
<PossiblyNullReference occurrences="33">
3562+
<PossiblyNullReference occurrences="32">
35633563
<code>buildCachedCollectionPersister</code>
35643564
<code>buildCachedEntityPersister</code>
35653565
<code>getCacheFactory</code>

0 commit comments

Comments
 (0)