Skip to content

fix #17993 - Deprecated callable strings #18020

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

Merged
merged 1 commit into from
Apr 15, 2016
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
19 changes: 19 additions & 0 deletions UPGRADE-3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ Form
* Support for data objects that implements both `Traversable` and `ArrayAccess`
in `ResizeFormListener::preSubmit` method has been deprecated and will be
removed in Symfony 4.0.

* Using callable strings as choice options in ChoiceType has been deprecated
in favor of `PropertyPath` in Symfony 4.0 use a "\Closure" instead.

Before:

```php
'choice_value' => new PropertyPath('range'),
'choice_label' => 'strtoupper',
```

After:

```php
'choice_value' => 'range',
'choice_label' => function ($choice) {
return strtoupper($choice);
},
```

FrameworkBundle
---------------
Expand Down
20 changes: 20 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ Form

* Support for data objects that implements both `Traversable` and
`ArrayAccess` in `ResizeFormListener::preSubmit` method has been removed.

* Using callable strings as choice options in ChoiceType is not supported
anymore in favor of passing PropertyPath instances.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace "has been removed" by "is not supported anymore".

But please wait for a symfony decider to tell you if it's ok. Thanks :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. That sounds a bit better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

Before:

```php
'choice_value' => new PropertyPath('range'),
'choice_label' => 'strtoupper',
```

After:

```php
'choice_value' => 'range',
'choice_label' => function ($choice) {
return strtoupper($choice);
},
```


FrameworkBundle
---------------
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CHANGELOG
* deprecated the "choices_as_values" option of ChoiceType
* deprecated support for data objects that implements both `Traversable` and
`ArrayAccess` in `ResizeFormListener::preSubmit` method

* Using callable strings as choice options in `ChoiceType` has been deprecated
and will be used as `PropertyPath` instead of callable in Symfony 4.0.

3.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public function createListFromChoices($choices, $value = null)
{
if (is_string($value) && !is_callable($value)) {
$value = new PropertyPath($value);
} elseif (is_string($value) && is_callable($value)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($value instanceof PropertyPath) {
Expand Down Expand Up @@ -117,6 +119,8 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
{
if (is_string($value) && !is_callable($value)) {
$value = new PropertyPath($value);
} elseif (is_string($value) && is_callable($value)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($value instanceof PropertyPath) {
Expand Down Expand Up @@ -153,6 +157,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($label) && !is_callable($label)) {
$label = new PropertyPath($label);
} elseif (is_string($label) && is_callable($label)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($label instanceof PropertyPath) {
Expand All @@ -163,6 +169,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
$preferredChoices = new PropertyPath($preferredChoices);
} elseif (is_string($preferredChoices) && is_callable($preferredChoices)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($preferredChoices instanceof PropertyPath) {
Expand All @@ -178,6 +186,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($index) && !is_callable($index)) {
$index = new PropertyPath($index);
} elseif (is_string($index) && is_callable($index)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($index instanceof PropertyPath) {
Expand All @@ -188,6 +198,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($groupBy) && !is_callable($groupBy)) {
$groupBy = new PropertyPath($groupBy);
} elseif (is_string($groupBy) && is_callable($groupBy)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($groupBy instanceof PropertyPath) {
Expand All @@ -202,6 +214,8 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,

if (is_string($attr) && !is_callable($attr)) {
$attr = new PropertyPath($attr);
} elseif (is_string($attr) && is_callable($attr)) {
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

if ($attr instanceof PropertyPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ public function testCreateFromChoicesPropertyPathInstance()
$this->assertSame(array('value'), $this->factory->createListFromChoices($choices, new PropertyPath('property')));
}

/**
* @group legacy
*/
public function testCreateFromChoicesPropertyPathWithCallableString()
{
$choices = array('foo' => 'bar');

$this->decoratedFactory->expects($this->once())
->method('createListFromChoices')
->with($choices, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT', $this->factory->createListFromChoices($choices, 'end'));
}

public function testCreateFromLoaderPropertyPath()
{
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');
Expand All @@ -77,6 +92,21 @@ public function testCreateFromLoaderPropertyPath()
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'property'));
}

/**
* @group legacy
*/
public function testCreateFromLoaderPropertyPathWithCallableString()
{
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');

$this->decoratedFactory->expects($this->once())
->method('createListFromLoader')
->with($loader, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT', $this->factory->createListFromLoader($loader, 'end'));
}

// https://github.com/symfony/symfony/issues/5494
public function testCreateFromChoicesAssumeNullIfValuePropertyPathUnreadable()
{
Expand Down Expand Up @@ -138,6 +168,24 @@ public function testCreateViewPreferredChoicesAsPropertyPath()
));
}

/**
* @group legacy
*/
public function testCreateViewPreferredChoicesAsPropertyPathWithCallableString()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT',$this->factory->createView(
$list,
'end'
));
}

public function testCreateViewPreferredChoicesAsPropertyPathInstance()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -191,6 +239,25 @@ public function testCreateViewLabelsAsPropertyPath()
));
}

/**
* @group legacy
*/
public function testCreateViewLabelsAsPropertyPathWithCallableString()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
'end'
));
}

public function testCreateViewLabelsAsPropertyPathInstance()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -228,6 +295,26 @@ public function testCreateViewIndicesAsPropertyPath()
));
}

/**
* @group legacy
*/
public function testCreateViewIndicesAsPropertyPathWithCallableString()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
'end'
));
}

public function testCreateViewIndicesAsPropertyPathInstance()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -267,6 +354,27 @@ public function testCreateViewGroupsAsPropertyPath()
));
}

/**
* @group legacy
*/
public function testCreateViewGroupsAsPropertyPathWithCallableString()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // index
'end'
));
}

public function testCreateViewGroupsAsPropertyPathInstance()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down Expand Up @@ -329,6 +437,28 @@ public function testCreateViewAttrAsPropertyPath()
));
}

/**
* @group legacy
*/
public function testCreateViewAttrAsPropertyPathWithCallableString()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');

$this->decoratedFactory->expects($this->once())
->method('createView')
->with($list, null, null, null, null, 'end')
->willReturn('RESULT');

$this->assertSame('RESULT', $this->factory->createView(
$list,
null, // preferred choices
null, // label
null, // inde
null, // groups
'end'
));
}

public function testCreateViewAttrAsPropertyPathInstance()
{
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
Expand Down