Skip to content

Commit 67b5237

Browse files
committed
Leverage the match expression
1 parent 44db49f commit 67b5237

File tree

60 files changed

+480
-995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+480
-995
lines changed

src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,44 +51,23 @@ public function guessType(string $class, string $property): ?TypeGuess
5151
return new TypeGuess('Symfony\Bridge\Doctrine\Form\Type\EntityType', ['em' => $name, 'class' => $mapping['targetEntity'], 'multiple' => $multiple], Guess::HIGH_CONFIDENCE);
5252
}
5353

54-
switch ($metadata->getTypeOfField($property)) {
55-
case Types::ARRAY:
56-
case Types::SIMPLE_ARRAY:
57-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
58-
case Types::BOOLEAN:
59-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
60-
case Types::DATETIME_MUTABLE:
61-
case Types::DATETIMETZ_MUTABLE:
62-
case 'vardatetime':
63-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
64-
case Types::DATETIME_IMMUTABLE:
65-
case Types::DATETIMETZ_IMMUTABLE:
66-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
67-
case Types::DATEINTERVAL:
68-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
69-
case Types::DATE_MUTABLE:
70-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
71-
case Types::DATE_IMMUTABLE:
72-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
73-
case Types::TIME_MUTABLE:
74-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
75-
case Types::TIME_IMMUTABLE:
76-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
77-
case Types::DECIMAL:
78-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', ['input' => 'string'], Guess::MEDIUM_CONFIDENCE);
79-
case Types::FLOAT:
80-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
81-
case Types::INTEGER:
82-
case Types::BIGINT:
83-
case Types::SMALLINT:
84-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
85-
case Types::STRING:
86-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
87-
case Types::TEXT:
88-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
89-
default:
90-
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
91-
}
54+
return match ($metadata->getTypeOfField($property)) {
55+
Types::ARRAY, Types::SIMPLE_ARRAY => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE),
56+
Types::BOOLEAN => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE),
57+
Types::DATETIME_MUTABLE, Types::DATETIMETZ_MUTABLE, 'vardatetime' => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE),
58+
Types::DATETIME_IMMUTABLE, Types::DATETIMETZ_IMMUTABLE => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE),
59+
Types::DATEINTERVAL => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE),
60+
Types::DATE_MUTABLE => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE),
61+
Types::DATE_IMMUTABLE => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE),
62+
Types::TIME_MUTABLE => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE),
63+
Types::TIME_IMMUTABLE => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE),
64+
Types::DECIMAL => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', ['input' => 'string'], Guess::MEDIUM_CONFIDENCE),
65+
Types::FLOAT => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE),
66+
Types::INTEGER, Types::BIGINT, Types::SMALLINT => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE),
67+
Types::STRING => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE),
68+
Types::TEXT => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE),
69+
default => new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE),
70+
};
9271
}
9372

9473
/**

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -247,47 +247,15 @@ private function isAssociationNullable(array $associationMapping): bool
247247
*/
248248
private function getPhpType(string $doctrineType): ?string
249249
{
250-
switch ($doctrineType) {
251-
case Types::SMALLINT:
252-
case Types::INTEGER:
253-
return Type::BUILTIN_TYPE_INT;
254-
255-
case Types::FLOAT:
256-
return Type::BUILTIN_TYPE_FLOAT;
257-
258-
case Types::BIGINT:
259-
case Types::STRING:
260-
case Types::TEXT:
261-
case Types::GUID:
262-
case Types::DECIMAL:
263-
return Type::BUILTIN_TYPE_STRING;
264-
265-
case Types::BOOLEAN:
266-
return Type::BUILTIN_TYPE_BOOL;
267-
268-
case Types::BLOB:
269-
case Types::BINARY:
270-
return Type::BUILTIN_TYPE_RESOURCE;
271-
272-
case Types::OBJECT:
273-
case Types::DATE_MUTABLE:
274-
case Types::DATETIME_MUTABLE:
275-
case Types::DATETIMETZ_MUTABLE:
276-
case 'vardatetime':
277-
case Types::TIME_MUTABLE:
278-
case Types::DATE_IMMUTABLE:
279-
case Types::DATETIME_IMMUTABLE:
280-
case Types::DATETIMETZ_IMMUTABLE:
281-
case Types::TIME_IMMUTABLE:
282-
case Types::DATEINTERVAL:
283-
return Type::BUILTIN_TYPE_OBJECT;
284-
285-
case Types::ARRAY:
286-
case Types::SIMPLE_ARRAY:
287-
case 'json_array':
288-
return Type::BUILTIN_TYPE_ARRAY;
289-
}
290-
291-
return null;
250+
return match ($doctrineType) {
251+
Types::SMALLINT, Types::INTEGER => Type::BUILTIN_TYPE_INT,
252+
Types::FLOAT => Type::BUILTIN_TYPE_FLOAT,
253+
Types::BIGINT, Types::STRING, Types::TEXT, Types::GUID, Types::DECIMAL => Type::BUILTIN_TYPE_STRING,
254+
Types::BOOLEAN => Type::BUILTIN_TYPE_BOOL,
255+
Types::BLOB, Types::BINARY => Type::BUILTIN_TYPE_RESOURCE,
256+
Types::OBJECT, Types::DATE_MUTABLE, Types::DATETIME_MUTABLE, Types::DATETIMETZ_MUTABLE, 'vardatetime', Types::TIME_MUTABLE, Types::DATE_IMMUTABLE, Types::DATETIME_IMMUTABLE, Types::DATETIMETZ_IMMUTABLE, Types::TIME_IMMUTABLE, Types::DATEINTERVAL => Type::BUILTIN_TYPE_OBJECT,
257+
Types::ARRAY, Types::SIMPLE_ARRAY, 'json_array' => Type::BUILTIN_TYPE_ARRAY,
258+
default => null,
259+
};
292260
}
293261
}

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,12 @@ private function validate(string $template, string $file): array
172172

173173
private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files)
174174
{
175-
switch ($this->format) {
176-
case 'txt':
177-
return $this->displayTxt($output, $io, $files);
178-
case 'json':
179-
return $this->displayJson($output, $files);
180-
case 'github':
181-
return $this->displayTxt($output, $io, $files, true);
182-
default:
183-
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
184-
}
175+
return match ($this->format) {
176+
'txt' => $this->displayTxt($output, $io, $files),
177+
'json' => $this->displayJson($output, $files),
178+
'github' => $this->displayTxt($output, $io, $files, true),
179+
default => throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format'))),
180+
};
185181
}
186182

187183
private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false)

src/Symfony/Bridge/Twig/Mime/NotificationEmail.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,12 @@ public function getPreparedHeaders(): Headers
192192

193193
private function determinePriority(string $importance): int
194194
{
195-
switch ($importance) {
196-
case self::IMPORTANCE_URGENT:
197-
return self::PRIORITY_HIGHEST;
198-
case self::IMPORTANCE_HIGH:
199-
return self::PRIORITY_HIGH;
200-
case self::IMPORTANCE_MEDIUM:
201-
return self::PRIORITY_NORMAL;
202-
case self::IMPORTANCE_LOW:
203-
default:
204-
return self::PRIORITY_LOW;
205-
}
195+
return match ($importance) {
196+
self::IMPORTANCE_URGENT => self::PRIORITY_HIGHEST,
197+
self::IMPORTANCE_HIGH => self::PRIORITY_HIGH,
198+
self::IMPORTANCE_MEDIUM => self::PRIORITY_NORMAL,
199+
default => self::PRIORITY_LOW,
200+
};
206201
}
207202

208203
private function getExceptionAsString(\Throwable|FlattenException $exception): string

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,12 @@ private function loadFromFile(ContainerBuilder $container, $file, $format)
297297
{
298298
$locator = new FileLocator(__DIR__.'/Fixtures/'.$format);
299299

300-
switch ($format) {
301-
case 'php':
302-
$loader = new PhpFileLoader($container, $locator);
303-
break;
304-
case 'xml':
305-
$loader = new XmlFileLoader($container, $locator);
306-
break;
307-
case 'yml':
308-
$loader = new YamlFileLoader($container, $locator);
309-
break;
310-
default:
311-
throw new \InvalidArgumentException(sprintf('Unsupported format: "%s"', $format));
312-
}
300+
$loader = match ($format) {
301+
'php' => new PhpFileLoader($container, $locator),
302+
'xml' => new XmlFileLoader($container, $locator),
303+
'yml' => new YamlFileLoader($container, $locator),
304+
default => throw new \InvalidArgumentException(sprintf('Unsupported format: "%s"', $format)),
305+
};
313306

314307
$loader->load($file.'.'.$format);
315308
}

src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,18 @@ public function testGetNameValidTemplate()
7575

7676
public function profilerHasCallback($panel)
7777
{
78-
switch ($panel) {
79-
case 'foo':
80-
case 'bar':
81-
return true;
82-
default:
83-
return false;
84-
}
78+
return match ($panel) {
79+
'foo', 'bar' => true,
80+
default => false,
81+
};
8582
}
8683

8784
public function profileHasCollectorCallback($panel)
8885
{
89-
switch ($panel) {
90-
case 'foo':
91-
case 'baz':
92-
return true;
93-
default:
94-
return false;
95-
}
86+
return match ($panel) {
87+
'foo', 'baz' => true,
88+
default => false,
89+
};
9690
}
9791

9892
protected function mockTwigEnvironment()
@@ -120,12 +114,9 @@ public function __construct()
120114

121115
public function hasCollector(string $name): bool
122116
{
123-
switch ($name) {
124-
case 'foo':
125-
case 'bar':
126-
return true;
127-
default:
128-
return false;
129-
}
117+
return match ($name) {
118+
'foo', 'bar' => true,
119+
default => false,
120+
};
130121
}
131122
}

src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -338,28 +338,14 @@ private function getPlatformName(): string
338338

339339
$platform = $this->conn->getDatabasePlatform();
340340

341-
switch (true) {
342-
case $platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform:
343-
case $platform instanceof \Doctrine\DBAL\Platforms\MySQL57Platform:
344-
return $this->platformName = 'mysql';
345-
346-
case $platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform:
347-
return $this->platformName = 'sqlite';
348-
349-
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform:
350-
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform:
351-
return $this->platformName = 'pgsql';
352-
353-
case $platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform:
354-
return $this->platformName = 'oci';
355-
356-
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform:
357-
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform:
358-
return $this->platformName = 'sqlsrv';
359-
360-
default:
361-
return $this->platformName = \get_class($platform);
362-
}
341+
return match (true) {
342+
$platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform, $platform instanceof \Doctrine\DBAL\Platforms\MySQL57Platform => $this->platformName = 'mysql',
343+
$platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform => $this->platformName = 'sqlite',
344+
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform, $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform => $this->platformName = 'pgsql',
345+
$platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform => $this->platformName = 'oci',
346+
$platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform, $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform => $this->platformName = 'sqlsrv',
347+
default => $this->platformName = \get_class($platform),
348+
};
363349
}
364350

365351
private function getServerVersion(): string

src/Symfony/Component/Cache/Adapter/PdoAdapter.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,19 @@ public function createTable()
103103
// connect if we are not yet
104104
$conn = $this->getConnection();
105105

106-
switch ($this->driver) {
107-
case 'mysql':
108-
// We use varbinary for the ID column because it prevents unwanted conversions:
109-
// - character set conversions between server and client
110-
// - trailing space removal
111-
// - case-insensitivity
112-
// - language processing like é == e
113-
$sql = "CREATE TABLE $this->table ($this->idCol VARBINARY(255) NOT NULL PRIMARY KEY, $this->dataCol MEDIUMBLOB NOT NULL, $this->lifetimeCol INTEGER UNSIGNED, $this->timeCol INTEGER UNSIGNED NOT NULL) COLLATE utf8mb4_bin, ENGINE = InnoDB";
114-
break;
115-
case 'sqlite':
116-
$sql = "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
117-
break;
118-
case 'pgsql':
119-
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
120-
break;
121-
case 'oci':
122-
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR2(255) NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
123-
break;
124-
case 'sqlsrv':
125-
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol VARBINARY(MAX) NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
126-
break;
127-
default:
128-
throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver));
129-
}
106+
$sql = match ($this->driver) {
107+
// We use varbinary for the ID column because it prevents unwanted conversions:
108+
// - character set conversions between server and client
109+
// - trailing space removal
110+
// - case-insensitivity
111+
// - language processing like é == e
112+
'mysql' => "CREATE TABLE $this->table ($this->idCol VARBINARY(255) NOT NULL PRIMARY KEY, $this->dataCol MEDIUMBLOB NOT NULL, $this->lifetimeCol INTEGER UNSIGNED, $this->timeCol INTEGER UNSIGNED NOT NULL) COLLATE utf8mb4_bin, ENGINE = InnoDB",
113+
'sqlite' => "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)",
114+
'pgsql' => "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)",
115+
'oci' => "CREATE TABLE $this->table ($this->idCol VARCHAR2(255) NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)",
116+
'sqlsrv' => "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol VARBINARY(MAX) NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)",
117+
default => throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver)),
118+
};
130119

131120
$conn->exec($sql);
132121
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ public static function createConnection(string $dsn, array $options = []): \Redi
239239
}
240240
} elseif (is_a($class, \RedisArray::class, true)) {
241241
foreach ($hosts as $i => $host) {
242-
switch ($host['scheme']) {
243-
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
244-
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
245-
default: $hosts[$i] = $host['path'];
246-
}
242+
$hosts[$i] = match ($host['scheme']) {
243+
'tcp' => $host['host'].':'.$host['port'],
244+
'tls' => 'tls://'.$host['host'].':'.$host['port'],
245+
default => $host['path'],
246+
};
247247
}
248248
$params['lazy_connect'] = $params['lazy'] ?? true;
249249
$params['connect_timeout'] = $params['timeout'];
@@ -260,11 +260,11 @@ public static function createConnection(string $dsn, array $options = []): \Redi
260260
} elseif (is_a($class, \RedisCluster::class, true)) {
261261
$initializer = static function () use ($class, $params, $dsn, $hosts) {
262262
foreach ($hosts as $i => $host) {
263-
switch ($host['scheme']) {
264-
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
265-
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
266-
default: $hosts[$i] = $host['path'];
267-
}
263+
$hosts[$i] = match ($host['scheme']) {
264+
'tcp' => $host['host'].':'.$host['port'],
265+
'tls' => 'tls://'.$host['host'].':'.$host['port'],
266+
default => $host['path'],
267+
};
268268
}
269269

270270
try {

0 commit comments

Comments
 (0)