Skip to content

Commit e7bb106

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: (43 commits) [AssetMapper] Fix entrypoint scripts are not preloaded Fix typo in method resolvePackages Make FormPerformanceTestCase compatible with PHPUnit 10 Avoid calling getInvocationCount() [AssetMapper] Always downloading vendor files [Security] Fix resetting traceable listeners [HttpClient] Fix type error with http_version 1.1 [DependencyInjection] Add tests for `AutowireLocator`/`AutowireIterator` [DependencyInjection] Add `#[AutowireIterator]` attribute and improve `#[AutowireLocator]` Update documentation link Fix typo that causes unit test to fail Fix CS [AssetMapper] Add audit command [Mailer] Use idn encoded address otherwise Brevo throws an error [Messenger] Resend failed retries back to failure transport [FrameworkBundle] Fix call to invalid method in NotificationAssertionsTrait [Validator] Add missing italian translations [Notifier] Fix failing testcase Fix order array sum normalizedData and nestedData Add test for 0 and '0' in PeriodicalTrigger Fix '0' case error and remove duplicate code ...
2 parents 76ba7d2 + 4fa0d87 commit e7bb106

File tree

10 files changed

+103
-29
lines changed

10 files changed

+103
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ CHANGELOG
1616
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`
1717
* Deprecate `PostSetDataEvent::setData()`, use `PreSetDataEvent::setData()` instead
1818
* Deprecate `PostSubmitEvent::setData()`, use `PreSubmitDataEvent::setData()` or `SubmitDataEvent::setData()` instead
19+
* Add `duplicate_preferred_choices` option in `ChoiceType`
20+
* Add `$duplicatePreferredChoices` parameter to `ChoiceListFactoryInterface::createView()`
1921

2022
6.3
2123
---

ChoiceList/Factory/CachingFactoryDecorator.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, mixed $value
145145
return $this->lists[$hash];
146146
}
147147

148-
public function createView(ChoiceListInterface $list, mixed $preferredChoices = null, mixed $label = null, mixed $index = null, mixed $groupBy = null, mixed $attr = null, mixed $labelTranslationParameters = []): ChoiceListView
148+
/**
149+
* @param bool $duplicatePreferredChoices
150+
*/
151+
public function createView(ChoiceListInterface $list, mixed $preferredChoices = null, mixed $label = null, mixed $index = null, mixed $groupBy = null, mixed $attr = null, mixed $labelTranslationParameters = []/* , bool $duplicatePreferredChoices = true */): ChoiceListView
149152
{
153+
$duplicatePreferredChoices = \func_num_args() > 7 ? func_get_arg(7) : true;
150154
$cache = true;
151155

152156
if ($preferredChoices instanceof Cache\PreferredChoice) {
@@ -193,11 +197,12 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
193197
$index,
194198
$groupBy,
195199
$attr,
196-
$labelTranslationParameters
200+
$labelTranslationParameters,
201+
$duplicatePreferredChoices,
197202
);
198203
}
199204

200-
$hash = self::generateHash([$list, $preferredChoices, $label, $index, $groupBy, $attr, $labelTranslationParameters]);
205+
$hash = self::generateHash([$list, $preferredChoices, $label, $index, $groupBy, $attr, $labelTranslationParameters, $duplicatePreferredChoices]);
201206

202207
if (!isset($this->views[$hash])) {
203208
$this->views[$hash] = $this->decoratedFactory->createView(
@@ -207,7 +212,8 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
207212
$index,
208213
$groupBy,
209214
$attr,
210-
$labelTranslationParameters
215+
$labelTranslationParameters,
216+
$duplicatePreferredChoices,
211217
);
212218
}
213219

ChoiceList/Factory/ChoiceListFactoryInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
7777
* pass false to discard the label
7878
* @param array|callable|null $attr The callable generating the HTML attributes
7979
* @param array|callable $labelTranslationParameters The parameters used to translate the choice labels
80+
* @param bool $duplicatePreferredChoices Whether the preferred choices should be duplicated
81+
* on top of the list and in their original position
82+
* or only in the top of the list
8083
*/
81-
public function createView(ChoiceListInterface $list, array|callable $preferredChoices = null, callable|false $label = null, callable $index = null, callable $groupBy = null, array|callable $attr = null, array|callable $labelTranslationParameters = []): ChoiceListView;
84+
public function createView(ChoiceListInterface $list, array|callable $preferredChoices = null, callable|false $label = null, callable $index = null, callable $groupBy = null, array|callable $attr = null, array|callable $labelTranslationParameters = []/* , bool $duplicatePreferredChoices = true */): ChoiceListView;
8285
}

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, callable $va
5252
return new LazyChoiceList($loader, $value);
5353
}
5454

55-
public function createView(ChoiceListInterface $list, array|callable $preferredChoices = null, callable|false $label = null, callable $index = null, callable $groupBy = null, array|callable $attr = null, array|callable $labelTranslationParameters = []): ChoiceListView
55+
/**
56+
* @param bool $duplicatePreferredChoices
57+
*/
58+
public function createView(ChoiceListInterface $list, array|callable $preferredChoices = null, callable|false $label = null, callable $index = null, callable $groupBy = null, array|callable $attr = null, array|callable $labelTranslationParameters = []/* , bool $duplicatePreferredChoices = true */): ChoiceListView
5659
{
60+
$duplicatePreferredChoices = \func_num_args() > 7 ? func_get_arg(7) : true;
5761
$preferredViews = [];
5862
$preferredViewsOrder = [];
5963
$otherViews = [];
@@ -92,7 +96,8 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
9296
$preferredChoices,
9397
$preferredViews,
9498
$preferredViewsOrder,
95-
$otherViews
99+
$otherViews,
100+
$duplicatePreferredChoices,
96101
);
97102
}
98103

@@ -130,7 +135,8 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
130135
$preferredChoices,
131136
$preferredViews,
132137
$preferredViewsOrder,
133-
$otherViews
138+
$otherViews,
139+
$duplicatePreferredChoices,
134140
);
135141
}
136142

@@ -139,7 +145,7 @@ public function createView(ChoiceListInterface $list, array|callable $preferredC
139145
return new ChoiceListView($otherViews, $preferredViews);
140146
}
141147

142-
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews): void
148+
private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews, bool $duplicatePreferredChoices): void
143149
{
144150
// $value may be an integer or a string, since it's stored in the array
145151
// keys. We want to guarantee it's a string though.
@@ -180,12 +186,16 @@ private static function addChoiceView($choice, string $value, $label, array $key
180186
if (null !== $isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) {
181187
$preferredViews[$nextIndex] = $view;
182188
$preferredViewsOrder[$nextIndex] = $preferredKey;
183-
}
184189

185-
$otherViews[$nextIndex] = $view;
190+
if ($duplicatePreferredChoices) {
191+
$otherViews[$nextIndex] = $view;
192+
}
193+
} else {
194+
$otherViews[$nextIndex] = $view;
195+
}
186196
}
187197

188-
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews): void
198+
private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews, bool $duplicatePreferredChoices): void
189199
{
190200
foreach ($values as $key => $value) {
191201
if (null === $value) {
@@ -208,7 +218,8 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
208218
$isPreferred,
209219
$preferredViewsForGroup,
210220
$preferredViewsOrder,
211-
$otherViewsForGroup
221+
$otherViewsForGroup,
222+
$duplicatePreferredChoices,
212223
);
213224

214225
if (\count($preferredViewsForGroup) > 0) {
@@ -234,12 +245,13 @@ private static function addChoiceViewsFromStructuredValues(array $values, $label
234245
$isPreferred,
235246
$preferredViews,
236247
$preferredViewsOrder,
237-
$otherViews
248+
$otherViews,
249+
$duplicatePreferredChoices,
238250
);
239251
}
240252
}
241253

242-
private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews): void
254+
private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, $labelTranslationParameters, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews, bool $duplicatePreferredChoices): void
243255
{
244256
$groupLabels = $groupBy($choice, $keys[$value], $value);
245257

@@ -256,7 +268,8 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
256268
$isPreferred,
257269
$preferredViews,
258270
$preferredViewsOrder,
259-
$otherViews
271+
$otherViews,
272+
$duplicatePreferredChoices,
260273
);
261274

262275
return;
@@ -286,7 +299,8 @@ private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choi
286299
$isPreferred,
287300
$preferredViews[$groupLabel]->choices,
288301
$preferredViewsOrder[$groupLabel],
289-
$otherViews[$groupLabel]->choices
302+
$otherViews[$groupLabel]->choices,
303+
$duplicatePreferredChoices,
290304
);
291305
}
292306
}

ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, mixed $value
109109
return $this->decoratedFactory->createListFromLoader($loader, $value, $filter);
110110
}
111111

112-
public function createView(ChoiceListInterface $list, mixed $preferredChoices = null, mixed $label = null, mixed $index = null, mixed $groupBy = null, mixed $attr = null, mixed $labelTranslationParameters = []): ChoiceListView
112+
/**
113+
* @param bool $duplicatePreferredChoices
114+
*/
115+
public function createView(ChoiceListInterface $list, mixed $preferredChoices = null, mixed $label = null, mixed $index = null, mixed $groupBy = null, mixed $attr = null, mixed $labelTranslationParameters = []/* , bool $duplicatePreferredChoices = true */): ChoiceListView
113116
{
117+
$duplicatePreferredChoices = \func_num_args() > 7 ? func_get_arg(7) : true;
114118
$accessor = $this->propertyAccessor;
115119

116120
if (\is_string($label)) {
@@ -182,7 +186,8 @@ public function createView(ChoiceListInterface $list, mixed $preferredChoices =
182186
$index,
183187
$groupBy,
184188
$attr,
185-
$labelTranslationParameters
189+
$labelTranslationParameters,
190+
$duplicatePreferredChoices,
186191
);
187192
}
188193
}

Extension/Core/Type/ChoiceType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ public function configureOptions(OptionsResolver $resolver): void
342342
'choice_attr' => null,
343343
'choice_translation_parameters' => [],
344344
'preferred_choices' => [],
345+
'duplicate_preferred_choices' => true,
345346
'group_by' => null,
346347
'empty_data' => $emptyData,
347348
'placeholder' => $placeholderDefault,
@@ -371,6 +372,7 @@ public function configureOptions(OptionsResolver $resolver): void
371372
$resolver->setAllowedTypes('choice_translation_parameters', ['null', 'array', 'callable', ChoiceTranslationParameters::class]);
372373
$resolver->setAllowedTypes('placeholder_attr', ['array']);
373374
$resolver->setAllowedTypes('preferred_choices', ['array', \Traversable::class, 'callable', 'string', PropertyPath::class, PreferredChoice::class]);
375+
$resolver->setAllowedTypes('duplicate_preferred_choices', 'bool');
374376
$resolver->setAllowedTypes('group_by', ['null', 'callable', 'string', PropertyPath::class, GroupBy::class]);
375377
}
376378

@@ -453,7 +455,8 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
453455
$options['choice_name'],
454456
$options['group_by'],
455457
$options['choice_attr'],
456-
$options['choice_translation_parameters']
458+
$options['choice_translation_parameters'],
459+
$options['duplicate_preferred_choices'],
457460
);
458461
}
459462
}

Test/FormPerformanceTestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Test;
1313

14+
use Symfony\Component\Form\Test\Traits\RunTestTrait;
1415
use Symfony\Component\Form\Tests\VersionAwareTest;
1516

1617
/**
@@ -23,19 +24,22 @@
2324
*/
2425
abstract class FormPerformanceTestCase extends FormIntegrationTestCase
2526
{
27+
use RunTestTrait;
2628
use VersionAwareTest;
2729

2830
protected int $maxRunningTime = 0;
2931

30-
protected function runTest()
32+
private function doRunTest(): mixed
3133
{
3234
$s = microtime(true);
33-
parent::runTest();
35+
$result = parent::runTest();
3436
$time = microtime(true) - $s;
3537

3638
if (0 != $this->maxRunningTime && $time > $this->maxRunningTime) {
3739
$this->fail(sprintf('expected running time: <= %s but was: %s', $this->maxRunningTime, $time));
3840
}
41+
42+
return $result;
3943
}
4044

4145
/**

Test/Traits/RunTestTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Test\Traits;
13+
14+
use PHPUnit\Framework\TestCase;
15+
16+
if ((new \ReflectionMethod(TestCase::class, 'runTest'))->hasReturnType()) {
17+
// PHPUnit 10
18+
/** @internal */
19+
trait RunTestTrait
20+
{
21+
protected function runTest(): mixed
22+
{
23+
return $this->doRunTest();
24+
}
25+
}
26+
} else {
27+
// PHPUnit 9
28+
/** @internal */
29+
trait RunTestTrait
30+
{
31+
protected function runTest()
32+
{
33+
return $this->doRunTest();
34+
}
35+
}
36+
}

Tests/Fixtures/Descriptor/resolved_form_type_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"choice_translation_parameters",
1313
"choice_value",
1414
"choices",
15+
"duplicate_preferred_choices",
1516
"expanded",
1617
"group_by",
1718
"multiple",

Tests/Fixtures/Descriptor/resolved_form_type_1.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
1414
choice_translation_parameters invalid_message auto_initialize csrf_token_manager
1515
choice_value trim block_name
1616
choices block_prefix
17-
expanded by_reference
18-
group_by data
19-
multiple disabled
20-
placeholder form_attr
21-
placeholder_attr getter
22-
preferred_choices help
23-
help_attr
17+
duplicate_preferred_choices by_reference
18+
expanded data
19+
group_by disabled
20+
multiple form_attr
21+
placeholder getter
22+
placeholder_attr help
23+
preferred_choices help_attr
2424
help_html
2525
help_translation_parameters
2626
inherit_data

0 commit comments

Comments
 (0)