Skip to content
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

Fix discriminatorValue key 0 #2716

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 */
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
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
{
}
Loading