Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8add405

Browse filesBrowse files
committed
[Form] Removed useless code
1 parent 7ba5f7a commit 8add405
Copy full SHA for 8add405

File tree

3 files changed

+46
-73
lines changed
Filter options

3 files changed

+46
-73
lines changed

‎src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
+6-30Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -982,37 +982,13 @@ public function testLoaderCaching()
982982
'property3' => 2,
983983
));
984984

985-
$choiceList1 = $form->get('property1')->getConfig()->getOption('choice_list');
986-
$choiceList2 = $form->get('property2')->getConfig()->getOption('choice_list');
987-
$choiceList3 = $form->get('property3')->getConfig()->getOption('choice_list');
985+
$choiceLoader1 = $form->get('property1')->getConfig()->getOption('choice_loader');
986+
$choiceLoader2 = $form->get('property2')->getConfig()->getOption('choice_loader');
987+
$choiceLoader3 = $form->get('property3')->getConfig()->getOption('choice_loader');
988988

989-
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\ChoiceListInterface', $choiceList1);
990-
$this->assertSame($choiceList1, $choiceList2);
991-
$this->assertSame($choiceList1, $choiceList3);
992-
}
993-
994-
public function testCacheChoiceLists()
995-
{
996-
$entity1 = new SingleIntIdEntity(1, 'Foo');
997-
998-
$this->persist(array($entity1));
999-
1000-
$field1 = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
1001-
'em' => 'default',
1002-
'class' => self::SINGLE_IDENT_CLASS,
1003-
'required' => false,
1004-
'choice_label' => 'name',
1005-
));
1006-
1007-
$field2 = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
1008-
'em' => 'default',
1009-
'class' => self::SINGLE_IDENT_CLASS,
1010-
'required' => false,
1011-
'choice_label' => 'name',
1012-
));
1013-
1014-
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\ChoiceListInterface', $field1->getConfig()->getOption('choice_list'));
1015-
$this->assertSame($field1->getConfig()->getOption('choice_list'), $field2->getConfig()->getOption('choice_list'));
989+
$this->assertInstanceOf('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface', $choiceLoader1);
990+
$this->assertSame($choiceLoader1, $choiceLoader2);
991+
$this->assertSame($choiceLoader1, $choiceLoader3);
1016992
}
1017993

1018994
protected function createRegistryMock($name, $em)

‎src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+40-33Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
5252
*/
5353
public function buildForm(FormBuilderInterface $builder, array $options)
5454
{
55+
$choiceList = $this->createChoiceList($options);
56+
$builder->setAttribute('choice_list', $choiceList);
57+
5558
if ($options['expanded']) {
5659
$builder->setDataMapper($options['multiple']
57-
? new CheckboxListMapper($options['choice_list'])
58-
: new RadioListMapper($options['choice_list']));
60+
? new CheckboxListMapper($choiceList)
61+
: new RadioListMapper($choiceList));
5962

6063
// Initialize all choices before doing the index check below.
6164
// This helps in cases where index checks are optimized for non
@@ -64,12 +67,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6467
// requires another SQL query. When the initialization is done first,
6568
// one SQL query is sufficient.
6669

67-
$choiceListView = $this->createChoiceListView($options['choice_list'], $options);
70+
$choiceListView = $this->createChoiceListView($choiceList, $options);
6871
$builder->setAttribute('choice_list_view', $choiceListView);
6972

7073
// Check if the choices already contain the empty value
7174
// Only add the placeholder option if this is not the case
72-
if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
75+
if (null !== $options['placeholder'] && 0 === count($choiceList->getChoicesForValues(array('')))) {
7376
$placeholderView = new ChoiceView(null, '', $options['placeholder']);
7477

7578
// "placeholder" is a reserved name
@@ -139,10 +142,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
139142
}
140143
} elseif ($options['multiple']) {
141144
// <select> tag with "multiple" option
142-
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list']));
145+
$builder->addViewTransformer(new ChoicesToValuesTransformer($choiceList));
143146
} else {
144147
// <select> tag without "multiple" option
145-
$builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list']));
148+
$builder->addViewTransformer(new ChoiceToValueTransformer($choiceList));
146149
}
147150

148151
if ($options['multiple'] && $options['by_reference']) {
@@ -162,10 +165,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
162165
$choiceTranslationDomain = $view->vars['translation_domain'];
163166
}
164167

168+
/** @var ChoiceListInterface $choiceList */
169+
$choiceList = $form->getConfig()->getAttribute('choice_list');
170+
165171
/** @var ChoiceListView $choiceListView */
166172
$choiceListView = $form->getConfig()->hasAttribute('choice_list_view')
167173
? $form->getConfig()->getAttribute('choice_list_view')
168-
: $this->createChoiceListView($options['choice_list'], $options);
174+
: $this->createChoiceListView($choiceList, $options);
169175

170176
$view->vars = array_replace($view->vars, array(
171177
'multiple' => $options['multiple'],
@@ -192,7 +198,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
192198
}
193199

194200
// Check if the choices already contain the empty value
195-
$view->vars['placeholder_in_choices'] = 0 !== count($options['choice_list']->getChoicesForValues(array('')));
201+
$view->vars['placeholder_in_choices'] = 0 !== count($choiceList->getChoicesForValues(array('')));
196202

197203
// Only add the empty value option if this is not the case
198204
if (null !== $options['placeholder'] && !$view->vars['placeholder_in_choices']) {
@@ -235,8 +241,6 @@ public function finishView(FormView $view, FormInterface $form, array $options)
235241
*/
236242
public function configureOptions(OptionsResolver $resolver)
237243
{
238-
$choiceListFactory = $this->choiceListFactory;
239-
240244
$emptyData = function (Options $options) {
241245
if ($options['multiple'] || $options['expanded']) {
242246
return array();
@@ -249,29 +253,20 @@ public function configureOptions(OptionsResolver $resolver)
249253
return $options['required'] ? null : '';
250254
};
251255

252-
$choiceListNormalizer = function (Options $options) use ($choiceListFactory) {
253-
if (null !== $options['choice_loader']) {
254-
return $choiceListFactory->createListFromLoader(
255-
$options['choice_loader'],
256-
$options['choice_value']
257-
);
256+
$choicesAsValuesNormalizer = function (Options $options, $choicesAsValues) {
257+
// Not set by the user
258+
if (null === $choicesAsValues) {
259+
return true;
258260
}
259261

260-
// Harden against NULL values (like in EntityType and ModelType)
261-
$choices = null !== $options['choices'] ? $options['choices'] : array();
262-
263-
return $choiceListFactory->createListFromChoices($choices, $options['choice_value']);
264-
};
265-
266-
$choicesAsValuesNormalizer = function (Options $options, $choicesAsValues) {
267-
if (null !== $choicesAsValues) {
268-
if (true !== $choicesAsValues) {
269-
throw new \RuntimeException('The "choices_as_values" option should not be used. Remove it and flip the contents of the "choices" option instead.');
270-
}
271-
// To be uncommented in 3.1
272-
//@trigger_error('The "choices_as_values" option is deprecated since version 3.1 and will be removed in 4.0. You should not use it anymore.', E_USER_DEPRECATED);
262+
// Set by the user
263+
if (true !== $choicesAsValues) {
264+
throw new \RuntimeException('The "choices_as_values" option should not be used. Remove it and flip the contents of the "choices" option instead.');
273265
}
274266

267+
// To be uncommented in 3.1
268+
//@trigger_error('The "choices_as_values" option is deprecated since version 3.1 and will be removed in 4.0. You should not use it anymore.', E_USER_DEPRECATED);
269+
275270
return true;
276271
};
277272

@@ -306,9 +301,8 @@ public function configureOptions(OptionsResolver $resolver)
306301
$resolver->setDefaults(array(
307302
'multiple' => false,
308303
'expanded' => false,
309-
'choice_list' => null, // deprecated
310304
'choices' => array(),
311-
'choices_as_values' => null, // to be deprecated in 3.1
305+
'choices_as_values' => null, // to be deprecated in 3.1
312306
'choice_loader' => null,
313307
'choice_label' => null,
314308
'choice_name' => null,
@@ -327,12 +321,10 @@ public function configureOptions(OptionsResolver $resolver)
327321
'choice_translation_domain' => true,
328322
));
329323

330-
$resolver->setNormalizer('choice_list', $choiceListNormalizer);
331324
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
332325
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
333326
$resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer);
334327

335-
$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface'));
336328
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
337329
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
338330
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
@@ -422,6 +414,21 @@ private function addSubForm(FormBuilderInterface $builder, $name, ChoiceView $ch
422414
$builder->add($name, $choiceType, $choiceOpts);
423415
}
424416

417+
private function createChoiceList(array $options)
418+
{
419+
if (null !== $options['choice_loader']) {
420+
return $this->choiceListFactory->createListFromLoader(
421+
$options['choice_loader'],
422+
$options['choice_value']
423+
);
424+
}
425+
426+
// Harden against NULL values (like in EntityType and ModelType)
427+
$choices = null !== $options['choices'] ? $options['choices'] : array();
428+
429+
return $this->choiceListFactory->createListFromChoices($choices, $options['choice_value']);
430+
}
431+
425432
private function createChoiceListView(ChoiceListInterface $choiceList, array $options)
426433
{
427434
// If no explicit grouping information is given, use the structural

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php
-10Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ public function testChoicesOptionExpectsArrayOrTraversable()
6868
));
6969
}
7070

71-
/**
72-
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
73-
*/
74-
public function testChoiceListOptionExpectsChoiceListInterface()
75-
{
76-
$this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
77-
'choice_list' => array('foo' => 'foo'),
78-
));
79-
}
80-
8171
/**
8272
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
8373
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.