From 811b8e36e97f7fd2adeb99c8884591a46ddc10dc Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 23 Dec 2020 18:15:32 +0100 Subject: [PATCH] Deprecate field name setter from FieldDescriptionInterface (#6720) --- UPGRADE-3.x.md | 17 ++++++++++++++ src/Admin/BaseFieldDescription.php | 28 +++++++++++++++++++++--- src/Admin/FieldDescriptionInterface.php | 4 ++++ tests/Admin/BaseFieldDescriptionTest.php | 12 ++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/UPGRADE-3.x.md b/UPGRADE-3.x.md index 04fa786c6d..44201cb212 100644 --- a/UPGRADE-3.x.md +++ b/UPGRADE-3.x.md @@ -1,6 +1,23 @@ UPGRADE 3.x =========== +### Sonata\AdminBundle\Admin\BaseFieldDescription + +Method `__construct()` has been updated to receive the field name as argument 6: + +```php +public function __construct( + ?string $name = null, + array $options = [], + array $fieldMapping = [], + array $associationMapping = [], + array $parentAssociationMappings = [], + ?string $fieldName = null +) { +``` + +Deprecated `Sonata\AdminBundle\Admin\BaseFieldDescription::setFieldName()`. + UPGRADE FROM 3.82 to 3.83 ========================= diff --git a/src/Admin/BaseFieldDescription.php b/src/Admin/BaseFieldDescription.php index f87c888a0a..f9672e4e01 100644 --- a/src/Admin/BaseFieldDescription.php +++ b/src/Admin/BaseFieldDescription.php @@ -137,14 +137,15 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface private static $fieldGetters = []; /** - * NEXT_MAJOR: Remove the null default value and restrict param type to `string`. + * NEXT_MAJOR: Remove the null default value for $name and restrict param type to `string`. */ public function __construct( ?string $name = null, array $options = [], array $fieldMapping = [], array $associationMapping = [], - array $parentAssociationMappings = [] + array $parentAssociationMappings = [], + ?string $fieldName = null ) { // NEXT_MAJOR: Remove this check and keep the else part. if (null === $name) { @@ -155,6 +156,15 @@ public function __construct( ), E_USER_DEPRECATED); } else { $this->setName($name); + + if (null === $fieldName) { + // NEXT_MAJOR: Remove this line and uncomment the following. + $fieldName = substr(strrchr('.'.$name, '.'), 1); +// $fieldName = $name; + } + + // NEXT_MAJOR: Remove 'sonata_deprecation_mute' and the phpstan-ignore. + $this->setFieldName($fieldName, 'sonata_deprecation_mute'); } $this->setOptions($options); @@ -177,8 +187,19 @@ public function __construct( // abstract protected function setAssociationMapping(array $associationMapping): void; // abstract protected function setParentAssociationMappings(array $parentAssociationMappings): void; + /** + * NEXT_MAJOR: Change the visibility to private. + */ public function setFieldName($fieldName) { + if ('sonata_deprecation_mute' !== (\func_get_args()[1] ?? null)) { + @trigger_error(sprintf( + 'The %s() method is deprecated since sonata-project/admin-bundle 3.x' + .' and will become private in version 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + } + $this->fieldName = $fieldName; } @@ -191,8 +212,9 @@ public function setName($name) { $this->name = $name; + // NEXT_MAJOR: Remove this code since the field name will be set in the construct. if (!$this->getFieldName()) { - $this->setFieldName(substr(strrchr('.'.$name, '.'), 1)); + $this->setFieldName(substr(strrchr('.'.$name, '.'), 1), 'sonata_deprecation_mute'); } } diff --git a/src/Admin/FieldDescriptionInterface.php b/src/Admin/FieldDescriptionInterface.php index e15c484e27..698bd88b9b 100644 --- a/src/Admin/FieldDescriptionInterface.php +++ b/src/Admin/FieldDescriptionInterface.php @@ -24,6 +24,10 @@ interface FieldDescriptionInterface { /** + * NEXT_MAJOR: Remove this method. + * + * @deprecated since sonata-project/admin-bundle 3.x and will be removed in 4.0. + * * set the field name. * * @param string $fieldName diff --git a/tests/Admin/BaseFieldDescriptionTest.php b/tests/Admin/BaseFieldDescriptionTest.php index 486f44adf0..328df67a4a 100644 --- a/tests/Admin/BaseFieldDescriptionTest.php +++ b/tests/Admin/BaseFieldDescriptionTest.php @@ -27,6 +27,16 @@ class BaseFieldDescriptionTest extends TestCase { use ExpectDeprecationTrait; + public function testConstruct(): void + { + $description = new FieldDescription('foo.bar'); + + $this->assertSame('foo.bar', $description->getName()); + // NEXT_MAJOR: Remove this line and uncomment the following + $this->assertSame('bar', $description->getFieldName()); +// $this->assertSame('foo.bar', $description->getFieldName()); + } + public function testConstructingWithMapping(): void { $fieldMapping = ['field_name' => 'fieldName']; @@ -39,11 +49,13 @@ public function testConstructingWithMapping(): void $fieldMapping, $associationMapping, $parentAssociationMapping, + 'bar' ); $this->assertSame($fieldMapping, $description->getFieldMapping()); $this->assertSame($associationMapping, $description->getAssociationMapping()); $this->assertSame($parentAssociationMapping, $description->getParentAssociationMappings()); + $this->assertSame('bar', $description->getFieldName()); } public function testSetName(): void