Skip to content

Commit 3b3010c

Browse files
committed
Use ClassMetadata::(get|set)FieldValue instead of getting the property reflection
1 parent eef8a13 commit 3b3010c

File tree

10 files changed

+110
-131
lines changed

10 files changed

+110
-131
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,24 +1038,6 @@ parameters:
10381038
count: 2
10391039
path: tests/Gedmo/DoctrineExtensionsTest.php
10401040

1041-
-
1042-
message: '#^Property Gedmo\\Tests\\Mapping\\Fixture\\Attribute\\TranslatableModel\:\:\$title \(string\|null\) is never assigned string so it can be removed from the property type\.$#'
1043-
identifier: property.unusedType
1044-
count: 1
1045-
path: tests/Gedmo/Mapping/Fixture/Attribute/TranslatableModel.php
1046-
1047-
-
1048-
message: '#^Property Gedmo\\Tests\\Mapping\\Fixture\\Attribute\\TranslatableModel\:\:\$titleFallbackFalse \(string\|null\) is never assigned string so it can be removed from the property type\.$#'
1049-
identifier: property.unusedType
1050-
count: 1
1051-
path: tests/Gedmo/Mapping/Fixture/Attribute/TranslatableModel.php
1052-
1053-
-
1054-
message: '#^Property Gedmo\\Tests\\Mapping\\Fixture\\Attribute\\TranslatableModel\:\:\$titleFallbackTrue \(string\|null\) is never assigned string so it can be removed from the property type\.$#'
1055-
identifier: property.unusedType
1056-
count: 1
1057-
path: tests/Gedmo/Mapping/Fixture/Attribute/TranslatableModel.php
1058-
10591041
-
10601042
message: '#^Class Gedmo\\Tests\\Translatable\\Fixture\\CategoryTranslation not found\.$#'
10611043
identifier: class.notFound
@@ -1116,12 +1098,6 @@ parameters:
11161098
count: 1
11171099
path: tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php
11181100

1119-
-
1120-
message: '#^Call to an undefined method Doctrine\\Persistence\\Mapping\\ClassMetadata\<object\>\:\:getReflectionProperty\(\)\.$#'
1121-
identifier: method.notFound
1122-
count: 2
1123-
path: tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php
1124-
11251101
-
11261102
message: '#^Call to an undefined method Doctrine\\Persistence\\ObjectManager\:\:getUnitOfWork\(\)\.$#'
11271103
identifier: method.notFound

tests/Gedmo/Mapping/MetadataFactory/CustomDriverTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ public function testShouldWork(): void
8989

9090
$id = $this->em
9191
->getClassMetadata(Timestampable::class)
92-
->getReflectionProperty('id')
93-
->getValue($test)
92+
->getFieldValue($test, 'id')
9493
;
9594
static::assertNotEmpty($id);
9695
}

tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public function testShouldWork(): void
9494

9595
$id = $this->em
9696
->getClassMetadata(Timestampable::class)
97-
->getReflectionProperty('id')
98-
->getValue($test)
97+
->getFieldValue($test, 'id')
9998
;
10099
static::assertNotEmpty($id);
101100
}

tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@ private function encode(EventAdapterInterface $ea, object $object, array $config
8383
$om = $ea->getObjectManager();
8484
$meta = $om->getClassMetadata(get_class($object));
8585
$uow = $om->getUnitOfWork();
86+
87+
if (!method_exists($meta, 'getFieldValue') || !method_exists($meta, 'setFieldValue')) {
88+
throw new \RuntimeException('ClassMetadata does not have (get|set)FieldValue() methods: '.get_class($meta));
89+
}
90+
8691
foreach ($config['encode'] as $field => $options) {
87-
$value = $meta->getReflectionProperty($field)->getValue($object);
92+
$value = $meta->getFieldValue($object, $field);
8893
$method = $options['type'];
8994
$encoded = $method($options['secret'].$value);
90-
$meta->getReflectionProperty($field)->setValue($object, $encoded);
95+
$meta->setFieldValue($object, $field, $encoded);
9196
}
9297
// recalculate changeset
9398
$ea->recomputeSingleObjectChangeSet($uow, $meta, $object);

tests/Gedmo/Sluggable/SluggablePositionTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testPositionedSlugOrder(): void
4040
$repo = $this->em->getRepository(Position::class);
4141

4242
$object = $repo->find(1);
43-
$slug = $meta->getReflectionProperty('slug')->getValue($object);
43+
$slug = $meta->getFieldValue($object, 'slug');
4444
static::assertSame('code-other-title-prop', $slug);
4545
}
4646

@@ -55,10 +55,10 @@ private function populate(): void
5555
{
5656
$meta = $this->em->getClassMetadata(Position::class);
5757
$object = new Position();
58-
$meta->getReflectionProperty('title')->setValue($object, 'title');
59-
$meta->getReflectionProperty('prop')->setValue($object, 'prop');
60-
$meta->getReflectionProperty('code')->setValue($object, 'code');
61-
$meta->getReflectionProperty('other')->setValue($object, 'other');
58+
$meta->setFieldValue($object, 'title', 'title');
59+
$meta->setFieldValue($object, 'prop', 'prop');
60+
$meta->setFieldValue($object, 'code', 'code');
61+
$meta->setFieldValue($object, 'other', 'other');
6262

6363
$this->em->persist($object);
6464
$this->em->flush();

tests/Gedmo/Tree/ConcurrencyTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ public function testConcurrentEntitiesInOneFlush(): void
7878

7979
$meta = $this->em->getClassMetadata(Category::class);
8080
$sport = $repo->findOneBy(['title' => 'Sport']);
81-
$left = $meta->getReflectionProperty('lft')->getValue($sport);
82-
$right = $meta->getReflectionProperty('rgt')->getValue($sport);
81+
$left = $meta->getFieldValue($sport, 'lft');
82+
$right = $meta->getFieldValue($sport, 'rgt');
8383

8484
static::assertSame(9, $left);
8585
static::assertSame(16, $right);
8686

8787
$skiing = $repo->findOneBy(['title' => 'Skiing']);
88-
$left = $meta->getReflectionProperty('lft')->getValue($skiing);
89-
$right = $meta->getReflectionProperty('rgt')->getValue($skiing);
88+
$left = $meta->getFieldValue($skiing, 'lft');
89+
$right = $meta->getFieldValue($skiing, 'rgt');
9090

9191
static::assertSame(10, $left);
9292
static::assertSame(13, $right);

tests/Gedmo/Tree/InMemoryUpdatesTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,29 @@ public function testInMemoryTreeInserts(): void
6464
$this->em->clear();
6565

6666
$node = $repo->find(2);
67-
$left = $meta->getReflectionProperty('lft')->getValue($node);
68-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
67+
$left = $meta->getFieldValue($node, 'lft');
68+
$right = $meta->getFieldValue($node, 'rgt');
6969
static::assertSame(2, $left);
7070
static::assertSame(5, $right);
7171

7272
$node = $repo->find(3);
73-
$left = $meta->getReflectionProperty('lft')->getValue($node);
74-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
73+
$left = $meta->getFieldValue($node, 'lft');
74+
$right = $meta->getFieldValue($node, 'rgt');
7575
static::assertSame(6, $left);
7676
static::assertSame(7, $right);
7777

7878
$node = $repo->find(4);
79-
$left = $meta->getReflectionProperty('lft')->getValue($node);
80-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
79+
$left = $meta->getFieldValue($node, 'lft');
80+
$right = $meta->getFieldValue($node, 'rgt');
8181
static::assertSame(3, $left);
8282
static::assertSame(4, $right);
8383

8484
/*print "Tree:\n";
8585
for ($i=1; $i < 5; $i++) {
8686
$node = $this->em->getRepository(Category::class)->find($i);
87-
$left = $meta->getReflectionProperty('lft')->getValue($node);
88-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
89-
$level = $meta->getReflectionProperty('level')->getValue($node);
87+
$left = $meta->getFieldValue($node, 'lft');
88+
$right = $meta->getFieldValue($node, 'rgt');
89+
$level = $meta->getFieldValue($node, 'level');
9090
print $node->getTitle()." - $left - $right - $level\n";
9191
}
9292
print "\n\n";*/

tests/Gedmo/Tree/MultiInheritanceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testInheritance(): void
3838
$repo = $this->em->getRepository(Node::class);
3939

4040
$food = $repo->findOneBy(['identifier' => 'food']);
41-
$left = $meta->getReflectionProperty('lft')->getValue($food);
41+
$left = $meta->getFieldValue($food, 'lft');
4242

4343
static::assertSame(1, $left);
4444
static::assertNotNull($food->getCreated());

tests/Gedmo/Tree/RepositoryTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public function testAdvancedFunctions(): void
177177
$repo = $this->em->getRepository(Category::class);
178178
$meta = $this->em->getClassMetadata(Category::class);
179179

180-
$left = $meta->getReflectionProperty('lft')->getValue($onions);
181-
$right = $meta->getReflectionProperty('rgt')->getValue($onions);
180+
$left = $meta->getFieldValue($onions, 'lft');
181+
$right = $meta->getFieldValue($onions, 'rgt');
182182

183183
static::assertSame(11, $left);
184184
static::assertSame(12, $right);
@@ -187,17 +187,17 @@ public function testAdvancedFunctions(): void
187187

188188
$repo->moveUp($onions, 1);
189189

190-
$left = $meta->getReflectionProperty('lft')->getValue($onions);
191-
$right = $meta->getReflectionProperty('rgt')->getValue($onions);
190+
$left = $meta->getFieldValue($onions, 'lft');
191+
$right = $meta->getFieldValue($onions, 'rgt');
192192

193193
static::assertSame(9, $left);
194194
static::assertSame(10, $right);
195195

196196
// move down onions by one position
197197
$repo->moveDown($onions, 1);
198198

199-
$left = $meta->getReflectionProperty('lft')->getValue($onions);
200-
$right = $meta->getReflectionProperty('rgt')->getValue($onions);
199+
$left = $meta->getFieldValue($onions, 'lft');
200+
$right = $meta->getFieldValue($onions, 'rgt');
201201

202202
static::assertSame(11, $left);
203203
static::assertSame(12, $right);
@@ -206,8 +206,8 @@ public function testAdvancedFunctions(): void
206206

207207
$repo->moveUp($onions, true);
208208

209-
$left = $meta->getReflectionProperty('lft')->getValue($onions);
210-
$right = $meta->getReflectionProperty('rgt')->getValue($onions);
209+
$left = $meta->getFieldValue($onions, 'lft');
210+
$right = $meta->getFieldValue($onions, 'rgt');
211211

212212
static::assertSame(5, $left);
213213
static::assertSame(6, $right);
@@ -220,32 +220,32 @@ public function testAdvancedFunctions(): void
220220

221221
$node = $this->em->getRepository(Category::class)
222222
->findOneBy(['title' => 'Cabbages']);
223-
$left = $meta->getReflectionProperty('lft')->getValue($node);
224-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
223+
$left = $meta->getFieldValue($node, 'lft');
224+
$right = $meta->getFieldValue($node, 'rgt');
225225

226226
static::assertSame(5, $left);
227227
static::assertSame(6, $right);
228228

229229
$node = $this->em->getRepository(Category::class)
230230
->findOneBy(['title' => 'Carrots']);
231-
$left = $meta->getReflectionProperty('lft')->getValue($node);
232-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
231+
$left = $meta->getFieldValue($node, 'lft');
232+
$right = $meta->getFieldValue($node, 'rgt');
233233

234234
static::assertSame(7, $left);
235235
static::assertSame(8, $right);
236236

237237
$node = $this->em->getRepository(Category::class)
238238
->findOneBy(['title' => 'Onions']);
239-
$left = $meta->getReflectionProperty('lft')->getValue($node);
240-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
239+
$left = $meta->getFieldValue($node, 'lft');
240+
$right = $meta->getFieldValue($node, 'rgt');
241241

242242
static::assertSame(9, $left);
243243
static::assertSame(10, $right);
244244

245245
$node = $this->em->getRepository(Category::class)
246246
->findOneBy(['title' => 'Potatoes']);
247-
$left = $meta->getReflectionProperty('lft')->getValue($node);
248-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
247+
$left = $meta->getFieldValue($node, 'lft');
248+
$right = $meta->getFieldValue($node, 'rgt');
249249

250250
static::assertSame(11, $left);
251251
static::assertSame(12, $right);
@@ -266,17 +266,17 @@ public function testAdvancedFunctions(): void
266266

267267
$node = $this->em->getRepository(Category::class)
268268
->findOneBy(['title' => 'Fruits']);
269-
$left = $meta->getReflectionProperty('lft')->getValue($node);
270-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
269+
$left = $meta->getFieldValue($node, 'lft');
270+
$right = $meta->getFieldValue($node, 'rgt');
271271

272272
static::assertSame(2, $left);
273273
static::assertSame(3, $right);
274274
static::assertSame('Food', $node->getParent()->getTitle());
275275

276276
$node = $this->em->getRepository(Category::class)
277277
->findOneBy(['title' => 'Cabbages']);
278-
$left = $meta->getReflectionProperty('lft')->getValue($node);
279-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
278+
$left = $meta->getFieldValue($node, 'lft');
279+
$right = $meta->getFieldValue($node, 'rgt');
280280

281281
static::assertSame(4, $left);
282282
static::assertSame(5, $right);
@@ -297,16 +297,16 @@ public function testRootRemoval(): void
297297
static::assertNull($food);
298298

299299
$node = $repo->findOneBy(['title' => 'Fruits']);
300-
$left = $meta->getReflectionProperty('lft')->getValue($node);
301-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
300+
$left = $meta->getFieldValue($node, 'lft');
301+
$right = $meta->getFieldValue($node, 'rgt');
302302

303303
static::assertSame(1, $left);
304304
static::assertSame(2, $right);
305305
static::assertNull($node->getParent());
306306

307307
$node = $repo->findOneBy(['title' => 'Vegitables']);
308-
$left = $meta->getReflectionProperty('lft')->getValue($node);
309-
$right = $meta->getReflectionProperty('rgt')->getValue($node);
308+
$left = $meta->getFieldValue($node, 'lft');
309+
$right = $meta->getFieldValue($node, 'rgt');
310310

311311
static::assertSame(3, $left);
312312
static::assertSame(12, $right);
@@ -367,8 +367,8 @@ public function testMoveRootNode(): void
367367

368368
$meta = $this->em->getClassMetadata(Category::class);
369369

370-
$left = $meta->getReflectionProperty('lft')->getValue($food);
371-
$right = $meta->getReflectionProperty('rgt')->getValue($food);
370+
$left = $meta->getFieldValue($food, 'lft');
371+
$right = $meta->getFieldValue($food, 'rgt');
372372

373373
static::assertSame(3, $left);
374374
static::assertSame(12, $right);

0 commit comments

Comments
 (0)