minor #30953 [Form] Refactor DefaultChoiceListFactory (vudaltsov)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Form] Refactor DefaultChoiceListFactory

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

A small optimization (remove unnecessary `foreach` calls) and a naming improvement.

ping @HeahDude , @stof

#EUFOSSA

Commits
-------

77424c8557 Refactor DefaultChoiceListFactory
This commit is contained in:
Fabien Potencier 2019-04-07 11:26:33 +02:00
commit 76906ab6e8

View File

@ -70,7 +70,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
// If the callable returns null, the choice is not added to any group
if (\is_callable($groupBy)) {
foreach ($choices as $value => $choice) {
self::addChoiceViewGroupedBy(
self::addChoiceViewsGroupedByCallable(
$groupBy,
$choice,
(string) $value,
@ -83,9 +83,23 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
$otherViews
);
}
// Remove empty group views that may have been created by
// addChoiceViewsGroupedByCallable()
foreach ($preferredViews as $key => $view) {
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
unset($preferredViews[$key]);
}
}
foreach ($otherViews as $key => $view) {
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
unset($otherViews[$key]);
}
}
} else {
// Otherwise use the original structure of the choices
self::addChoiceViewsGroupedBy(
self::addChoiceViewsFromStructuredValues(
$list->getStructuredValues(),
$label,
$choices,
@ -98,20 +112,6 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
);
}
// Remove any empty group view that may have been created by
// addChoiceViewGroupedBy()
foreach ($preferredViews as $key => $view) {
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
unset($preferredViews[$key]);
}
}
foreach ($otherViews as $key => $view) {
if ($view instanceof ChoiceGroupView && 0 === \count($view->choices)) {
unset($otherViews[$key]);
}
}
return new ChoiceListView($otherViews, $preferredViews);
}
@ -150,9 +150,9 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
}
}
private static function addChoiceViewsGroupedBy($groupBy, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
{
foreach ($groupBy as $key => $value) {
foreach ($values as $key => $value) {
if (null === $value) {
continue;
}
@ -162,7 +162,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
$preferredViewsForGroup = [];
$otherViewsForGroup = [];
self::addChoiceViewsGroupedBy(
self::addChoiceViewsFromStructuredValues(
$value,
$label,
$choices,
@ -200,7 +200,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
}
}
private static function addChoiceViewGroupedBy($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$otherViews)
{
$groupLabels = $groupBy($choice, $keys[$value], $value);