Skip to content

Replace switch statement with match expression #17287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions bundles/prepend_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,16 @@ in case a specific other bundle is not registered::
// disable AcmeGoodbyeBundle in bundles
$config = ['use_acme_goodbye' => false];
foreach ($container->getExtensions() as $name => $extension) {
switch ($name) {
case 'acme_something':
case 'acme_other':
// set use_acme_goodbye to false in the config of
// acme_something and acme_other
//
// note that if the user manually configured
// use_acme_goodbye to true in config/services.yaml
// then the setting would in the end be true and not false
$container->prependExtensionConfig($name, $config);
break;
}
match ($name) {
// set use_acme_goodbye to false in the config of
// acme_something and acme_other
//
// note that if the user manually configured
// use_acme_goodbye to true in config/services.yaml
// then the setting would in the end be true and not false
'acme_something', 'acme_other' => $container->prependExtensionConfig($name, $config),
default => null
};
}
}

Expand Down
39 changes: 15 additions & 24 deletions form/type_guesser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,21 @@ With this knowledge, you can implement the ``guessType()`` method of the
}

// otherwise, base the type on the @var annotation
switch ($annotations['var']) {
case 'string':
// there is a high confidence that the type is text when
// @var string is used
return new TypeGuess(TextType::class, [], Guess::HIGH_CONFIDENCE);

case 'int':
case 'integer':
// integers can also be the id of an entity or a checkbox (0 or 1)
return new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE);

case 'float':
case 'double':
case 'real':
return new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE);

case 'boolean':
case 'bool':
return new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE);

default:
// there is a very low confidence that this one is correct
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
}
return match($annotations['var']) {
// there is a high confidence that the type is text when
// @var string is used
'string' => new TypeGuess(TextType::class, [], Guess::HIGH_CONFIDENCE),

// integers can also be the id of an entity or a checkbox (0 or 1)
'int', 'integer' => new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE),

'float', 'double', 'real' => new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE),

'boolean', 'bool' => new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE),

// there is a very low confidence that this one is correct
default => new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)
};
}

protected function readPhpDocAnnotations(string $class, string $property): array
Expand Down
13 changes: 5 additions & 8 deletions security/voters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,11 @@ would look like this::
/** @var Post $post */
$post = $subject;

switch ($attribute) {
case self::VIEW:
return $this->canView($post, $user);
case self::EDIT:
return $this->canEdit($post, $user);
}

throw new \LogicException('This code should not be reached!');
return match($attribute) {
self::VIEW => $this->canView($post, $user),
self::EDIT => $this->canEdit($post, $user),
default => throw new \LogicException('This code should not be reached!')
};
}

private function canView(Post $post, User $user): bool
Expand Down