Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22f8264

Browse files
author
hamza
committed
fix #17993 - Deprecated callable strings
1 parent f5cf886 commit 22f8264

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

src/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public function createListFromChoices($choices, $value = null)
8686
{
8787
if (is_string($value) && !is_callable($value)) {
8888
$value = new PropertyPath($value);
89+
} elseif (is_string($value) && is_callable($value)) {
90+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
8991
}
9092

9193
if ($value instanceof PropertyPath) {
@@ -117,6 +119,8 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
117119
{
118120
if (is_string($value) && !is_callable($value)) {
119121
$value = new PropertyPath($value);
122+
} elseif (is_string($value) && is_callable($value)) {
123+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
120124
}
121125

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

154158
if (is_string($label) && !is_callable($label)) {
155159
$label = new PropertyPath($label);
160+
} elseif (is_string($label) && is_callable($label)) {
161+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
156162
}
157163

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

164170
if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
165171
$preferredChoices = new PropertyPath($preferredChoices);
172+
} elseif (is_string($preferredChoices) && is_callable($preferredChoices)) {
173+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
166174
}
167175

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

179187
if (is_string($index) && !is_callable($index)) {
180188
$index = new PropertyPath($index);
189+
} elseif (is_string($index) && is_callable($index)) {
190+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
181191
}
182192

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

189199
if (is_string($groupBy) && !is_callable($groupBy)) {
190200
$groupBy = new PropertyPath($groupBy);
201+
} elseif (is_string($groupBy) && is_callable($groupBy)) {
202+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
191203
}
192204

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

203215
if (is_string($attr) && !is_callable($attr)) {
204216
$attr = new PropertyPath($attr);
217+
} elseif (is_string($attr) && is_callable($attr)) {
218+
@trigger_error('Treating strings as callable is deprecated since version 3.1 and will throw an error in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
205219
}
206220

207221
if ($attr instanceof PropertyPath) {

src/Symfony/Component/Form/Tests/ChoiceList/Factory/PropertyAccessDecoratorTest.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ public function testCreateFromChoicesPropertyPathInstance()
6363
$this->assertSame(array('value'), $this->factory->createListFromChoices($choices, new PropertyPath('property')));
6464
}
6565

66+
/**
67+
* @group legacy
68+
*/
69+
public function testCreateFromChoicesPropertyPathWithCallableString()
70+
{
71+
$choices = array((object) array('property' => 'value'));
72+
73+
$this->decoratedFactory->expects($this->once())
74+
->method('createListFromChoices')
75+
->with($choices, 'end')
76+
->will($this->returnCallback(function ($choices, $callback) {
77+
return array_map($callback, $choices);
78+
}));
79+
80+
$this->assertSame(array('value'), $this->factory->createListFromChoices($choices, 'end'));
81+
}
82+
6683
public function testCreateFromLoaderPropertyPath()
6784
{
6885
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');
@@ -77,6 +94,25 @@ public function testCreateFromLoaderPropertyPath()
7794
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'property'));
7895
}
7996

97+
/**
98+
* @group legacy
99+
*/
100+
public function testCreateFromLoaderPropertyPathWithCallableString()
101+
{
102+
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');
103+
104+
$this->decoratedFactory->expects($this->once())
105+
->method('createListFromLoader')
106+
->with($loader, 'end')
107+
->will($this->returnCallback(function ($loader, $callback) {
108+
$array = array('property' => 'value');
109+
110+
return $callback($array);
111+
}));
112+
113+
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'end'));
114+
}
115+
80116
// https://github.com/symfony/symfony/issues/5494
81117
public function testCreateFromChoicesAssumeNullIfValuePropertyPathUnreadable()
82118
{
@@ -138,6 +174,28 @@ public function testCreateViewPreferredChoicesAsPropertyPath()
138174
));
139175
}
140176

177+
/**
178+
* @group legacy
179+
*/
180+
public function testCreateViewPreferredChoicesAsPropertyPathWithCallableString()
181+
{
182+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
183+
184+
$this->decoratedFactory->expects($this->once())
185+
->method('createView')
186+
->with($list, 'end')
187+
->will($this->returnCallback(function ($list, $preferred) {
188+
$array = (object) array('property' => true);
189+
190+
return $preferred($array);
191+
}));
192+
193+
$this->assertTrue($this->factory->createView(
194+
$list,
195+
'end'
196+
));
197+
}
198+
141199
public function testCreateViewPreferredChoicesAsPropertyPathInstance()
142200
{
143201
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -191,6 +249,29 @@ public function testCreateViewLabelsAsPropertyPath()
191249
));
192250
}
193251

252+
/**
253+
* @group legacy
254+
*/
255+
public function testCreateViewLabelsAsPropertyPathWithCallableString()
256+
{
257+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
258+
259+
$this->decoratedFactory->expects($this->once())
260+
->method('createView')
261+
->with($list, null, 'end')
262+
->will($this->returnCallback(function ($list, $preferred, $label) {
263+
$array = (object) array('property' => 'label');
264+
265+
return $label($array);
266+
}));
267+
268+
$this->assertSame('label', $this->factory->createView(
269+
$list,
270+
null, // preferred choices
271+
'end'
272+
));
273+
}
274+
194275
public function testCreateViewLabelsAsPropertyPathInstance()
195276
{
196277
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -228,6 +309,30 @@ public function testCreateViewIndicesAsPropertyPath()
228309
));
229310
}
230311

312+
/**
313+
* @group legacy
314+
*/
315+
public function testCreateViewIndicesAsPropertyPathWithCallableString()
316+
{
317+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
318+
319+
$this->decoratedFactory->expects($this->once())
320+
->method('createView')
321+
->with($list, null, null, 'end')
322+
->will($this->returnCallback(function ($list, $preferred, $label, $index) {
323+
$array = (object) array('property' => 'index');
324+
325+
return $index($array);
326+
}));
327+
328+
$this->assertSame('index', $this->factory->createView(
329+
$list,
330+
null, // preferred choices
331+
null, // label
332+
'end'
333+
));
334+
}
335+
231336
public function testCreateViewIndicesAsPropertyPathInstance()
232337
{
233338
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -267,6 +372,31 @@ public function testCreateViewGroupsAsPropertyPath()
267372
));
268373
}
269374

375+
/**
376+
* @group legacy
377+
*/
378+
public function testCreateViewGroupsAsPropertyPathWithCallableString()
379+
{
380+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
381+
382+
$this->decoratedFactory->expects($this->once())
383+
->method('createView')
384+
->with($list, null, null, null, 'end')
385+
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy) {
386+
$array = (object) array('property' => 'group');
387+
388+
return $groupBy($array);
389+
}));
390+
391+
$this->assertSame('group', $this->factory->createView(
392+
$list,
393+
null, // preferred choices
394+
null, // label
395+
null, // index
396+
'end'
397+
));
398+
}
399+
270400
public function testCreateViewGroupsAsPropertyPathInstance()
271401
{
272402
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -329,6 +459,32 @@ public function testCreateViewAttrAsPropertyPath()
329459
));
330460
}
331461

462+
/**
463+
* @group legacy
464+
*/
465+
public function testCreateViewAttrAsPropertyPathWithCallableString()
466+
{
467+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
468+
469+
$this->decoratedFactory->expects($this->once())
470+
->method('createView')
471+
->with($list, null, null, null, null, 'end')
472+
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) {
473+
$array = (object) array('property' => 'attr');
474+
475+
return $attr($array);
476+
}));
477+
478+
$this->assertSame('attr', $this->factory->createView(
479+
$list,
480+
null, // preferred choices
481+
null, // label
482+
null, // index
483+
null, // groups
484+
'end'
485+
));
486+
}
487+
332488
public function testCreateViewAttrAsPropertyPathInstance()
333489
{
334490
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -349,4 +505,5 @@ public function testCreateViewAttrAsPropertyPathInstance()
349505
new PropertyPath('property')
350506
));
351507
}
508+
352509
}

0 commit comments

Comments
 (0)