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

Skip to content

Commit 4ae9bf6

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

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ 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);
91+
92+
$value = function (array $choices) use ($value) {
93+
return $value($choices);
94+
};
95+
96+
return $value($choices);
8997
}
9098

9199
if ($value instanceof PropertyPath) {
@@ -117,6 +125,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
117125
{
118126
if (is_string($value) && !is_callable($value)) {
119127
$value = new PropertyPath($value);
128+
} elseif (is_string($value) && is_callable($value)) {
129+
@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);
130+
131+
$value = function ($choices) use ($value) {
132+
return $value($choices);
133+
};
120134
}
121135

122136
if ($value instanceof PropertyPath) {
@@ -153,6 +167,13 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
153167

154168
if (is_string($label) && !is_callable($label)) {
155169
$label = new PropertyPath($label);
170+
} elseif (is_string($label) && is_callable($label)) {
171+
@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);
172+
173+
$label = function ($choice) use ($label) {
174+
return $label($choice);
175+
};
176+
156177
}
157178

158179
if ($label instanceof PropertyPath) {
@@ -163,6 +184,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
163184

164185
if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
165186
$preferredChoices = new PropertyPath($preferredChoices);
187+
} elseif (is_string($preferredChoices) && is_callable($preferredChoices)) {
188+
@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);
189+
190+
$preferredChoices = function ($choice) use ($preferredChoices) {
191+
return $preferredChoices($choice);
192+
};
166193
}
167194

168195
if ($preferredChoices instanceof PropertyPath) {
@@ -178,6 +205,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
178205

179206
if (is_string($index) && !is_callable($index)) {
180207
$index = new PropertyPath($index);
208+
} elseif (is_string($index) && is_callable($index)) {
209+
@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);
210+
211+
$index = function ($choice) use ($index) {
212+
return $index($choice);
213+
};
181214
}
182215

183216
if ($index instanceof PropertyPath) {
@@ -188,6 +221,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
188221

189222
if (is_string($groupBy) && !is_callable($groupBy)) {
190223
$groupBy = new PropertyPath($groupBy);
224+
} elseif (is_string($groupBy) && is_callable($groupBy)) {
225+
@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);
226+
227+
$groupBy = function ($choice) use ($groupBy) {
228+
return $groupBy($choice);
229+
};
191230
}
192231

193232
if ($groupBy instanceof PropertyPath) {
@@ -202,6 +241,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
202241

203242
if (is_string($attr) && !is_callable($attr)) {
204243
$attr = new PropertyPath($attr);
244+
} elseif (is_string($attr) && is_callable($attr)) {
245+
@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);
246+
247+
$attr = function ($choice) use ($attr) {
248+
return $attr($choice);
249+
};
205250
}
206251

207252
if ($attr instanceof PropertyPath) {

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

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

66+
public function testCreateFromChoicesPropertyPathWithCallableString()
67+
{
68+
$choices = array(array('property' => 'value'));
69+
70+
$this->decoratedFactory->expects($this->once())
71+
->method('createListFromChoices')
72+
->with($choices,'count')
73+
->will($this->returnValue(0));
74+
75+
$this->assertEquals(count($choices), $this->factory->createListFromChoices($choices,'count'));
76+
}
77+
6678
public function testCreateFromLoaderPropertyPath()
6779
{
6880
$loader = $this->getMock('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface');

0 commit comments

Comments
 (0)