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

Skip to content

Commit b070941

Browse files
author
hamza
committed
fix #17993 - Deprecated callable strings
1 parent 518a513 commit b070941

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ 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('createListFromChoices() - 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+
$value = function (array $choices) use ($value) {
92+
return $value($choices);
93+
};
94+
95+
return $value($choices);
8996
}
9097

9198
if ($value instanceof PropertyPath) {
@@ -117,6 +124,11 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
117124
{
118125
if (is_string($value) && !is_callable($value)) {
119126
$value = new PropertyPath($value);
127+
} elseif (is_string($value) && is_callable($value)) {
128+
@trigger_error('createListFromLoader() - 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);
129+
$value = function ($choices) use ($value) {
130+
return $value($choices);
131+
};
120132
}
121133

122134
if ($value instanceof PropertyPath) {
@@ -153,6 +165,13 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
153165

154166
if (is_string($label) && !is_callable($label)) {
155167
$label = new PropertyPath($label);
168+
} elseif (is_string($label) && is_callable($label)) {
169+
@trigger_error('createView() - $label - 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);
170+
171+
$label = function ($choice) use ($label) {
172+
return $label($choice);
173+
};
174+
156175
}
157176

158177
if ($label instanceof PropertyPath) {
@@ -163,6 +182,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
163182

164183
if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
165184
$preferredChoices = new PropertyPath($preferredChoices);
185+
} elseif (is_string($preferredChoices) && is_callable($preferredChoices)) {
186+
@trigger_error('createView() - $preferredChoices - 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);
187+
188+
$preferredChoices = function ($choice) use ($preferredChoices) {
189+
return $preferredChoices($choice);
190+
};
166191
}
167192

168193
if ($preferredChoices instanceof PropertyPath) {
@@ -178,6 +203,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
178203

179204
if (is_string($index) && !is_callable($index)) {
180205
$index = new PropertyPath($index);
206+
} elseif (is_string($index) && is_callable($index)) {
207+
@trigger_error('createView() - $index - 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);
208+
209+
$index = function ($choice) use ($index) {
210+
return $index($choice);
211+
};
181212
}
182213

183214
if ($index instanceof PropertyPath) {
@@ -188,6 +219,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
188219

189220
if (is_string($groupBy) && !is_callable($groupBy)) {
190221
$groupBy = new PropertyPath($groupBy);
222+
} elseif (is_string($groupBy) && is_callable($groupBy)) {
223+
@trigger_error('createView() - $groupBy - 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);
224+
225+
$groupBy = function ($choice) use ($groupBy) {
226+
return $groupBy($choice);
227+
};
191228
}
192229

193230
if ($groupBy instanceof PropertyPath) {
@@ -202,6 +239,12 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
202239

203240
if (is_string($attr) && !is_callable($attr)) {
204241
$attr = new PropertyPath($attr);
242+
} elseif (is_string($attr) && is_callable($attr)) {
243+
@trigger_error('createView() - $attr - 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);
244+
245+
$attr = function ($choice) use ($attr) {
246+
return $attr($choice);
247+
};
205248
}
206249

207250
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)