diff --git a/src/Util/FormBuilderIterator.php b/src/Util/FormBuilderIterator.php index a16083a2cd2..3c97587bc53 100644 --- a/src/Util/FormBuilderIterator.php +++ b/src/Util/FormBuilderIterator.php @@ -52,10 +52,20 @@ class FormBuilderIterator extends \RecursiveArrayIterator * * @param string|false $prefix */ - public function __construct(FormBuilderInterface $formBuilder, $prefix = false) + public function __construct(FormBuilderInterface $formBuilder, $prefix = null) { parent::__construct(); $this->formBuilder = $formBuilder; + + // NEXT_MAJOR: Remove this block. + if (null !== $prefix && !\is_string($prefix)) { + @trigger_error(sprintf( + 'Passing other type than string or null as argument 2 for method %s() is deprecated since' + .' sonata-project/admin-bundle 3.x. It will accept only string and null in version 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + } + // NEXT_MAJOR: Remove next line. $this->prefix = \is_string($prefix) ? $prefix : $formBuilder->getName(); // NEXT_MAJOR: Uncomment next line. diff --git a/tests/Util/FormBuilderIteratorTest.php b/tests/Util/FormBuilderIteratorTest.php index ff15f7708cc..6ff9a8c6174 100644 --- a/tests/Util/FormBuilderIteratorTest.php +++ b/tests/Util/FormBuilderIteratorTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; use Sonata\AdminBundle\Util\FormBuilderIterator; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilder; @@ -25,6 +26,8 @@ */ class FormBuilderIteratorTest extends TestCase { + use ExpectDeprecationTrait; + /** * @var EventDispatcherInterface */ @@ -69,4 +72,19 @@ public function testHasChildren(): void $iterator = new FormBuilderIterator($this->builder); $this->assertTrue($iterator->hasChildren()); } + + /** + * NEXT_MAJOR: Remove this test. + * + * @group legacy + */ + public function testTriggersADeprecationWithWrongPrefixType(): void + { + $this->expectDeprecation('Passing other type than string or null as argument 2 for method Sonata\AdminBundle\Util\FormBuilderIterator::__construct() is deprecated since sonata-project/admin-bundle 3.x. It will accept only string and null in version 4.0.'); + + $this->builder->add('name', TextType::class); + $iterator = new FormBuilderIterator($this->builder, new \stdClass()); + + $this->assertSame($iterator->key(), 'name_name'); + } }