Skip to content

Commit 3d47788

Browse files
author
Romain Monteil
committed
Replace switch statement with match expression
1 parent f6791ca commit 3d47788

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

form/type_guesser.rst

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,21 @@ With this knowledge, you can implement the ``guessType()`` method of the
105105
}
106106

107107
// otherwise, base the type on the @var annotation
108-
switch ($annotations['var']) {
109-
case 'string':
110-
// there is a high confidence that the type is text when
111-
// @var string is used
112-
return new TypeGuess(TextType::class, [], Guess::HIGH_CONFIDENCE);
113-
114-
case 'int':
115-
case 'integer':
116-
// integers can also be the id of an entity or a checkbox (0 or 1)
117-
return new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE);
118-
119-
case 'float':
120-
case 'double':
121-
case 'real':
122-
return new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE);
123-
124-
case 'boolean':
125-
case 'bool':
126-
return new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE);
127-
128-
default:
129-
// there is a very low confidence that this one is correct
130-
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
131-
}
108+
return match($annotations['var']) {
109+
// there is a high confidence that the type is text when
110+
// @var string is used
111+
'string' => new TypeGuess(TextType::class, [], Guess::HIGH_CONFIDENCE),
112+
113+
// integers can also be the id of an entity or a checkbox (0 or 1)
114+
'int', 'integer' => new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE),
115+
116+
'float', 'double', 'real' => new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE),
117+
118+
'boolean', 'bool' => new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE),
119+
120+
// there is a very low confidence that this one is correct
121+
default => new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)
122+
};
132123
}
133124

134125
protected function readPhpDocAnnotations(string $class, string $property): array

security/voters.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,11 @@ would look like this::
153153
/** @var Post $post */
154154
$post = $subject;
155155

156-
switch ($attribute) {
157-
case self::VIEW:
158-
return $this->canView($post, $user);
159-
case self::EDIT:
160-
return $this->canEdit($post, $user);
161-
}
162-
163-
throw new \LogicException('This code should not be reached!');
156+
return match($attribute) {
157+
self::VIEW => $this->canView($post, $user),
158+
self::EDIT => $this->canEdit($post, $user),
159+
default => throw new \LogicException('This code should not be reached!')
160+
};
164161
}
165162

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

0 commit comments

Comments
 (0)