Skip to content

Commit

Permalink
minor #33132 [Form] Add type declarations to private DefaultChoiceLis…
Browse files Browse the repository at this point in the history
…tFactory 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
-------

9fc6ba6 Add type declarations to private DefaultChoiceListFactory methods
  • Loading branch information
nicolas-grekas committed Aug 13, 2019
2 parents 3ec9106 + 9fc6ba6 commit 046aff2
Showing 1 changed file with 15 additions and 11 deletions.
Expand Up @@ -53,12 +53,16 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
$choices = $list->getChoices();
$keys = $list->getOriginalKeys();

if (!\is_callable($preferredChoices) && !empty($preferredChoices)) {
// make sure we have keys that reflect order
$preferredChoices = array_values($preferredChoices);
$preferredChoices = static function ($choice) use ($preferredChoices) {
return array_search($choice, $preferredChoices, true);
};
if (!\is_callable($preferredChoices)) {
if (empty($preferredChoices)) {
$preferredChoices = null;
} else {
// 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
Expand All @@ -76,7 +80,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
self::addChoiceViewsGroupedByCallable(
$groupBy,
$choice,
(string) $value,
$value,
$label,
$keys,
$index,
Expand Down Expand Up @@ -126,7 +130,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
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
// keys. We want to guarantee it's a string though.
Expand Down Expand Up @@ -154,15 +158,15 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
);

// $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;
$preferredViewsOrder[$nextIndex] = $preferredKey;
}

$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) {
if (null === $value) {
Expand Down Expand Up @@ -214,7 +218,7 @@ private static function addChoiceViewsFromStructuredValues($values, $label, $cho
}
}

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);

Expand Down

0 comments on commit 046aff2

Please sign in to comment.