Skip to content

Commit

Permalink
minor #45433 Leverage the match expression (fancyweb)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.1 branch.

Discussion
----------

Leverage the match expression

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | -
| Tickets       | -
| License       | MIT
| Doc PR        | -

Tentative to leverage the `match` expression when we can in order to keep on modernizing the code (after `array_is_list()`, `str_starts_with()`, `str_ends_with()` and `str_contains()`).

Firstly, let's see if the CI is green.

Commits
-------

217dc16 Leverage the match expression
  • Loading branch information
fabpot committed Feb 19, 2022
2 parents 4e48b5f + 217dc16 commit 01ea9a3
Show file tree
Hide file tree
Showing 60 changed files with 634 additions and 995 deletions.
71 changes: 33 additions & 38 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\MappingException;
use Doctrine\Persistence\Proxy;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateIntervalType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
Expand Down Expand Up @@ -51,44 +61,29 @@ public function guessType(string $class, string $property): ?TypeGuess
return new TypeGuess('Symfony\Bridge\Doctrine\Form\Type\EntityType', ['em' => $name, 'class' => $mapping['targetEntity'], 'multiple' => $multiple], Guess::HIGH_CONFIDENCE);
}

switch ($metadata->getTypeOfField($property)) {
case Types::ARRAY:
case Types::SIMPLE_ARRAY:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
case Types::BOOLEAN:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
case Types::DATETIME_MUTABLE:
case Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
case Types::DATETIME_IMMUTABLE:
case Types::DATETIMETZ_IMMUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
case Types::DATEINTERVAL:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
case Types::DATE_MUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
case Types::DATE_IMMUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
case Types::TIME_MUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
case Types::TIME_IMMUTABLE:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
case Types::DECIMAL:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', ['input' => 'string'], Guess::MEDIUM_CONFIDENCE);
case Types::FLOAT:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
case Types::INTEGER:
case Types::BIGINT:
case Types::SMALLINT:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
case Types::STRING:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
case Types::TEXT:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
default:
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
}
return match ($metadata->getTypeOfField($property)) {
Types::ARRAY,
Types::SIMPLE_ARRAY => new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE),
Types::BOOLEAN => new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE),
Types::DATETIME_MUTABLE,
Types::DATETIMETZ_MUTABLE,
'vardatetime' => new TypeGuess(DateTimeType::class, [], Guess::HIGH_CONFIDENCE),
Types::DATETIME_IMMUTABLE,
Types::DATETIMETZ_IMMUTABLE => new TypeGuess(DateTimeType::class, ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE),
Types::DATEINTERVAL => new TypeGuess(DateIntervalType::class, [], Guess::HIGH_CONFIDENCE),
Types::DATE_MUTABLE => new TypeGuess(DateType::class, [], Guess::HIGH_CONFIDENCE),
Types::DATE_IMMUTABLE => new TypeGuess(DateType::class, ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE),
Types::TIME_MUTABLE => new TypeGuess(TimeType::class, [], Guess::HIGH_CONFIDENCE),
Types::TIME_IMMUTABLE => new TypeGuess(TimeType::class, ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE),
Types::DECIMAL => new TypeGuess(NumberType::class, ['input' => 'string'], Guess::MEDIUM_CONFIDENCE),
Types::FLOAT => new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE),
Types::INTEGER,
Types::BIGINT,
Types::SMALLINT => new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE),
Types::STRING => new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE),
Types::TEXT => new TypeGuess(TextareaType::class, [], Guess::MEDIUM_CONFIDENCE),
default => new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE),
};
}

/**
Expand Down
70 changes: 28 additions & 42 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,47 +247,33 @@ private function isAssociationNullable(array $associationMapping): bool
*/
private function getPhpType(string $doctrineType): ?string
{
switch ($doctrineType) {
case Types::SMALLINT:
case Types::INTEGER:
return Type::BUILTIN_TYPE_INT;

case Types::FLOAT:
return Type::BUILTIN_TYPE_FLOAT;

case Types::BIGINT:
case Types::STRING:
case Types::TEXT:
case Types::GUID:
case Types::DECIMAL:
return Type::BUILTIN_TYPE_STRING;

case Types::BOOLEAN:
return Type::BUILTIN_TYPE_BOOL;

case Types::BLOB:
case Types::BINARY:
return Type::BUILTIN_TYPE_RESOURCE;

case Types::OBJECT:
case Types::DATE_MUTABLE:
case Types::DATETIME_MUTABLE:
case Types::DATETIMETZ_MUTABLE:
case 'vardatetime':
case Types::TIME_MUTABLE:
case Types::DATE_IMMUTABLE:
case Types::DATETIME_IMMUTABLE:
case Types::DATETIMETZ_IMMUTABLE:
case Types::TIME_IMMUTABLE:
case Types::DATEINTERVAL:
return Type::BUILTIN_TYPE_OBJECT;

case Types::ARRAY:
case Types::SIMPLE_ARRAY:
case 'json_array':
return Type::BUILTIN_TYPE_ARRAY;
}

return null;
return match ($doctrineType) {
Types::SMALLINT,
Types::INTEGER => Type::BUILTIN_TYPE_INT,
Types::FLOAT => Type::BUILTIN_TYPE_FLOAT,
Types::BIGINT,
Types::STRING,
Types::TEXT,
Types::GUID,
Types::DECIMAL => Type::BUILTIN_TYPE_STRING,
Types::BOOLEAN => Type::BUILTIN_TYPE_BOOL,
Types::BLOB,
Types::BINARY => Type::BUILTIN_TYPE_RESOURCE,
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,
Types::ARRAY,
Types::SIMPLE_ARRAY,
'json_array' => Type::BUILTIN_TYPE_ARRAY,
default => null,
};
}
}
16 changes: 6 additions & 10 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,12 @@ private function validate(string $template, string $file): array

private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, array $files)
{
switch ($this->format) {
case 'txt':
return $this->displayTxt($output, $io, $files);
case 'json':
return $this->displayJson($output, $files);
case 'github':
return $this->displayTxt($output, $io, $files, true);
default:
throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
}
return match ($this->format) {
'txt' => $this->displayTxt($output, $io, $files),
'json' => $this->displayJson($output, $files),
'github' => $this->displayTxt($output, $io, $files, true),
default => throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format'))),
};
}

private function displayTxt(OutputInterface $output, SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false)
Expand Down
17 changes: 6 additions & 11 deletions src/Symfony/Bridge/Twig/Mime/NotificationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,12 @@ public function getPreparedHeaders(): Headers

private function determinePriority(string $importance): int
{
switch ($importance) {
case self::IMPORTANCE_URGENT:
return self::PRIORITY_HIGHEST;
case self::IMPORTANCE_HIGH:
return self::PRIORITY_HIGH;
case self::IMPORTANCE_MEDIUM:
return self::PRIORITY_NORMAL;
case self::IMPORTANCE_LOW:
default:
return self::PRIORITY_LOW;
}
return match ($importance) {
self::IMPORTANCE_URGENT => self::PRIORITY_HIGHEST,
self::IMPORTANCE_HIGH => self::PRIORITY_HIGH,
self::IMPORTANCE_MEDIUM => self::PRIORITY_NORMAL,
default => self::PRIORITY_LOW,
};
}

private function getExceptionAsString(\Throwable|FlattenException $exception): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,11 @@ private function loadFromFile(ContainerBuilder $container, $file, $format)
{
$locator = new FileLocator(__DIR__.'/Fixtures/'.$format);

switch ($format) {
case 'php':
$loader = new PhpFileLoader($container, $locator);
break;
case 'xml':
$loader = new XmlFileLoader($container, $locator);
break;
case 'yml':
$loader = new YamlFileLoader($container, $locator);
break;
default:
throw new \InvalidArgumentException(sprintf('Unsupported format: "%s"', $format));
}
$loader = match ($format) {
'php' => new PhpFileLoader($container, $locator),
'xml' => new XmlFileLoader($container, $locator),
'yml' => new YamlFileLoader($container, $locator),
};

$loader->load($file.'.'.$format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,20 @@ public function testGetNameValidTemplate()

public function profilerHasCallback($panel)
{
switch ($panel) {
case 'foo':
case 'bar':
return true;
default:
return false;
}
return match ($panel) {
'foo',
'bar' => true,
default => false,
};
}

public function profileHasCollectorCallback($panel)
{
switch ($panel) {
case 'foo':
case 'baz':
return true;
default:
return false;
}
return match ($panel) {
'foo',
'baz' => true,
default => false,
};
}

protected function mockTwigEnvironment()
Expand Down Expand Up @@ -120,12 +116,10 @@ public function __construct()

public function hasCollector(string $name): bool
{
switch ($name) {
case 'foo':
case 'bar':
return true;
default:
return false;
}
return match ($name) {
'foo',
'bar' => true,
default => false,
};
}
}
33 changes: 11 additions & 22 deletions src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,28 +338,17 @@ private function getPlatformName(): string

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

switch (true) {
case $platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform:
case $platform instanceof \Doctrine\DBAL\Platforms\MySQL57Platform:
return $this->platformName = 'mysql';

case $platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform:
return $this->platformName = 'sqlite';

case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform:
case $platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform:
return $this->platformName = 'pgsql';

case $platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform:
return $this->platformName = 'oci';

case $platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform:
case $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform:
return $this->platformName = 'sqlsrv';

default:
return $this->platformName = \get_class($platform);
}
return match (true) {
$platform instanceof \Doctrine\DBAL\Platforms\MySQLPlatform,
$platform instanceof \Doctrine\DBAL\Platforms\MySQL57Platform => $this->platformName = 'mysql',
$platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform => $this->platformName = 'sqlite',
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQLPlatform,
$platform instanceof \Doctrine\DBAL\Platforms\PostgreSQL94Platform => $this->platformName = 'pgsql',
$platform instanceof \Doctrine\DBAL\Platforms\OraclePlatform => $this->platformName = 'oci',
$platform instanceof \Doctrine\DBAL\Platforms\SQLServerPlatform,
$platform instanceof \Doctrine\DBAL\Platforms\SQLServer2012Platform => $this->platformName = 'sqlsrv',
default => $this->platformName = \get_class($platform),
};
}

private function getServerVersion(): string
Expand Down
37 changes: 13 additions & 24 deletions src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,19 @@ public function createTable()
// connect if we are not yet
$conn = $this->getConnection();

switch ($this->driver) {
case 'mysql':
// We use varbinary for the ID column because it prevents unwanted conversions:
// - character set conversions between server and client
// - trailing space removal
// - case-insensitivity
// - language processing like é == e
$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";
break;
case 'sqlite':
$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)";
break;
case 'pgsql':
$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)";
break;
case 'oci':
$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)";
break;
case 'sqlsrv':
$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)";
break;
default:
throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver));
}
$sql = match ($this->driver) {
// We use varbinary for the ID column because it prevents unwanted conversions:
// - character set conversions between server and client
// - trailing space removal
// - case-insensitivity
// - language processing like é == e
'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",
'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)",
'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)",
'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)",
'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)",
default => throw new \DomainException(sprintf('Creating the cache table is currently not implemented for PDO driver "%s".', $this->driver)),
};

$conn->exec($sql);
}
Expand Down
Loading

0 comments on commit 01ea9a3

Please sign in to comment.