Skip to content

Commit

Permalink
Fix discriminatorValue 0
Browse files Browse the repository at this point in the history
  • Loading branch information
José Ángel Parada committed Jan 10, 2025
1 parent 27002d1 commit 15ad85d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
final class DiscriminatorMap implements Annotation
{
/** @var array<string, class-string> */
/** @var array<class-string> */
public $value;

/** @param array<string, class-string> $value */
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ private function getClassDiscriminatorValues(ClassMetadata $metadata): array

foreach ($metadata->subClasses as $className) {
$key = array_search($className, $metadata->discriminatorMap);
if (! $key) {
if ($key === false) {
continue;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;

class GH2158Test extends BaseTestCase
{
public function testDiscriminatorMapCreationType(): void
{
$obj = new GH2158FirstType();
$this->dm->persist($obj);
$this->dm->flush();

self::assertEquals($this->dm->find(GH2158Abstract::class, $obj->getId()), $obj);
}
}

#[ODM\Document(collection: 'documents')]
#[ODM\InheritanceType('SINGLE_COLLECTION')]
#[ODM\DiscriminatorField('type')]
#[ODM\DiscriminatorMap([0 => GH2158FirstType::class, 1 => GH2158SecondType::class])]

Check failure on line 25 in tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Parameter #1 $value of attribute class Doctrine\ODM\MongoDB\Mapping\Annotations\DiscriminatorMap constructor expects array<string, class-string>, array<int, string> given.

Check failure on line 25 in tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InvalidArgument

tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2158Test.php:25:24: InvalidArgument: Argument 1 of Doctrine\ODM\MongoDB\Mapping\Annotations\DiscriminatorMap::__construct expects array<string, class-string>, but list{Doctrine\ODM\MongoDB\Tests\Functional\Ticket\GH2158FirstType::class, Doctrine\ODM\MongoDB\Tests\Functional\Ticket\GH2158SecondType::class} provided (see https://psalm.dev/004)
abstract class GH2158Abstract
{
/** @var string */
#[ODM\Id]
protected $id;

public function getId(): string
{
return $this->id;
}
}

#[ODM\Document]
class GH2158FirstType extends GH2158Abstract
{
}

#[ODM\Document]
class GH2158SecondType extends GH2158Abstract
{
}

0 comments on commit 15ad85d

Please sign in to comment.