Skip to content

Commit 8337184

Browse files
Merge branch '4.2'
* 4.2: (27 commits) [VarExporter] dont call userland code with uninitialized objects Fix typos in doc blocks [Debug] ignore underscore vs backslash namespaces in DebugClassLoader [TwigBridge][Form] Prevent multiple rendering of form collection prototypes [FrameworkBundle] fix describing routes with no controllers [DI] move RegisterServiceSubscribersPass before DecoratorServicePass Update ValidationListener.php [Yaml] ensures that the mb_internal_encoding is reset to its initial value [Messenger] Restore message handlers laziness [WebLink] Fixed documentation link [Security] getTargetPath of TargetPathTrait must return string or null [Hackday][Serializer] Deserialization ignores argument type hint from phpdoc for array in constructor argument Optimize perf by replacing call_user_func with dynamic vars [Cache] Fix dsn parsing [Routing] fix dumping same-path routes with placeholders [WebProfilerBundle][TwigBundle] CSS fixes Add a docblock for FormFactoryInterface [Security] defer log message in guard authenticator [Validator] Added IBAN format for Vatican City State merge conflicts ...
2 parents ac90a26 + fb2378c commit 8337184

26 files changed

+89
-54
lines changed

CallbackTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(callable $transform, callable $reverseTransform)
4141
*/
4242
public function transform($data)
4343
{
44-
return \call_user_func($this->transform, $data);
44+
return ($this->transform)($data);
4545
}
4646

4747
/**
@@ -57,6 +57,6 @@ public function transform($data)
5757
*/
5858
public function reverseTransform($data)
5959
{
60-
return \call_user_func($this->reverseTransform, $data);
60+
return ($this->reverseTransform)($data);
6161
}
6262
}

ChoiceList/ArrayChoiceList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function getValuesForChoices(array $choices)
155155
$givenValues = array();
156156

157157
foreach ($choices as $i => $givenChoice) {
158-
$givenValues[$i] = (string) \call_user_func($this->valueCallback, $givenChoice);
158+
$givenValues[$i] = (string) ($this->valueCallback)($givenChoice);
159159
}
160160

161161
return array_intersect($givenValues, array_keys($this->choices));
@@ -202,7 +202,7 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$keysByVa
202202
continue;
203203
}
204204

205-
$choiceValue = (string) \call_user_func($value, $choice);
205+
$choiceValue = (string) $value($choice);
206206
$choicesByValues[$choiceValue] = $choice;
207207
$keysByValues[$choiceValue] = $key;
208208
$structuredValues[$key] = $choiceValue;

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
118118
// $value may be an integer or a string, since it's stored in the array
119119
// keys. We want to guarantee it's a string though.
120120
$key = $keys[$value];
121-
$nextIndex = \is_int($index) ? $index++ : \call_user_func($index, $choice, $key, $value);
121+
$nextIndex = \is_int($index) ? $index++ : $index($choice, $key, $value);
122122

123123
// BC normalize label to accept a false value
124124
if (null === $label) {
@@ -127,7 +127,7 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
127127
} elseif (false !== $label) {
128128
// If "choice_label" is set to false and "expanded" is true, the value false
129129
// should be passed on to the "label" option of the checkboxes/radio buttons
130-
$dynamicLabel = \call_user_func($label, $choice, $key, $value);
130+
$dynamicLabel = $label($choice, $key, $value);
131131
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
132132
}
133133

@@ -137,11 +137,11 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
137137
$label,
138138
// The attributes may be a callable or a mapping from choice indices
139139
// to nested arrays
140-
\is_callable($attr) ? \call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
140+
\is_callable($attr) ? $attr($choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
141141
);
142142

143143
// $isPreferred may be null if no choices are preferred
144-
if ($isPreferred && \call_user_func($isPreferred, $choice, $key, $value)) {
144+
if ($isPreferred && $isPreferred($choice, $key, $value)) {
145145
$preferredViews[$nextIndex] = $view;
146146
} else {
147147
$otherViews[$nextIndex] = $view;
@@ -200,7 +200,7 @@ private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $key
200200

201201
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
202202
{
203-
$groupLabel = \call_user_func($groupBy, $choice, $keys[$value], $value);
203+
$groupLabel = $groupBy($choice, $keys[$value], $value);
204204

205205
if (null === $groupLabel) {
206206
// If the callable returns null, don't group the choice

ChoiceList/Loader/CallbackChoiceLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function loadChoiceList($value = null)
4646
return $this->choiceList;
4747
}
4848

49-
return $this->choiceList = new ArrayChoiceList(\call_user_func($this->callback), $value);
49+
return $this->choiceList = new ArrayChoiceList(($this->callback)(), $value);
5050
}
5151

5252
/**

ChoiceList/Loader/IntlCallbackChoiceLoader.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public function loadChoicesForValues(array $values, $value = null)
3030
return array();
3131
}
3232

33-
// If no callable is set, values are the same as choices
34-
if (null === $value) {
35-
return $values;
36-
}
37-
3833
return $this->loadChoiceList($value)->getChoicesForValues($values);
3934
}
4035

Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function onSubmit(FormEvent $event)
135135
/** @var FormInterface $child */
136136
foreach ($form as $name => $child) {
137137
$isNew = !isset($previousData[$name]);
138-
$isEmpty = \is_callable($this->deleteEmpty) ? \call_user_func($this->deleteEmpty, $child->getData()) : $child->isEmpty();
138+
$isEmpty = \is_callable($this->deleteEmpty) ? ($this->deleteEmpty)($child->getData()) : $child->isEmpty();
139139

140140
// $isNew can only be true if allowAdd is true, so we don't
141141
// need to check allowAdd again

Extension/Core/Type/CountryType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ public function loadChoicesForValues(array $values, $value = null)
106106
return array();
107107
}
108108

109-
// If no callable is set, values are the same as choices
110-
if (null === $value) {
111-
return $values;
112-
}
113-
114109
return $this->loadChoiceList($value)->getChoicesForValues($values);
115110
}
116111

Extension/Core/Type/CurrencyType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ public function loadChoicesForValues(array $values, $value = null)
106106
return array();
107107
}
108108

109-
// If no callable is set, values are the same as choices
110-
if (null === $value) {
111-
return $values;
112-
}
113-
114109
return $this->loadChoiceList($value)->getChoicesForValues($values);
115110
}
116111

Extension/Core/Type/LanguageType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ public function loadChoicesForValues(array $values, $value = null)
106106
return array();
107107
}
108108

109-
// If no callable is set, values are the same as choices
110-
if (null === $value) {
111-
return $values;
112-
}
113-
114109
return $this->loadChoiceList($value)->getChoicesForValues($values);
115110
}
116111

Extension/Core/Type/LocaleType.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ public function loadChoicesForValues(array $values, $value = null)
106106
return array();
107107
}
108108

109-
// If no callable is set, values are the same as choices
110-
if (null === $value) {
111-
return $values;
112-
}
113-
114109
return $this->loadChoiceList($value)->getChoicesForValues($values);
115110
}
116111

Extension/HttpFoundation/HttpFoundationRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function handleRequest(FormInterface $form, $request = null)
7373
$form->submit(null, false);
7474

7575
$form->addError(new FormError(
76-
\call_user_func($form->getConfig()->getOption('upload_max_size_message')),
76+
$form->getConfig()->getOption('upload_max_size_message')(),
7777
null,
7878
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
7979
));

Extension/Validator/Constraints/FormValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private static function getValidationGroups(FormInterface $form)
180180
private static function resolveValidationGroups($groups, FormInterface $form)
181181
{
182182
if (!\is_string($groups) && \is_callable($groups)) {
183-
$groups = \call_user_func($groups, $form);
183+
$groups = $groups($form);
184184
}
185185

186186
if ($groups instanceof GroupSequence) {

Extension/Validator/EventListener/ValidationListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function validateForm(FormEvent $event)
5151
$form = $event->getForm();
5252

5353
if ($form->isRoot()) {
54-
// Validate the form in group "Default"
54+
// Form groups are validated internally (FormValidator). Here we don't set groups as they are retrieved into the validator.
5555
foreach ($this->validator->validate($form) as $violation) {
5656
// Allow the "invalid" constraint to be put onto
5757
// non-synchronized forms

Extension/Validator/Type/UploadValidatorExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function configureOptions(OptionsResolver $resolver)
4848
$translationDomain = $this->translationDomain;
4949
$resolver->setNormalizer('upload_max_size_message', function (Options $options, $message) use ($translator, $translationDomain) {
5050
return function () use ($translator, $translationDomain, $message) {
51-
return $translator->trans(\call_user_func($message), array(), $translationDomain);
51+
return $translator->trans($message(), array(), $translationDomain);
5252
};
5353
});
5454
}

Form.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,12 @@ public function submit($submittedData, $clearMissing = true)
532532
$submittedData = null;
533533
} elseif (is_scalar($submittedData)) {
534534
$submittedData = (string) $submittedData;
535-
} elseif ($this->config->getOption('allow_file_upload')) {
536-
// no-op
537-
} elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) {
535+
} elseif (!$this->config->getOption('allow_file_upload') && $this->config->getRequestHandler()->isFileUpload($submittedData)) {
538536
$submittedData = null;
539537
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
538+
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
539+
$submittedData = null;
540+
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');
540541
}
541542

542543
$dispatcher = $this->config->getEventDispatcher();

FormFactoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Form;
1313

1414
/**
15+
* Allows creating a form based on a name, a class or a property.
16+
*
1517
* @author Bernhard Schussek <[email protected]>
1618
*/
1719
interface FormFactoryInterface

NativeRequestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function handleRequest(FormInterface $form, $request = null)
7878
$form->submit(null, false);
7979

8080
$form->addError(new FormError(
81-
\call_user_func($form->getConfig()->getOption('upload_max_size_message')),
81+
$form->getConfig()->getOption('upload_max_size_message')(),
8282
null,
8383
array('{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize())
8484
));

Tests/ChoiceList/Loader/IntlCallbackChoiceLoaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
8484
);
8585
}
8686

87+
public function testLoadChoicesForValuesDropsNonExistentChoices()
88+
{
89+
$this->assertSame(array(), self::$loader->loadChoicesForValues(array('foo')));
90+
}
91+
8792
public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
8893
{
8994
$this->assertSame(

Tests/CompoundFormTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,22 @@ public function testDisabledButtonIsNotSubmitted()
10811081
$this->assertFalse($submit->isSubmitted());
10821082
}
10831083

1084+
public function testArrayTransformationFailureOnSubmit()
1085+
{
1086+
$this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
1087+
$this->form->add($this->getBuilder('bar', null, null, array('multiple' => false))->setCompound(false)->getForm());
1088+
1089+
$this->form->submit(array(
1090+
'foo' => array('foo'),
1091+
'bar' => array('bar'),
1092+
));
1093+
1094+
$this->assertNull($this->form->get('foo')->getData());
1095+
$this->assertSame('Submitted data was expected to be text or number, array given.', $this->form->get('foo')->getTransformationFailure()->getMessage());
1096+
1097+
$this->assertSame(array('bar'), $this->form->get('bar')->getData());
1098+
}
1099+
10841100
public function testFileUpload()
10851101
{
10861102
$reqHandler = new HttpFoundationRequestHandler();

Tests/Extension/Core/Type/CountryTypeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
15+
use Symfony\Component\Form\Extension\Core\Type\CountryType;
1516
use Symfony\Component\Intl\Util\IntlTestHelper;
1617

1718
class CountryTypeTest extends BaseTypeTest
@@ -80,4 +81,14 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'FR', $expectedD
8081
{
8182
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
8283
}
84+
85+
/**
86+
* @group legacy
87+
*/
88+
public function testInvalidChoiceValuesAreDropped()
89+
{
90+
$type = new CountryType();
91+
92+
$this->assertSame(array(), $type->loadChoicesForValues(array('foo')));
93+
}
8394
}

Tests/Extension/Core/Type/CurrencyTypeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
15+
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
1516
use Symfony\Component\Intl\Util\IntlTestHelper;
1617

1718
class CurrencyTypeTest extends BaseTypeTest
@@ -61,4 +62,14 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'EUR', $expected
6162
{
6263
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
6364
}
65+
66+
/**
67+
* @group legacy
68+
*/
69+
public function testInvalidChoiceValuesAreDropped()
70+
{
71+
$type = new CurrencyType();
72+
73+
$this->assertSame(array(), $type->loadChoicesForValues(array('foo')));
74+
}
6475
}

Tests/Extension/Core/Type/LanguageTypeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
15+
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
1516
use Symfony\Component\Intl\Util\IntlTestHelper;
1617

1718
class LanguageTypeTest extends BaseTypeTest
@@ -73,4 +74,14 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedD
7374
{
7475
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
7576
}
77+
78+
/**
79+
* @group legacy
80+
*/
81+
public function testInvalidChoiceValuesAreDropped()
82+
{
83+
$type = new LanguageType();
84+
85+
$this->assertSame(array(), $type->loadChoicesForValues(array('foo')));
86+
}
7687
}

Tests/Extension/Core/Type/LocaleTypeTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
15+
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
1516
use Symfony\Component\Intl\Util\IntlTestHelper;
1617

1718
class LocaleTypeTest extends BaseTypeTest
@@ -61,4 +62,14 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedD
6162
{
6263
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
6364
}
65+
66+
/**
67+
* @group legacy
68+
*/
69+
public function testInvalidChoiceValuesAreDropped()
70+
{
71+
$type = new LocaleType();
72+
73+
$this->assertSame(array(), $type->loadChoicesForValues(array('foo')));
74+
}
6475
}

Tests/Extension/Core/Type/UrlTypeTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ public function testThrowExceptionIfDefaultProtocolIsInvalid()
8383
));
8484
}
8585

86-
public function testSubmitWithNonStringDataDoesNotBreakTheFixUrlProtocolListener()
87-
{
88-
$form = $this->factory->create(static::TESTED_TYPE);
89-
$form->submit(array('domain.com', 'www.domain.com'));
90-
91-
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
92-
}
93-
9486
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
9587
{
9688
$form = $this->factory->create(static::TESTED_TYPE, null, array(

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function testDontValidateConstraintsIfNoValidationGroups()
220220
->getForm();
221221

222222
// Launch transformer
223-
$form->submit(array());
223+
$form->submit('foo');
224224

225225
$this->expectNoValidate();
226226

Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function testPostMaxSizeTranslation()
4141
$extension->configureOptions($resolver);
4242
$options = $resolver->resolve();
4343

44-
$this->assertEquals('translated max {{ max }}!', \call_user_func($options['upload_max_size_message']));
44+
$this->assertEquals('translated max {{ max }}!', $options['upload_max_size_message']());
4545
}
4646
}

0 commit comments

Comments
 (0)