Skip to content
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

[9.x] Use match expression where possible #39583

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 7 additions & 10 deletions src/Illuminate/Auth/CreatesUserProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ public function createUserProvider($provider = null)
);
}

switch ($driver) {
case 'database':
return $this->createDatabaseProvider($config);
case 'eloquent':
return $this->createEloquentProvider($config);
default:
throw new InvalidArgumentException(
"Authentication user provider [{$driver}] is not defined."
);
}
return match ($driver) {
'database' => $this->createDatabaseProvider($config),
'eloquent' => $this->createEloquentProvider($config),
default => (throw new InvalidArgumentException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the throw wrapped in parentheses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are not necessary. I removed them.

"Authentication user provider [{$driver}] is not defined."
)),
};
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,11 @@ protected function fireConnectionEvent($event)
return;
}

switch ($event) {
case 'beganTransaction':
return $this->events->dispatch(new TransactionBeginning($this));
case 'committed':
return $this->events->dispatch(new TransactionCommitted($this));
case 'rollingBack':
return $this->events->dispatch(new TransactionRolledBack($this));
}
return $this->events->dispatch(match ($event) {
'beganTransaction' => new TransactionBeginning($this),
'committed' => new TransactionCommitted($this),
'rollingBack' => new TransactionRolledBack($this),
});
}

/**
Expand Down
38 changes: 14 additions & 24 deletions src/Illuminate/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,13 @@ public function createConnector(array $config)
return $this->container->make($key);
}

switch ($config['driver']) {
case 'mysql':
return new MySqlConnector;
case 'pgsql':
return new PostgresConnector;
case 'sqlite':
return new SQLiteConnector;
case 'sqlsrv':
return new SqlServerConnector;
}

throw new InvalidArgumentException("Unsupported driver [{$config['driver']}].");
return match ($config['driver']) {
'mysql' => new MySqlConnector,
'pgsql' => new PostgresConnector,
'sqlite' => new SQLiteConnector,
'sqlsrv' => new SqlServerConnector,
default => (throw new InvalidArgumentException("Unsupported driver [{$config['driver']}].")),
};
}

/**
Expand All @@ -273,17 +268,12 @@ protected function createConnection($driver, $connection, $database, $prefix = '
return $resolver($connection, $database, $prefix, $config);
}

switch ($driver) {
case 'mysql':
return new MySqlConnection($connection, $database, $prefix, $config);
case 'pgsql':
return new PostgresConnection($connection, $database, $prefix, $config);
case 'sqlite':
return new SQLiteConnection($connection, $database, $prefix, $config);
case 'sqlsrv':
return new SqlServerConnection($connection, $database, $prefix, $config);
}

throw new InvalidArgumentException("Unsupported driver [{$driver}].");
return match ($driver) {
'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config),
'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
default => (throw new InvalidArgumentException("Unsupported driver [{$driver}].")),
};
}
}
33 changes: 11 additions & 22 deletions src/Illuminate/Database/DBAL/TimestampType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,17 @@ class TimestampType extends Type implements PhpDateTimeMappingType
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$name = $platform->getName();

switch ($name) {
case 'mysql':
case 'mysql2':
return $this->getMySqlPlatformSQLDeclaration($fieldDeclaration);

case 'postgresql':
case 'pgsql':
case 'postgres':
return $this->getPostgresPlatformSQLDeclaration($fieldDeclaration);

case 'mssql':
return $this->getSqlServerPlatformSQLDeclaration($fieldDeclaration);

case 'sqlite':
case 'sqlite3':
return $this->getSQLitePlatformSQLDeclaration($fieldDeclaration);

default:
throw new DBALException('Invalid platform: '.$name);
}
return match ($name = $platform->getName()) {
'mysql',
'mysql2' => $this->getMySqlPlatformSQLDeclaration($fieldDeclaration),
'postgresql',
'pgsql',
'postgres' => $this->getPostgresPlatformSQLDeclaration($fieldDeclaration),
'mssql' => $this->getSqlServerPlatformSQLDeclaration($fieldDeclaration),
'sqlite',
'sqlite3' => $this->getSQLitePlatformSQLDeclaration($fieldDeclaration),
default => (throw new DBALException('Invalid platform: '.$name)),
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,18 +669,11 @@ protected function castAttributes($attributes)
*/
protected function getTypeSwapValue($type, $value)
{
switch (strtolower($type)) {
case 'int':
case 'integer':
return (int) $value;
case 'real':
case 'float':
case 'double':
return (float) $value;
case 'string':
return (string) $value;
default:
return $value;
}
return match (strtolower($type)) {
'int', 'integer' => (int) $value,
'real', 'float', 'double' => (float) $value,
'string' => (string) $value,
default => $value,
};
}
}
48 changes: 15 additions & 33 deletions src/Illuminate/Database/Schema/Grammars/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,14 @@ protected static function getDoctrineColumnType($type)
{
$type = strtolower($type);

switch ($type) {
case 'biginteger':
$type = 'bigint';
break;
case 'smallinteger':
$type = 'smallint';
break;
case 'mediumtext':
case 'longtext':
$type = 'text';
break;
case 'binary':
$type = 'blob';
break;
case 'uuid':
$type = 'guid';
break;
}

return Type::getType($type);
return Type::getType(match ($type) {
'biginteger' => 'bigint',
'smallinteger' => 'smallint',
'mediumtext', 'longtext' => 'text',
'binary' => 'blob',
'uuid' => 'guid',
default => $type,
});
}

/**
Expand Down Expand Up @@ -216,19 +204,13 @@ protected static function doesntNeedCharacterOptions($type)
*/
protected static function mapFluentOptionToDoctrine($attribute)
{
switch ($attribute) {
case 'type':
case 'name':
return;
case 'nullable':
return 'notnull';
case 'total':
return 'precision';
case 'places':
return 'scale';
default:
return $attribute;
}
return match ($attribute) {
'type', 'name' => null,
'nullable' => 'notnull',
'total' => 'precision',
'places' => 'scale',
default => $attribute,
};
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,11 @@ protected function parseVisibility($visibility)
return;
}

switch ($visibility) {
case FilesystemContract::VISIBILITY_PUBLIC:
return Visibility::PUBLIC;
case FilesystemContract::VISIBILITY_PRIVATE:
return Visibility::PRIVATE;
}

throw new InvalidArgumentException("Unknown visibility: {$visibility}.");
return match ($visibility) {
FilesystemContract::VISIBILITY_PUBLIC => Visibility::PUBLIC,
FilesystemContract::VISIBILITY_PRIVATE => Visibility::PRIVATE,
default => (throw new InvalidArgumentException("Unknown visibility: {$visibility}.")),
};
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/Illuminate/Notifications/RoutesNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ public function routeNotificationFor($driver, $notification = null)
return $this->{$method}($notification);
}

switch ($driver) {
case 'database':
return $this->notifications();
case 'mail':
return $this->email;
}
return match ($driver) {
'database' => $this->notifications(),
'mail' => $this->email,
};
}
}
10 changes: 4 additions & 6 deletions src/Illuminate/Redis/RedisManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ protected function connector()
return $customCreator();
}

switch ($this->driver) {
case 'predis':
return new PredisConnector;
case 'phpredis':
return new PhpRedisConnector;
}
return match ($this->driver) {
'predis' => new PredisConnector,
'phpredis' => new PhpRedisConnector,
};
}

/**
Expand Down