8000 [9.x] Use match expression where possible (#39583) · laravel/framework@23cdfcf · GitHub
[go: up one dir, main page]

Skip to content

Commit 23cdfcf

Browse files
authored
[9.x] Use match expression where possible (#39583)
* Use match expression where possible * Fix coding style * Remove unneeded parentheses
1 parent 4baf4f7 commit 23cdfcf

File tree

9 files changed

+71
-130
lines changed

9 files changed

+71
-130
lines changed

src/Illuminate/Auth/CreatesUserProviders.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ public function createUserProvider($provider = null)
3333
);
3434
}
3535

36-
switch ($driver) {
37-
case 'database':
38-
return $this->createDatabaseProvider($config);
39-
case 'eloquent':
40-
return $this->createEloquentProvider($config);
41-
default:
42-
throw new InvalidArgumentException(
43-
"Authentication user provider [{$driver}] is not defined."
44-
);
45-
}
36+
return match ($driver) {
37+
'database' => $this->createDatabaseProvider($config),
38+
'eloquent' => $this->createEloquentProvider($config),
39+
default => throw new InvalidArgumentException(
40+
"Authentication user provider [{$driver}] is not defined."
41+
),
42+
};
4643
}
4744

4845
/**

src/Illuminate/Database/Connection.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -856,14 +856,11 @@ protected function fireConnectionEvent($event)
856856
return;
857857
}
858858

859-
switch ($event) {
860-
case 'beganTransaction':
861-
return $this->events->dispatch(new TransactionBeginning($this));
862-
case 'committed':
863-
return $this->events->dispatch(new TransactionCommitted($this));
864-
case 'rollingBack':
865-
return $this->events->dispatch(new TransactionRolledBack($this));
866-
}
859+
return $this->events->dispatch(match ($event) {
860+
'beganTransaction' => new TransactionBeginning($this),
861+
'committed' => new TransactionCommitted($this),
862+
'rollingBack' => new TransactionRolledBack($this),
863+
});
867864
}
868865

869866
/**

src/Illuminate/Database/Connectors/ConnectionFactory.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,13 @@ public function createConnector(array $config)
241241
return $this->container->make($key);
242242
}
243243

244-
switch ($config['driver']) {
245-
case 'mysql':
246-
return new MySqlConnector;
247-
case 'pgsql':
248-
return new PostgresConnector;
249-
case 'sqlite':
250-
return new SQLiteConnector;
251-
case 'sqlsrv':
252-
return new SqlServerConnector;
253-
}
254-
255-
throw new InvalidArgumentException("Unsupported driver [{$config['driver']}].");
244+
return match ($config['driver']) {
245+
'mysql' => new MySqlConnector,
246+
'pgsql' => new PostgresConnector,
247+
'sqlite' => new SQLiteConnector,
248+
'sqlsrv' => new SqlServerConnector,
249+
default => throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."),
250+
};
256251
}
257252

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

276-
switch ($driver) {
277-
case 'mysql':
278-
return new MySqlConnection($connection, $database, $prefix, $config);
279-
case 'pgsql':
280-
return new PostgresConnection($connection, $database, $prefix, $config);
281-
case 'sqlite':
282-
return new SQLiteConnection($connection, $database, $prefix, $config);
283-
case 'sqlsrv':
284-
return new SqlServerConnection($connection, $database, $prefix, $config);
285-
}
286-
287-
throw new InvalidArgumentException("Unsupported driver [{$driver}].");
271+
return match ($driver) {
272+
'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
273+
'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
274+
'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config),
275+
'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
276+
default => throw new InvalidArgumentException("Unsupported driver [{$driver}]."),
277+
};
288278
}
289279
}

src/Illuminate/Database/DBAL/TimestampType.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,17 @@ class TimestampType extends Type implements PhpDateTimeMappingType
1616
*/
1717
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
1818
{
19-
$name = $platform->getName();
20-
21-
switch ($name) {
22-
case 'mysql':
23-
case 'mysql2':
24-
return $this->getMySqlPlatformSQLDeclaration($fieldDeclaration);
25-
26-
case 'postgresql':
27-
case 'pgsql':
28-
case 'postgres':
29-
return $this->getPostgresPlatformSQLDeclaration($fieldDeclaration);
30-
31-
case 'mssql':
32-
return $this->getSqlServerPlatformSQLDeclaration($fieldDeclaration);
33-
34-
case 'sqlite':
35-
case 'sqlite3':
36-
return $this->getSQLitePlatformSQLDeclaration($fieldDeclaration);
37-
38-
default:
39-
throw new DBALException('Invalid platform: '.$name);
40-
}
19+
return match ($name = $platform->getName()) {
20+
'mysql',
21+
'mysql2' => $this->getMySqlPlatformSQLDeclaration($fieldDeclaration),
22+
'postgresql',
23+
'pgsql',
24+
'postgres' => $this->getPostgresPlatformSQLDeclaration($fieldDeclaration),
25+
'mssql' => $this->getSqlServerPlatformSQLDeclaration($fieldDeclaration),
26+
'sqlite',
27+
'sqlite3' => $this->getSQLitePlatformSQLDeclaration($fieldDeclaration),
28+
default => throw new DBALException('Invalid platform: '.$name),
29+
};
4130
}
4231

4332
/**

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -669,18 +669,11 @@ protected function castAttributes($attributes)
669669
*/
670670
protected function getTypeSwapValue($type, $value)
671671
{
672-
switch (strtolower($type)) {
673-
case 'int':
674-
case 'integer':
675-
return (int) $value;
676-
case 'real':
677-
case 'float':
678-
case 'double':
679-
return (float) $value;
680-
case 'string':
681-
return (string) $value;
682-
default:
683-
return $value;
684-
}
672+
return match (strtolower($type)) {
673+
'int', 'integer' => (int) $value,
674+
'real', 'float', 'double' => (float) $value,
675+
'string' => (string) $value,
676+
default => $value,
677+
};
685678
}
686679
}

src/Illuminate/Database/Schema/Grammars/ChangeColumn.php

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,14 @@ protected static function getDoctrineColumnType($type)
145145
{
146146
$type = strtolower($type);
147147

148-
switch ($type) {
149-
case 'biginteger':
150-
$type = 'bigint';
151-
break;
152-
case 'smallinteger':
153-
$type = 'smallint';
154-
break;
155-
case 'mediumtext':
156-
case 'longtext':
157-
$type = 'text';
158-
break;
159-
case 'binary':
160-
$type = 'blob';
161-
break;
162-
case 'uuid':
163-
$type = 'guid';
164-
break;
165-
}
166-
167-
return Type::getType($type);
148+
return Type::getType(match ($type) {
149+
'biginteger' => 'bigint',
150+
'smallinteger' => 'smallint',
151+
'mediumtext', 'longtext' => 'text',
152+
'binary' => 'blob',
153+
'uuid' => 'guid',
154+
default => $type,
155+
});
168156
}
169157

170158
/**
@@ -216,19 +204,13 @@ protected static function doesntNeedCharacterOptions($type)
216204
*/
217205
protected static function mapFluentOptionToDoctrine($attribute)
218206
{
219-
switch ($attribute) {
220-
case 'type':
221-
case 'name':
222-
return;
223-
case 'nullable':
224-
return 'notnull';
225-
case 'total':
226-
return 'precision';
227-
case 'places':
228-
return 'scale';
229-
default:
230-
return $attribute;
231-
}
207+
return match ($attribute) {
208+
'type', 'name' => null,
209+
'nullable' => 'notnull',
210+
'total' => 'precision',
211+
'places' => 'scale',
212+
default => $attribute,
213+
};
232214
}
233215

234216
/**

src/Illuminate/Filesystem/FilesystemAdapter.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,11 @@ protected function parseVisibility($visibility)
758758
return;
759759
}
760760

761-
switch ($visibility) {
762-
case FilesystemContract::VISIBILITY_PUBLIC:
763-
return Visibility::PUBLIC;
764-
case FilesystemContract::VISIBILITY_PRIVATE:
765-
return Visibility::PRIVATE;
766-
}
767-
768-
throw new InvalidArgumentException("Unknown visibility: {$visibility}.");
761+
return match ($visibility) {
762+
FilesystemContract::VISIBILITY_PUBLIC => Visibility::PUBLIC,
763+
FilesystemContract::VISIBILITY_PRIVATE => Visibility::PRIVATE,
764+
default => throw new InvalidArgumentException("Unknown visibility: {$visibility}."),
765+
};
769766
}
770767

771768
/**

src/Illuminate/Notifications/RoutesNotifications.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ public function routeNotificationFor($driver, $notification = null)
4343
return $this->{$method}($notification);
4444
}
4545

46-
switch ($driver) {
47-
case 'database':
48-
return $this->notifications();
49-
case 'mail':
50-
return $this->email;
51-
}
46+
return match ($driver) {
47+
'database' => $this->notifications(),
48+
'mail' => $this->email,
49+
};
5250
}
5351
}

src/Illuminate/Redis/RedisManager.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,10 @@ protected function connector()
168168
return $customCreator();
169169
}
170170

171-
switch ($this->driver) {
172-
case 'predis':
173-
return new PredisConnector;
174-
case 'phpredis':
175-
return new PhpRedisConnector;
176-
}
171+
return match ($this->driver) {
172+
'predis' => new PredisConnector,
173+
'phpredis' => new PhpRedisConnector,
174+
};
177175
}
178176

179177
/**

0 commit comments

Comments
 (0)
0