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

Skip to content

Commit fc219d7

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

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-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(< 8000 span class=pl-s1>$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,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('Passing callable strings is deprecated since ve 8000 rsion 3.1 and PropertyAccessDecorator will treat them as strings 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('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);
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('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);
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('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);
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('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);
205219
}
206220

207221
if ($attr instanceof PropertyPath) {

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

Lines changed: 154 additions & 0 deletions
4D03
Original file line numberDiff line numberDiff line change
@@ 8000 -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 testCreateFromLoaderPropertyP 77F4 ath()
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
}
10000 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');

0 commit comments

Comments
 (0)
0