minor #33132 [Form] Add type declarations to private DefaultChoiceListFactory methods (vudaltsov)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] Add type declarations to private DefaultChoiceListFactory methods

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

These are the type declarations that can be safely added to private methods of the `DefaultChoiceListFactory` without breaking BC.

Commits
-------

9fc6ba66b3 Add type declarations to private DefaultChoiceListFactory methods
This commit is contained in:
Nicolas Grekas 2019-08-13 08:55:08 +02:00
commit 046aff2c04

View File

@ -53,12 +53,16 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
$choices = $list->getChoices(); $choices = $list->getChoices();
$keys = $list->getOriginalKeys(); $keys = $list->getOriginalKeys();
if (!\is_callable($preferredChoices) && !empty($preferredChoices)) { if (!\is_callable($preferredChoices)) {
// make sure we have keys that reflect order if (empty($preferredChoices)) {
$preferredChoices = array_values($preferredChoices); $preferredChoices = null;
$preferredChoices = static function ($choice) use ($preferredChoices) { } else {
return array_search($choice, $preferredChoices, true); // make sure we have keys that reflect order
}; $preferredChoices = array_values($preferredChoices);
$preferredChoices = static function ($choice) use ($preferredChoices) {
return array_search($choice, $preferredChoices, true);
};
}
} }
// The names are generated from an incrementing integer by default // The names are generated from an incrementing integer by default
@ -76,7 +80,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
self::addChoiceViewsGroupedByCallable( self::addChoiceViewsGroupedByCallable(
$groupBy, $groupBy,
$choice, $choice,
(string) $value, $value,
$label, $label,
$keys, $keys,
$index, $index,
@ -126,7 +130,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
return new ChoiceListView($otherViews, $preferredViews); return new ChoiceListView($otherViews, $preferredViews);
} }
private static function addChoiceView($choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) private static function addChoiceView($choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
{ {
// $value may be an integer or a string, since it's stored in the array // $value may be an integer or a string, since it's stored in the array
// keys. We want to guarantee it's a string though. // keys. We want to guarantee it's a string though.
@ -154,7 +158,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
); );
// $isPreferred may be null if no choices are preferred // $isPreferred may be null if no choices are preferred
if ($isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) { if (null !== $isPreferred && false !== $preferredKey = $isPreferred($choice, $key, $value)) {
$preferredViews[$nextIndex] = $view; $preferredViews[$nextIndex] = $view;
$preferredViewsOrder[$nextIndex] = $preferredKey; $preferredViewsOrder[$nextIndex] = $preferredKey;
} }
@ -162,7 +166,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
$otherViews[$nextIndex] = $view; $otherViews[$nextIndex] = $view;
} }
private static function addChoiceViewsFromStructuredValues($values, $label, $choices, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) private static function addChoiceViewsFromStructuredValues(array $values, $label, array $choices, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
{ {
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (null === $value) { if (null === $value) {
@ -214,7 +218,7 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface
} }
} }
private static function addChoiceViewsGroupedByCallable($groupBy, $choice, $value, $label, $keys, &$index, $attr, $isPreferred, &$preferredViews, &$preferredViewsOrder, &$otherViews) private static function addChoiceViewsGroupedByCallable(callable $groupBy, $choice, string $value, $label, array $keys, &$index, $attr, ?callable $isPreferred, array &$preferredViews, array &$preferredViewsOrder, array &$otherViews)
{ {
$groupLabels = $groupBy($choice, $keys[$value], $value); $groupLabels = $groupBy($choice, $keys[$value], $value);