Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 0 additions & 6 deletions src/Mapping/Annotations/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ final class Document extends AbstractDocument
/** @var string|null */
public $repositoryClass;

/** @var Index[] */
public $indexes;

/** @var bool */
public $readOnly;

Expand All @@ -33,21 +30,18 @@ final class Document extends AbstractDocument

/**
* @param string|array{name: string, capped?: bool, size?: int, max?: int}|null $collection
* @param Index[] $indexes
* @param int|string|null $writeConcern
*/
public function __construct(
?string $db = null,
public $collection = null,
?string $repositoryClass = null,
array $indexes = [],
bool $readOnly = false,
?string $shardKey = null,
public $writeConcern = null,
) {
$this->db = $db;
$this->repositoryClass = $repositoryClass;
$this->indexes = $indexes;
$this->readOnly = $readOnly;
$this->shardKey = $shardKey;
}
Expand Down
22 changes: 0 additions & 22 deletions src/Mapping/Annotations/Indexes.php

This file was deleted.

28 changes: 2 additions & 26 deletions src/Mapping/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
$this->addVectorSearchIndex($metadata, $attribute);
}

if ($attribute instanceof ODM\Indexes) {
trigger_deprecation(
'doctrine/mongodb-odm',
'2.2',
'The "@Indexes" attribute used in class "%s" is deprecated. Specify all "@Index" and "@UniqueIndex" attributes on the class.',
$className,
);
$value = $attribute->value;
foreach (is_array($value) ? $value : [$value] as $index) {
$this->addIndex($metadata, $index);
}
} elseif ($attribute instanceof ODM\InheritanceType) {
if ($attribute instanceof ODM\InheritanceType) {
$metadata->setInheritanceType(constant(ClassMetadata::class . '::INHERITANCE_TYPE_' . $attribute->value));
} elseif ($attribute instanceof ODM\DiscriminatorField) {
$metadata->setDiscriminatorField($attribute->value);
Expand Down Expand Up @@ -255,20 +244,7 @@ public function loadMetadataForClass($className, PersistenceClassMetadata $metad
$indexes[] = $propertyAttribute;
}

if ($propertyAttribute instanceof ODM\Indexes) {
trigger_deprecation(
'doctrine/mongodb-odm',
'2.2',
'The "@Indexes" attribute used in property "%s" of class "%s" is deprecated. Specify all "@Index" and "@UniqueIndex" attributes on the class.',
$property->getName(),
$className,
);

$value = $propertyAttribute->value;
foreach (is_array($value) ? $value : [$value] as $index) {
$indexes[] = $index;
}
} elseif ($propertyAttribute instanceof ODM\AlsoLoad) {
if ($propertyAttribute instanceof ODM\AlsoLoad) {
$mapping['alsoLoadFields'] = (array) $propertyAttribute->value;
} elseif ($propertyAttribute instanceof ODM\Version) {
$mapping['version'] = true;
Expand Down
134 changes: 0 additions & 134 deletions tests/Tests/Mapping/AnnotationDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@

namespace Doctrine\ODM\MongoDB\Tests\Mapping;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\FileClassLocator;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;

use function call_user_func;
use function class_exists;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;

use const E_USER_DEPRECATED;

class AnnotationDriverTest extends AbstractAnnotationDriverTestCase
{
Expand All @@ -29,129 +20,4 @@ protected static function loadDriver(array $paths = []): MappingDriver

return AnnotationDriver::create($paths);
}

public function testIndexesClassAnnotationEmitsDeprecationMessage(): void
{
$driver = static::loadDriver();
$classMetadata = new ClassMetadata(DeprecatedIndexesClassAnnotation::class);

$this->captureDeprecationMessages(
static fn () => $driver->loadMetadataForClass($classMetadata->name, $classMetadata),
$errors,
);

self::assertCount(1, $errors);
self::assertSame(sprintf('Since doctrine/mongodb-odm 2.2: The "@Indexes" attribute used in class "%s" is deprecated. Specify all "@Index" and "@UniqueIndex" attributes on the class.', DeprecatedIndexesClassAnnotation::class), $errors[0]);

$indexes = $classMetadata->indexes;

self::assertTrue(isset($indexes[0]['keys']['foo']));
self::assertEquals(1, $indexes[0]['keys']['foo']);
}

public function testIndexesOptionOfDocumentClassAnnotationEmitsDeprecationMessage(): void
{
$driver = static::loadDriver();
$classMetadata = new ClassMetadata(DeprecatedDocumentClassAnnotationIndexesOption::class);

$this->captureDeprecationMessages(
static fn () => $driver->loadMetadataForClass($classMetadata->name, $classMetadata),
$errors,
);

self::assertCount(1, $errors);
self::assertSame(sprintf('Since doctrine/mongodb-odm 2.2: The "indexes" parameter in the "%s" attribute for class "%s" is deprecated. Specify all "@Index" and "@UniqueIndex" attributes on the class.', Document::class, DeprecatedDocumentClassAnnotationIndexesOption::class), $errors[0]);

$indexes = $classMetadata->indexes;

self::assertTrue(isset($indexes[0]['keys']['foo']));
self::assertEquals(1, $indexes[0]['keys']['foo']);
}

public function testIndexesPropertyAnnotationEmitsDeprecationMessage(): void
{
$driver = static::loadDriver();
$classMetadata = new ClassMetadata(DeprecatedIndexesPropertyAnnotation::class);

$this->captureDeprecationMessages(
static fn () => $driver->loadMetadataForClass($classMetadata->name, $classMetadata),
$errors,
);

self::assertCount(1, $errors);
self::assertSame(sprintf('Since doctrine/mongodb-odm 2.2: The "@Indexes" attribute used in property "foo" of class "%s" is deprecated. Specify all "@Index" and "@UniqueIndex" attributes on the class.', DeprecatedIndexesPropertyAnnotation::class), $errors[0]);

$indexes = $classMetadata->indexes;

self::assertTrue(isset($indexes[0]['keys']['foo']));
self::assertEquals(1, $indexes[0]['keys']['foo']);
}

/**
* @param list<string> $errors
*
* @param-out list<string> $errors
*/
private function captureDeprecationMessages(callable $callable, ?array &$errors): mixed
{
/* TODO: this method can be replaced with expectUserDeprecationMessage() in PHPUnit 11+.
* See: https://docs.phpunit.de/en/11.1/error-handling.html#expecting-deprecations-e-user-deprecated */
$errors = [];

set_error_handler(static function (int $errno, string $errstr) use (&$errors): bool {
$errors[] = $errstr;

return false;
}, E_USER_DEPRECATED);

try {
return call_user_func($callable);
} finally {
restore_error_handler();
}
}
}

/**
* @ODM\Document
* @ODM\Indexes({
* @ODM\Index(keys={"foo"="asc"})
* })
*/
class DeprecatedIndexesClassAnnotation
{
/** @ODM\Id */
public ?string $id;

/** @ODM\Field(type="string") */
public string $foo;
}

/**
* @ODM\Document(indexes={
* @ODM\Index(keys={"foo"="asc"})
* })
*/
class DeprecatedDocumentClassAnnotationIndexesOption
{
/** @ODM\Id */
public ?string $id;

/** @ODM\Field(type="string") */
public string $foo;
}

/** @ODM\Document */
class DeprecatedIndexesPropertyAnnotation
{
/** @ODM\Id */
public ?string $id;

/**
* @ODM\Field(type="string")
* @ODM\Indexes({
* @ODM\Index
* })
*/
public string $foo;
}