Skip to content

Commit f41c7b4

Browse files
committed
Leverage the match expression
1 parent e6b352a commit f41c7b4

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

Extension/Validator/ValidatorTypeGuesser.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Component\Form\Guess\TypeGuess;
1717
use Symfony\Component\Form\Guess\ValueGuess;
1818
use Symfony\Component\Validator\Constraint;
19+
use Symfony\Component\Validator\Constraints\IsTrue;
20+
use Symfony\Component\Validator\Constraints\NotBlank;
21+
use Symfony\Component\Validator\Constraints\NotNull;
1922
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
2023
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
2124

@@ -166,14 +169,12 @@ public function guessTypeForConstraint(Constraint $constraint): ?TypeGuess
166169
*/
167170
public function guessRequiredForConstraint(Constraint $constraint): ?ValueGuess
168171
{
169-
switch (\get_class($constraint)) {
170-
case 'Symfony\Component\Validator\Constraints\NotNull':
171-
case 'Symfony\Component\Validator\Constraints\NotBlank':
172-
case 'Symfony\Component\Validator\Constraints\IsTrue':
173-
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
174-
}
175-
176-
return null;
172+
return match (\get_class($constraint)) {
173+
NotNull::class,
174+
NotBlank::class,
175+
IsTrue::class => new ValueGuess(true, Guess::HIGH_CONFIDENCE),
176+
default => null,
177+
};
177178
}
178179

179180
/**

Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,11 @@ public function testCreateViewFlatAttrClosureReceivesKey()
729729
null, // index
730730
null, // group
731731
function ($object, $key) {
732-
switch ($key) {
733-
case 'B': return ['attr1' => 'value1'];
734-
case 'C': return ['attr2' => 'value2'];
735-
default: return [];
736-
}
732+
return match ($key) {
733+
'B' => ['attr1' => 'value1'],
734+
'C' => ['attr2' => 'value2'],
735+
default => [],
736+
};
737737
}
738738
);
739739

@@ -749,11 +749,11 @@ public function testCreateViewFlatAttrClosureReceivesValue()
749749
null, // index
750750
null, // group
751751
function ($object, $key, $value) {
752-
switch ($value) {
753-
case '1': return ['attr1' => 'value1'];
754-
case '2': return ['attr2' => 'value2'];
755-
default: return [];
756-
}
752+
return match ($value) {
753+
'1' => ['attr1' => 'value1'],
754+
'2' => ['attr2' => 'value2'],
755+
default => [],
756+
};
757757
}
758758
);
759759

@@ -852,10 +852,10 @@ public function testCreateViewFlatlabelTranslationParametersClosureReceivesKey()
852852
null, // group
853853
null, // attr
854854
function ($object, $key) {
855-
switch ($key) {
856-
case 'D': return ['%placeholder1%' => 'value1'];
857-
default: return [];
858-
}
855+
return match ($key) {
856+
'D' => ['%placeholder1%' => 'value1'],
857+
default => [],
858+
};
859859
}
860860
);
861861

@@ -872,10 +872,10 @@ public function testCreateViewFlatlabelTranslationParametersClosureReceivesValue
872872
null, // group
873873
null, // attr
874874
function ($object, $key, $value) {
875-
switch ($value) {
876-
case '3': return ['%placeholder1%' => 'value1'];
877-
default: return [];
878-
}
875+
return match ($value) {
876+
'3' => ['%placeholder1%' => 'value1'],
877+
default => [],
878+
};
879879
}
880880
);
881881

0 commit comments

Comments
 (0)