Skip to content

Commit 02cb00a

Browse files
ovitoresgreg0ire
authored andcommitted
Fix error when use numeric for reference name
PHP automatically casts integerish strings used as array keys to int.
1 parent 9876650 commit 02cb00a

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

phpstan-baseline.neon

+2-14
Original file line numberDiff line numberDiff line change
@@ -261,31 +261,19 @@ parameters:
261261
-
262262
message: '#^Parameter \#1 \$className of method Doctrine\\Persistence\\ObjectManager\:\:getClassMetadata\(\) expects class\-string\<object\>, string given\.$#'
263263
identifier: argument.type
264-
count: 2
265-
path: src/ReferenceRepository.php
266-
267-
-
268-
message: '#^Property Doctrine\\Common\\DataFixtures\\ReferenceRepository\:\:\$identitiesByClass \(array\<class\-string, array\<string, mixed\>\>\) does not accept array\<string, array\<string, mixed\>\>\.$#'
269-
identifier: assign.propertyType
270-
count: 2
271-
path: src/ReferenceRepository.php
272-
273-
-
274-
message: '#^Property Doctrine\\Common\\DataFixtures\\ReferenceRepository\:\:\$referencesByClass \(array\<class\-string, array\<string, object\>\>\) does not accept array\<string, array\<string, mixed\>\>\.$#'
275-
identifier: assign.propertyType
276264
count: 1
277265
path: src/ReferenceRepository.php
278266

279267
-
280-
message: '#^Property Doctrine\\Common\\DataFixtures\\ReferenceRepository\:\:\$referencesByClass \(array\<class\-string, array\<string, object\>\>\) does not accept array\<string, array\<string, object\>\>\.$#'
268+
message: '#^Property Doctrine\\Common\\DataFixtures\\ReferenceRepository\:\:\$identitiesByClass \(array\<class\-string, array\<string, mixed\>\>\) does not accept array\<string, array\<string, mixed\>\>\.$#'
281269
identifier: assign.propertyType
282270
count: 1
283271
path: src/ReferenceRepository.php
284272

285273
-
286274
message: '#^Unable to resolve the template type T in call to method Doctrine\\Persistence\\ObjectManager\:\:getClassMetadata\(\)$#'
287275
identifier: argument.templateType
288-
count: 2
276+
count: 1
289277
path: src/ReferenceRepository.php
290278

291279
-

src/ReferenceRepository.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use function array_key_exists;
1515
use function array_keys;
16+
use function array_map;
1617
use function get_class;
1718
use function sprintf;
1819

@@ -35,7 +36,7 @@ class ReferenceRepository
3536
* List of named references to the fixture objects
3637
* gathered during fixure loading
3738
*
38-
* @phpstan-var array<class-string, array<string, object>>
39+
* @phpstan-var array<class-string, array<string|int, object>>
3940
*/
4041
private array $referencesByClass = [];
4142

@@ -283,7 +284,7 @@ public function getReferenceNames(object $reference)
283284
return [];
284285
}
285286

286-
return array_keys($this->referencesByClass[$class], $reference, true);
287+
return array_map('strval', array_keys($this->referencesByClass[$class], $reference, true));
287288
}
288289

289290
/**
@@ -346,7 +347,7 @@ public function getReferences()
346347
/**
347348
* Get all stored references
348349
*
349-
* @phpstan-return array<class-string, array<string, object>>
350+
* @phpstan-return array<class-string, array<string|int, object>>
350351
*/
351352
public function getReferencesByClass(): array
352353
{
@@ -368,7 +369,7 @@ public function getManager()
368369
*
369370
* @param string $className Class name of reference object
370371
*
371-
* @return string
372+
* @return class-string
372373
*/
373374
protected function getRealClass(string $className)
374375
{

tests/Common/DataFixtures/ReferenceRepositoryTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,25 @@ public function testGetIdentifierWhenHasNotBeenManagedYetByUnitOfWork(): void
268268

269269
$this->assertEquals($identitiesExpected, $identities['entity']);
270270
}
271+
272+
public function testGivenAReferenceWithNameWithIntegerValueWhenGetReferenceNamesOfEntitiesThenReturnNamesInStringType(): void
273+
{
274+
$em = $this->getMockSqliteEntityManager();
275+
$referenceRepository = new ReferenceRepository($em);
276+
277+
$schemaTool = new SchemaTool($em);
278+
$schemaTool->dropSchema([]);
279+
$schemaTool->createSchema([$em->getClassMetadata(Role::class)]);
280+
281+
$role = new Role();
282+
$role->setName('role_name');
283+
$em->persist($role);
284+
$em->flush();
285+
286+
$name = (string) $role->getId();
287+
$referenceRepository->setReference($name, $role);
288+
$names = $referenceRepository->getReferenceNames($role);
289+
$this->assertCount(1, $names);
290+
$this->assertSame('1', $names[0]);
291+
}
271292
}

0 commit comments

Comments
 (0)