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

Doctrine\ORM\Mapping\ClassMetadata::getFieldMapping() will return an object on ORM 3 instead of an array #2765

Merged
merged 1 commit into from
Feb 13, 2024
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
30 changes: 30 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,21 @@ parameters:
count: 2
path: src/Sluggable/SluggableListener.php

-
message: "#^Access to property \\$type on an unknown class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
count: 1
path: src/SoftDeleteable/Mapping/Event/Adapter/ORM.php

-
message: "#^Class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping not found\\.$#"
count: 1
path: src/SoftDeleteable/Mapping/Event/Adapter/ORM.php

-
message: "#^Parameter \\$mapping of method Gedmo\\\\SoftDeleteable\\\\Mapping\\\\Event\\\\Adapter\\\\ORM\\:\\:getRawDateValue\\(\\) has invalid type Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
count: 1
path: src/SoftDeleteable/Mapping/Event/Adapter/ORM.php

-
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$isMappedSuperclass\\.$#"
count: 1
Expand Down Expand Up @@ -355,6 +370,21 @@ parameters:
count: 1
path: src/Timestampable/Mapping/Driver/Yaml.php

-
message: "#^Access to property \\$type on an unknown class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
count: 1
path: src/Timestampable/Mapping/Event/Adapter/ORM.php

-
message: "#^Class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping not found\\.$#"
count: 1
path: src/Timestampable/Mapping/Event/Adapter/ORM.php

-
message: "#^Parameter \\$mapping of method Gedmo\\\\Timestampable\\\\Mapping\\\\Event\\\\Adapter\\\\ORM\\:\\:getRawDateValue\\(\\) has invalid type Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
count: 1
path: src/Timestampable/Mapping/Event/Adapter/ORM.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 1
Expand Down
7 changes: 4 additions & 3 deletions src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\Mapping\Event\ClockAwareAdapterInterface;
use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter;
Expand Down Expand Up @@ -50,21 +51,21 @@
/**
* Generates current timestamp for the specified mapping
*
* @param array<string, mixed> $mapping
* @param array<string, mixed>|FieldMapping $mapping
*
* @return \DateTimeInterface|int
*/
private function getRawDateValue(array $mapping)
private function getRawDateValue($mapping)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$type = $mapping['type'] ?? null;
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');

if ('integer' === $type) {
return (int) $datetime->format('U');
}

if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return $datetime;

Check warning on line 68 in src/SoftDeleteable/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/SoftDeleteable/Mapping/Event/Adapter/ORM.php#L68

Added line #L68 was not covered by tests
}

return \DateTime::createFromImmutable($datetime);
Expand Down
7 changes: 4 additions & 3 deletions src/Timestampable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\Mapping\Event\ClockAwareAdapterInterface;
use Gedmo\Timestampable\Mapping\Event\TimestampableAdapter;
Expand Down Expand Up @@ -50,21 +51,21 @@
/**
* Generates current timestamp for the specified mapping
*
* @param array<string, mixed> $mapping
* @param array<string, mixed>|FieldMapping $mapping
*
* @return \DateTimeInterface|int
*/
private function getRawDateValue(array $mapping)
private function getRawDateValue($mapping)
{
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
$type = $mapping['type'] ?? null;
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');

if ('integer' === $type) {
return (int) $datetime->format('U');
}

if (in_array($type, ['date_immutable', 'time_immutable', 'datetime_immutable', 'datetimetz_immutable'], true)) {
return $datetime;

Check warning on line 68 in src/Timestampable/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/Timestampable/Mapping/Event/Adapter/ORM.php#L68

Added line #L68 was not covered by tests
}

return \DateTime::createFromImmutable($datetime);
Expand Down
41 changes: 36 additions & 5 deletions tests/Gedmo/Uploadable/Mapping/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Gedmo\Tests\Uploadable\Mapping;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Exception\UploadableInvalidPathException;
use Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorSha1;
Expand Down Expand Up @@ -51,7 +52,13 @@ public function testValidateFieldIfFieldIsNotOfAValidTypeThrowException(): void
$this->expectException(InvalidMappingException::class);
$this->meta->expects(static::once())
->method('getFieldMapping')
->willReturn(['type' => 'someType']);
->willReturnCallback(static function (string $fieldName) {
if (class_exists(FieldMapping::class)) {
return FieldMapping::fromMappingArray(['type' => 'someType', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
}

return ['type' => 'someType'];
});

Validator::validateField(
$this->meta,
Expand Down Expand Up @@ -122,7 +129,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsNotValidThrow
->willReturn(new \ReflectionClass(new FakeEntity()));
$this->meta
->method('getFieldMapping')
->willReturn(['type' => 'someType']);
->willReturnCallback(static function (string $fieldName) {
if (class_exists(FieldMapping::class)) {
return FieldMapping::fromMappingArray(['type' => 'someType', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
}

return ['type' => 'someType'];
});

$config = [
'fileMimeTypeField' => '',
Expand Down Expand Up @@ -151,7 +164,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsValidButDoesn
->willReturn(new \ReflectionClass(new FakeEntity()));
$this->meta
->method('getFieldMapping')
->willReturn(['type' => 'someType']);
->willReturnCallback(static function (string $fieldName) {
if (class_exists(FieldMapping::class)) {
return FieldMapping::fromMappingArray(['type' => 'someType', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
}

return ['type' => 'someType'];
});

$config = [
'fileMimeTypeField' => '',
Expand Down Expand Up @@ -179,7 +198,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsValidThenDont
->willReturn(new \ReflectionClass(new FakeEntity()));
$this->meta
->method('getFieldMapping')
->willReturn(['type' => 'string']);
->willReturnCallback(static function (string $fieldName) {
if (class_exists(FieldMapping::class)) {
return FieldMapping::fromMappingArray(['type' => 'string', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
}

return ['type' => 'string'];
});

$config = [
'fileMimeTypeField' => '',
Expand Down Expand Up @@ -207,7 +232,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsAValidClassTh
->willReturn(new \ReflectionClass(new FakeEntity()));
$this->meta
->method('getFieldMapping')
->willReturn(['type' => 'string']);
->willReturnCallback(static function (string $fieldName) {
if (class_exists(FieldMapping::class)) {
return FieldMapping::fromMappingArray(['type' => 'string', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
}

return ['type' => 'string'];
});

$config = [
'fileMimeTypeField' => '',
Expand Down
Loading