8000 fix #17993 - Deprecated callable strings · Simperfit/symfony@edfe973 · GitHub
[go: up one dir, main page]

Skip to content

Commit edfe973

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

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-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( 8000 $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: 153 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,24 @@ 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+
return $callback($array);
110+
}));
111+
112+
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'end'));
113+
}
114+
80115
// https://github.com/symfony/symfony/issues/5494
81116
public function testCreateFromChoicesAssumeNullIfValuePropertyPathUnreadable()
82117
{
@@ -138,6 +173,27 @@ public function testCreateViewPreferredChoicesAsPropertyPath()
138173
));
139174
}
140175

176+
/**
177+
* @group legacy
178+
*/
179+
public function testCreateViewPreferredChoicesAsPropertyPathWithCallableString()
180+
{
181+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
182+
183+
$this->decoratedFactory->expects($this->once())
184+
->method('createView')
185+
->with($list, 'end')
186+
->will($this->returnCallback(function ($list, $preferred) {
187+
$array = (object) array('property' => true);
188+
return $preferred($array);
189+
}));
190+
191+
$this->assertTrue($this->factory->createView(
192+
$list,
193+
'end'
194+
));
195+
}
196+
141197
public function testCreateViewPreferredChoicesAsPropertyPathInstance()
142198
{
143199
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -191,6 +247,28 @@ public function testCreateViewLabelsAsPropertyPath()
191247
));
192248
}
193249

250+
/**
251+
* @group legacy
252+
*/
253+
public function testCreateViewLabelsAsPropertyPathWithCallableString()
254+
{
255+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
256+
257+
$this->decoratedFactory->expects($this->once())
258+
->method('createView')
259+
->with($list, null, 'end')
260+
->will($this->returnCallback(function ($list, $preferred, $label) {
261+
$array = (object) array('property' => 'label');
262+
return $label($array);
263+
}));
264+
265+
$this->assertSame('label', $this->factory->createView(
266+
$list,
267+
null, // preferred choices
268+
'end'
269+
));
270+
}
271+
194272
public function testCreateViewLabelsAsPropertyPathInstance()
195273
{
196274
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -228,6 +306,29 @@ public function testCreateViewIndicesAsPropertyPath()
228306
));
229307
}
230308

309+
/**
310+
* @group legacy
311+
*/
312+
public function testCreateViewIndicesAsPropertyPathWithCallableString()
313+
{
314+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
315+
316+
$this->decoratedFactory->expects($this->once())
317+
->method('createView')
318+
->with($list, null, null, 'end')
319+
->will($this->returnCallback(function ($list, $preferred, $label, $index) {
320+
$array = (object) array('property' => 'index');
321+
return $index($array);
322+
}));
323+
324+
$this->assertSame('index', $this->factory->createView(
325+
$list,
326+
null, // preferred choices
327+
null, // label
328+
'end'
329+
));
330+
}
331+
231332
public function testCreateViewIndicesAsPropertyPathInstance()
232333
{
233334
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -267,6 +368,30 @@ public function testCreateViewGroupsAsPropertyPath()
267368
));
268369
}
269370

371+
/**
372+
* @group legacy
373+
*/
374+
public function testCreateViewGroupsAsPropertyPathWithCallableString()
375+
{
376+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
377+
378+
$this->decoratedFactory->expects($this->once())
379+
->method('createView')
380+
->with($list, null, null, null, 'end')
381+
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy) {
382+
$array = (object) array('property' => 'group');
383+
return $groupBy($array);
384+
}));
385+
386+
$this->assertSame('group', $this->factory->createView(
387+
$list,
388+
null, // preferred choices
389+
null, // label
390+
null, // index
391+
'end'
392+
));
393+
}
394+
270395
public function testCreateViewGroupsAsPropertyPathInstance()
271396
{
272397
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -329,6 +454,32 @@ public function testCreateViewAttrAsPropertyPath()
329454
));
330455
}
331456

457+
458+
/**
459+
* @group legacy
460+
*/
461+
public function testCreateViewAttrAsPropertyPathWithCallableString()
462+
{
463+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
464+
465+
$this->decoratedFactory->expects($this->once())
466+
->method('createView')
467+
->with($list, null, null, null, null, 'end')
468+
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) {
469+
$array = (object) array('property' => 'attr');
470+
return $attr($array);
471+
}));
472+
473+
$this->assertSame('attr', $this->factory->createView(
474+
$list,
475+
null, // preferred choices
476+
null, // label
477+
null, // index
478+
null, // groups
479+
'end'
480+
));
481+
}
482+
332483
public function testCreateViewAttrAsPropertyPathInstance()
333484
{
334485
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -349,4 +500,6 @@ public function testCreateViewAttrAsPropertyPathInstance()
349500
new PropertyPath('property')
350501
));
351502
}
503+
504+
352505
}

0 commit comments

Comments
 (0)
0