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

Skip to content

Commit 3ab86d5

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

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

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

Lines changed: 34 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('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings 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('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
120124
}
121125

122126
if ($value instanceof PropertyPath) {
@@ -153,6 +157,12 @@ 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('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
162+
163+
$label = function ($choice) use ($label) {
164+
return $label($choice);
165+
};
156166
}
157167

158168
if ($label instanceof PropertyPath) {
@@ -163,6 +173,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
163173

164174
if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
165175
$preferredChoices = new PropertyPath($preferredChoices);
176+
} elseif (is_string($preferredChoices) && is_callable($preferredChoices)) {
177+
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
178+
179+
$preferredChoices = function ($choice) use ($preferredChoices) {
180+
return $preferredChoices($choice);
181+
};
166182
}
167183

168184
if ($preferredChoices instanceof PropertyPath) {
@@ -178,6 +194,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
178194

179195
if (is_string($index) && !is_callable($index)) {
180196
$index = new PropertyPath($index);
197+
} elseif (is_string($index) && is_callable($index)) {
198+
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
199+
200+
$index = function ($choice) use ($index) {
201+
return $index($choice);
202+
};
181203
}
182204

183205
if ($index instanceof PropertyPath) {
@@ -188,6 +210,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
188210

189211
if (is_string($groupBy) && !is_callable($groupBy)) {
190212
$groupBy = new PropertyPath($groupBy);
213+
} elseif (is_string($groupBy) && is_callable($groupBy)) {
214+
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
215+
216+
$groupBy = function ($choice) use ($groupBy) {
217+
return $groupBy($choice);
218+
};
191219
}
192220

193221
if ($groupBy instanceof PropertyPath) {
@@ -202,6 +230,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
202230

203231
if (is_string($attr) && !is_callable($attr)) {
204232
$attr = new PropertyPath($attr);
233+
} elseif (is_string($attr) && is_callable($attr)) {
234+
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as strings in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
235+
236+
$attr = function ($choice) use ($attr) {
237+
return $attr($choice);
238+
};
205239
}
206240

207241
if ($attr instanceof PropertyPath) {

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

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ 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(array('end' => 'value'));
72+
73+
$this->decoratedFactory->expects($this->once())
74+
->method('createListFromChoices')
75+
->with($choices, 'end')
76+
->will($this->returnValue(array('value')));
77+
78+
$this->assertSame(array('value'), $this->factory->createListFromChoices($choices, 'end'));
79+
}
80+
6681
public function testCreateFromLoaderPropertyPath()
6782
{
6883
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');
@@ -77,6 +92,25 @@ public function testCreateFromLoaderPropertyPath()
7792
$this->assertSame('value', $this->factory->createListFromLoader($loader, 'property'));
7893
}
7994

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

175+
/**
176+
* @group legacy
177+
*/
178+
public function testCreateViewPreferredChoicesAsPropertyPathWithCallableString()
179+
{
180+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
181+
182+
$this->decoratedFactory->expects($this->once())
183+
->method('createView')
184+
->with($list, 'end')
185+
->will($this->returnCallback(function ($list, $preferred) {
186+
$array = array('end' => 'result');
187+
188+
return $preferred($array);
189+
}));
190+
191+
$this->assertSame('result',$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,29 @@ 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 = array('property' => 'label');
262+
263+
return $label($array);
264+
}));
265+
266+
$this->assertSame('label', $this->factory->createView(
267+
$list,
268+
null, // preferred choices
269+
'end'
270+
));
271+
}
272+
194273
public function testCreateViewLabelsAsPropertyPathInstance()
195274
{
196275
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -228,6 +307,30 @@ public function testCreateViewIndicesAsPropertyPath()
228307
));
229308
}
230309

310+
/**
311+
* @group legacy
312+
*/
313+
public function testCreateViewIndicesAsPropertyPathWithCallableString()
314+
{
315+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
316+
317+
$this->decoratedFactory->expects($this->once())
318+
->method('createView')
319+
->with($list, null, null, 'end')
320+
->will($this->returnCallback(function ($list, $preferred, $label, $index) {
321+
$array = array('property' => 'index');
322+
323+
return $index($array);
324+
}));
325+
326+
$this->assertSame('index', $this->factory->createView(
327+
$list,
328+
null, // preferred choices
329+
null, // label
330+
'end'
331+
));
332+
}
333+
231334
public function testCreateViewIndicesAsPropertyPathInstance()
232335
{
233336
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -267,6 +370,31 @@ public function testCreateViewGroupsAsPropertyPath()
267370
));
268371
}
269372

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

460+
/**
461+
* @group legacy
462+
*/
463+
public function testCreateViewAttrAsPropertyPathWithCallableString()
464+
{
465+
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
466+
467+
$this->decoratedFactory->expects($this->once())
468+
->method('createView')
469+
->with($list, null, null, null, null, 'end')
470+
->will($this->returnCallback(function ($list, $preferred, $label, $index, $groupBy, $attr) {
471+
$array = array('property' => 'attr');
472+
473+
return $attr($array);
474+
}));
475+
476+
$this->assertSame('attr', $this->factory->createView(
477+
$list,
478+
null, // preferred choices
479+
null, // label
480+
null, // index
481+
null, // groups
482+
'end'
483+
));
484+
}
485+
332486
public function testCreateViewAttrAsPropertyPathInstance()
333487
{
334488
$list = $this->getMock('Symfony\Component\Form\ChoiceList\ChoiceListInterface');
@@ -349,4 +503,5 @@ public function testCreateViewAttrAsPropertyPathInstance()
349503
new PropertyPath('property')
350504
));
351505
}
506+
352507
}

0 commit comments

Comments
 (0)
0