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

Add some deprecations about invalid values #451

Merged
merged 1 commit into from
Oct 29, 2020
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: 6 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ UPGRADE 3.x
UPGRADE FROM 3.x to 3.x
=======================

### Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager

Deprecated passing `null` as argument 2 for `find()`.
Deprecated passing `null` or an object which is in state new or removed as argument 1 for `getNormalizedIdentifier()`.
Deprecated passing `null` as argument 1 for `getUrlSafeIdentifier()`.

UPGRADE FROM 3.3 to 3.4
=======================

Expand Down
26 changes: 25 additions & 1 deletion src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ public function delete($object)

public function find($class, $id)
{
if (!isset($id)) {
if (null === $id) {
@trigger_error(sprintf(
'Passing null as argument 1 for %s() is deprecated since'
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return null;
}

Expand Down Expand Up @@ -293,7 +299,14 @@ public function getIdentifierFieldNames($class)

public function getNormalizedIdentifier($document)
{
// NEXT_MAJOR: Remove the following 2 checks and declare "object" as type for argument 1.
if (null === $document) {
@trigger_error(sprintf(
'Passing null as argument 1 for %s() is deprecated since'
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return null;
}

Expand All @@ -313,6 +326,17 @@ public function getNormalizedIdentifier($document)

public function getUrlSafeIdentifier($document)
{
// NEXT_MAJOR: Remove the following check and declare "object" as type for argument 1.
if (!\is_object($document)) {
@trigger_error(sprintf(
'Passing other type than object for argument 1 for %s() is deprecated since'
.' sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return null;
}

return $this->getNormalizedIdentifier($document);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,19 @@ public function getWrongDocuments(): iterable
yield ['sonata-project'];
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testGetNormalizedIdentifierNull(): void
{
$manager = new ModelManager($this->registry, $this->propertyAccessor);

$this->expectDeprecation(
'Passing null as argument 1 for Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getNormalizedIdentifier() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.'
);

$this->assertNull($manager->getNormalizedIdentifier(null));
}

Expand Down Expand Up @@ -335,10 +344,19 @@ public function testGetModelInstanceForProtectedDocument(): void
$this->assertInstanceOf(ProtectedDocument::class, $model->getModelInstance(ProtectedDocument::class));
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testFindBadId(): void
{
$model = new ModelManager($this->registry, $this->propertyAccessor);

$this->expectDeprecation(
'Passing null as argument 1 for Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::find() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.'
);

$this->assertNull($model->find('notImportant', null));
}

Expand All @@ -351,10 +369,19 @@ public function testGetUrlSafeIdentifierException(): void
$model->getNormalizedIdentifier(new \stdClass());
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testGetUrlSafeIdentifierNull(): void
{
$model = new ModelManager($this->registry, $this->propertyAccessor);

$this->expectDeprecation(
'Passing null as argument 1 for Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::getNormalizedIdentifier() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be not allowed in version 4.0.'
);

$this->assertNull($model->getNormalizedIdentifier(null));
}

Expand Down