Skip to content

Commit

Permalink
Deprecate field name setter from FieldDescriptionInterface (#6720)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Dec 23, 2020
1 parent 2882754 commit 811b8e3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
17 changes: 17 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
@@ -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
=========================

Expand Down
28 changes: 25 additions & 3 deletions src/Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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');
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Admin/FieldDescriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/Admin/BaseFieldDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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
Expand Down

0 comments on commit 811b8e3

Please sign in to comment.