Skip to content

Commit f5b6385

Browse files
mbabkerfranmomu
authored andcommitted
Doctrine\ORM\Mapping\ClassMetadata::getFieldMapping() will return an object on ORM 3 instead of an array
1 parent 2a89103 commit f5b6385

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

phpstan-baseline.neon

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,21 @@ parameters:
290290
count: 2
291291
path: src/Sluggable/SluggableListener.php
292292

293+
-
294+
message: "#^Access to property \\$type on an unknown class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
295+
count: 1
296+
path: src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
297+
298+
-
299+
message: "#^Class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping not found\\.$#"
300+
count: 1
301+
path: src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
302+
303+
-
304+
message: "#^Parameter \\$mapping of method Gedmo\\\\SoftDeleteable\\\\Mapping\\\\Event\\\\Adapter\\\\ORM\\:\\:getRawDateValue\\(\\) has invalid type Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
305+
count: 1
306+
path: src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
307+
293308
-
294309
message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$isMappedSuperclass\\.$#"
295310
count: 1
@@ -355,6 +370,21 @@ parameters:
355370
count: 1
356371
path: src/Timestampable/Mapping/Driver/Yaml.php
357372

373+
-
374+
message: "#^Access to property \\$type on an unknown class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
375+
count: 1
376+
path: src/Timestampable/Mapping/Event/Adapter/ORM.php
377+
378+
-
379+
message: "#^Class Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping not found\\.$#"
380+
count: 1
381+
path: src/Timestampable/Mapping/Event/Adapter/ORM.php
382+
383+
-
384+
message: "#^Parameter \\$mapping of method Gedmo\\\\Timestampable\\\\Mapping\\\\Event\\\\Adapter\\\\ORM\\:\\:getRawDateValue\\(\\) has invalid type Doctrine\\\\ORM\\\\Mapping\\\\FieldMapping\\.$#"
385+
count: 1
386+
path: src/Timestampable/Mapping/Event/Adapter/ORM.php
387+
358388
-
359389
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
360390
count: 1

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\DBAL\Types\Type;
1313
use Doctrine\DBAL\Types\Types;
1414
use Doctrine\ORM\Mapping\ClassMetadata;
15+
use Doctrine\ORM\Mapping\FieldMapping;
1516
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
1617
use Gedmo\Mapping\Event\ClockAwareAdapterInterface;
1718
use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter;
@@ -50,14 +51,14 @@ public function getDateValue($meta, $field)
5051
/**
5152
* Generates current timestamp for the specified mapping
5253
*
53-
* @param array<string, mixed> $mapping
54+
* @param array<string, mixed>|FieldMapping $mapping
5455
*
5556
* @return \DateTimeInterface|int
5657
*/
57-
private function getRawDateValue(array $mapping)
58+
private function getRawDateValue($mapping)
5859
{
5960
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
60-
$type = $mapping['type'] ?? null;
61+
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');
6162

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\DBAL\Types\Type;
1313
use Doctrine\DBAL\Types\Types;
1414
use Doctrine\ORM\Mapping\ClassMetadata;
15+
use Doctrine\ORM\Mapping\FieldMapping;
1516
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
1617
use Gedmo\Mapping\Event\ClockAwareAdapterInterface;
1718
use Gedmo\Timestampable\Mapping\Event\TimestampableAdapter;
@@ -50,14 +51,14 @@ public function getDateValue($meta, $field)
5051
/**
5152
* Generates current timestamp for the specified mapping
5253
*
53-
* @param array<string, mixed> $mapping
54+
* @param array<string, mixed>|FieldMapping $mapping
5455
*
5556
* @return \DateTimeInterface|int
5657
*/
57-
private function getRawDateValue(array $mapping)
58+
private function getRawDateValue($mapping)
5859
{
5960
$datetime = $this->clock instanceof ClockInterface ? $this->clock->now() : new \DateTimeImmutable();
60-
$type = $mapping['type'] ?? null;
61+
$type = $mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? '');
6162

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

tests/Gedmo/Uploadable/Mapping/ValidatorTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Gedmo\Tests\Uploadable\Mapping;
1313

1414
use Doctrine\ORM\Mapping\ClassMetadata;
15+
use Doctrine\ORM\Mapping\FieldMapping;
1516
use Gedmo\Exception\InvalidMappingException;
1617
use Gedmo\Exception\UploadableInvalidPathException;
1718
use Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorSha1;
@@ -51,7 +52,13 @@ public function testValidateFieldIfFieldIsNotOfAValidTypeThrowException(): void
5152
$this->expectException(InvalidMappingException::class);
5253
$this->meta->expects(static::once())
5354
->method('getFieldMapping')
54-
->willReturn(['type' => 'someType']);
55+
->willReturnCallback(static function (string $fieldName) {
56+
if (class_exists(FieldMapping::class)) {
57+
return FieldMapping::fromMappingArray(['type' => 'someType', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
58+
}
59+
60+
return ['type' => 'someType'];
61+
});
5562

5663
Validator::validateField(
5764
$this->meta,
@@ -122,7 +129,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsNotValidThrow
122129
->willReturn(new \ReflectionClass(new FakeEntity()));
123130
$this->meta
124131
->method('getFieldMapping')
125-
->willReturn(['type' => 'someType']);
132+
->willReturnCallback(static function (string $fieldName) {
133+
if (class_exists(FieldMapping::class)) {
134+
return FieldMapping::fromMappingArray(['type' => 'someType', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
135+
}
136+
137+
return ['type' => 'someType'];
138+
});
126139

127140
$config = [
128141
'fileMimeTypeField' => '',
@@ -151,7 +164,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsValidButDoesn
151164
->willReturn(new \ReflectionClass(new FakeEntity()));
152165
$this->meta
153166
->method('getFieldMapping')
154-
->willReturn(['type' => 'someType']);
167+
->willReturnCallback(static function (string $fieldName) {
168+
if (class_exists(FieldMapping::class)) {
169+
return FieldMapping::fromMappingArray(['type' => 'someType', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
170+
}
171+
172+
return ['type' => 'someType'];
173+
});
155174

156175
$config = [
157176
'fileMimeTypeField' => '',
@@ -179,7 +198,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsValidThenDont
179198
->willReturn(new \ReflectionClass(new FakeEntity()));
180199
$this->meta
181200
->method('getFieldMapping')
182-
->willReturn(['type' => 'string']);
201+
->willReturnCallback(static function (string $fieldName) {
202+
if (class_exists(FieldMapping::class)) {
203+
return FieldMapping::fromMappingArray(['type' => 'string', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
204+
}
205+
206+
return ['type' => 'string'];
207+
});
183208

184209
$config = [
185210
'fileMimeTypeField' => '',
@@ -207,7 +232,13 @@ public function testValidateConfigurationIfFilenameGeneratorValueIsAValidClassTh
207232
->willReturn(new \ReflectionClass(new FakeEntity()));
208233
$this->meta
209234
->method('getFieldMapping')
210-
->willReturn(['type' => 'string']);
235+
->willReturnCallback(static function (string $fieldName) {
236+
if (class_exists(FieldMapping::class)) {
237+
return FieldMapping::fromMappingArray(['type' => 'string', 'fieldName' => $fieldName, 'columnName' => $fieldName]);
238+
}
239+
240+
return ['type' => 'string'];
241+
});
211242

212243
$config = [
213244
'fileMimeTypeField' => '',

0 commit comments

Comments
 (0)