convert `switch` to `match`

This commit is contained in:
Thomas Rupprecht 2022-12-22 00:08:54 +01:00 committed by GitHub
parent fdddd63aae
commit 3d88ae7916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 119 additions and 213 deletions

View File

@ -27,19 +27,14 @@ function angeltypes_controller()
{ {
$action = strip_request_item('action', 'list'); $action = strip_request_item('action', 'list');
switch ($action) { return match ($action) {
case 'view': 'view' => angeltype_controller(),
return angeltype_controller(); 'edit' => angeltype_edit_controller(),
case 'edit': 'delete' => angeltype_delete_controller(),
return angeltype_edit_controller(); 'about' => angeltypes_about_controller(),
case 'delete': 'list' => angeltypes_list_controller(),
return angeltype_delete_controller(); default => angeltypes_list_controller(),
case 'about': };
return angeltypes_about_controller();
case 'list':
default:
return angeltypes_list_controller();
}
} }
/** /**

View File

@ -71,15 +71,11 @@ function rooms_controller(): array
$action = 'list'; $action = 'list';
} }
switch ($action) { return match ($action) {
case 'view': 'view' => room_controller(),
return room_controller(); 'list' => throw_redirect(page_link_to('admin_rooms')),
case 'list': default => throw_redirect(page_link_to('admin_rooms')),
default: };
throw_redirect(page_link_to('admin_rooms'));
}
return ['', ''];
} }
/** /**

View File

@ -24,14 +24,11 @@ function shift_entries_controller(): array
throw_redirect(user_link($user->id)); throw_redirect(user_link($user->id));
} }
switch ($action) { return match ($action) {
case 'create': 'create' => shift_entry_create_controller(),
return shift_entry_create_controller(); 'delete' => shift_entry_delete_controller(),
case 'delete': default => ['', ''],
return shift_entry_delete_controller(); };
}
return ['', ''];
} }
/** /**

View File

@ -334,7 +334,7 @@ function shift_controller()
} }
/** /**
* @return array|false * @return array
*/ */
function shifts_controller() function shifts_controller()
{ {
@ -343,18 +343,11 @@ function shifts_controller()
throw_redirect(page_link_to('user_shifts')); throw_redirect(page_link_to('user_shifts'));
} }
switch ($request->input('action')) { return match ($request->input('action')) {
case 'view': 'view' => shift_controller(),
return shift_controller(); 'next' => shift_next_controller(), // throw_redirect
/** @noinspection PhpMissingBreakStatementInspection */ default => throw_redirect(page_link_to('/')),
case 'next': };
shift_next_controller();
// fall through
default:
throw_redirect(page_link_to('/'));
}
return false;
} }
/** /**

View File

@ -153,15 +153,11 @@ function shifttypes_controller()
$action = $request->input('action'); $action = $request->input('action');
} }
switch ($action) { return match ($action) {
case 'view': 'view' => shifttype_controller(),
return shifttype_controller(); 'edit' => shifttype_edit_controller(),
case 'edit': 'delete' => shifttype_delete_controller(),
return shifttype_edit_controller(); 'list' => shifttypes_list_controller(),
case 'delete': default => shifttypes_list_controller(),
return shifttype_delete_controller(); };
case 'list':
default:
return shifttypes_list_controller();
}
} }

View File

@ -467,22 +467,13 @@ function user_angeltypes_controller(): array
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
switch ($request->input('action')) { return match ($request->input('action')) {
case 'delete_all': 'delete_all' => user_angeltypes_delete_all_controller(),
return user_angeltypes_delete_all_controller(); 'confirm_all' => user_angeltypes_confirm_all_controller(),
case 'confirm_all': 'confirm' => user_angeltype_confirm_controller(),
return user_angeltypes_confirm_all_controller(); 'delete' => user_angeltype_delete_controller(),
case 'confirm': 'update' => user_angeltype_update_controller(),
return user_angeltype_confirm_controller(); 'add' => user_angeltype_add_controller(),
case 'delete': default => throw_redirect(page_link_to('angeltyps')),
return user_angeltype_delete_controller(); };
case 'update':
return user_angeltype_update_controller();
case 'add':
return user_angeltype_add_controller();
default:
throw_redirect(page_link_to('angeltypes'));
}
return ['', ''];
} }

View File

@ -45,11 +45,10 @@ function user_driver_licenses_controller()
$action = strip_request_item('action', 'edit'); $action = strip_request_item('action', 'edit');
switch ($action) { return match ($action) {
default: 'edit' => user_driver_license_edit_controller(),
case 'edit': default => user_driver_license_edit_controller(),
return user_driver_license_edit_controller(); };
}
} }
/** /**

View File

@ -27,17 +27,13 @@ function users_controller()
$action = $request->input('action'); $action = $request->input('action');
} }
switch ($action) { return match ($action) {
case 'view': 'view' => user_controller(),
return user_controller(); 'delete' => user_delete_controller(),
case 'delete': 'edit_vouchers' => user_edit_vouchers_controller(),
return user_delete_controller(); 'list' => users_list_controller(),
case 'edit_vouchers': default => users_list_controller(),
return user_edit_vouchers_controller(); };
case 'list':
default:
return users_list_controller();
}
} }
/** /**

View File

@ -87,29 +87,14 @@ class ShiftSignupState
*/ */
private function valueForState($state) private function valueForState($state)
{ {
switch ($state) { return match ($state) {
case ShiftSignupState::NOT_ARRIVED: ShiftSignupState::NOT_ARRIVED, ShiftSignupState::NOT_YET, ShiftSignupState::SHIFT_ENDED => 100,
case ShiftSignupState::NOT_YET: ShiftSignupState::SIGNED_UP => 90,
case ShiftSignupState::SHIFT_ENDED: ShiftSignupState::FREE => 80,
return 100; ShiftSignupState::ANGELTYPE, ShiftSignupState::COLLIDES => 70,
ShiftSignupState::OCCUPIED, ShiftSignupState::ADMIN => 60,
case ShiftSignupState::SIGNED_UP: default => 0,
return 90; };
case ShiftSignupState::FREE:
return 80;
case ShiftSignupState::ANGELTYPE:
case ShiftSignupState::COLLIDES:
return 70;
case ShiftSignupState::OCCUPIED:
case ShiftSignupState::ADMIN:
return 60;
default:
return 0;
}
} }
/** /**
@ -119,13 +104,10 @@ class ShiftSignupState
*/ */
public function isSignupAllowed() public function isSignupAllowed()
{ {
switch ($this->state) { return match ($this->state) {
case ShiftSignupState::FREE: ShiftSignupState::FREE, ShiftSignupState::ADMIN => true,
case ShiftSignupState::ADMIN: default => false,
return true; };
}
return false;
} }
/** /**

View File

@ -71,28 +71,14 @@ class ShiftCalendarShiftRenderer
*/ */
private function classForSignupState(ShiftSignupState $shiftSignupState) private function classForSignupState(ShiftSignupState $shiftSignupState)
{ {
switch ($shiftSignupState->getState()) { return match ($shiftSignupState->getState()) {
case ShiftSignupState::ADMIN: ShiftSignupState::ADMIN, ShiftSignupState::OCCUPIED => 'success',
case ShiftSignupState::OCCUPIED: ShiftSignupState::SIGNED_UP => 'primary',
return 'success'; ShiftSignupState::NOT_ARRIVED, ShiftSignupState::NOT_YET, ShiftSignupState::SHIFT_ENDED => 'secondary',
ShiftSignupState::ANGELTYPE, ShiftSignupState::COLLIDES => 'warning',
case ShiftSignupState::SIGNED_UP: ShiftSignupState::FREE => 'danger',
return 'primary'; default => 'light',
};
case ShiftSignupState::NOT_ARRIVED:
case ShiftSignupState::NOT_YET:
case ShiftSignupState::SHIFT_ENDED:
return 'secondary';
case ShiftSignupState::ANGELTYPE:
case ShiftSignupState::COLLIDES:
return 'warning';
case ShiftSignupState::FREE:
return 'danger';
default:
return 'light';
}
} }
/** /**

View File

@ -90,24 +90,13 @@ class Stats
public function email(string $type): int public function email(string $type): int
{ {
switch ($type) { return match ($type) {
case 'system': 'system' => Settings::whereEmailShiftinfo(true)->count(),
$query = Settings::whereEmailShiftinfo(true); 'humans' => Settings::whereEmailHuman(true)->count(),
break; 'goody' => Settings::whereEmailGoody(true)->count(),
case 'humans': 'news' => Settings::whereEmailNews(true)->count(),
$query = Settings::whereEmailHuman(true); default => 0,
break; };
case 'goody':
$query = Settings::whereEmailGoody(true);
break;
case 'news':
$query = Settings::whereEmailNews(true);
break;
default:
return 0;
}
return $query->count();
} }
/** /**

View File

@ -50,26 +50,7 @@ class LegacyDevelopment extends Legacy
$args = []; $args = [];
foreach (($trace['args'] ?? []) as $arg) { foreach (($trace['args'] ?? []) as $arg) {
// @codeCoverageIgnoreStart $args[] = $this->getDisplayNameOfValue($arg);
switch (gettype($arg)) {
case 'string':
case 'integer':
case 'double':
$args[] = $arg;
break;
case 'boolean':
$args[] = $arg ? 'true' : 'false';
break;
case 'object':
$args[] = get_class($arg);
break;
case 'resource':
$args[] = get_resource_type($arg);
break;
default:
$args[] = gettype($arg);
// @codeCoverageIgnoreEnd
}
} }
$return[] = [ $return[] = [
@ -80,4 +61,19 @@ class LegacyDevelopment extends Legacy
return $return; return $return;
} }
/**
* @param mixed $arg
* @return string
*/
private function getDisplayNameOfValue(mixed $arg): string
{
return match (gettype($arg)) {
'string', 'integer', 'double' => (string)$arg,
'boolean' => $arg ? 'true' : 'false',
'object' => get_class($arg),
'resource' => get_resource_type($arg), // @codeCoverageIgnore
default => gettype($arg),
};
}
} }

View File

@ -49,12 +49,10 @@ class SessionServiceProvider extends ServiceProvider
$config = $this->app->get('config'); $config = $this->app->get('config');
$sessionConfig = $config->get('session'); $sessionConfig = $config->get('session');
$handler = null; $handler = match ($sessionConfig['driver']) {
switch ($sessionConfig['driver']) { 'pdo' => $this->app->make(DatabaseHandler::class),
case 'pdo': default => null,
$handler = $this->app->make(DatabaseHandler::class); };
break;
}
return $this->app->make(NativeSessionStorage::class, [ return $this->app->make(NativeSessionStorage::class, [
'options' => [ 'options' => [

View File

@ -46,17 +46,12 @@ class MailerServiceProvider extends ServiceProvider
protected function getTransport(?string $transport, array $config): TransportInterface protected function getTransport(?string $transport, array $config): TransportInterface
{ {
switch ($transport) { return match ($transport) {
case 'log': 'log' => $this->app->make(LogTransport::class),
return $this->app->make(LogTransport::class); 'mail', 'sendmail' => $this->app->make(SendmailTransport::class, ['command' => $config['sendmail'] ?? null]),
case 'mail': 'smtp' => $this->getSmtpTransport($config),
case 'sendmail': default => Transport::fromDsn($transport ?? ''),
return $this->app->make(SendmailTransport::class, ['command' => $config['sendmail'] ?? null]); };
case 'smtp':
return $this->getSmtpTransport($config);
default:
return Transport::fromDsn($transport ?? '');
}
} }
protected function getSmtpTransport(array $config): SmtpTransport protected function getSmtpTransport(array $config): SmtpTransport

View File

@ -51,12 +51,11 @@ class EventConfig extends BaseModel
/** @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute */ /** @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute */
if (!empty($value)) { if (!empty($value)) {
switch ($this->getValueCast($this->name)) { return match ($this->getValueCast($this->name)) {
case 'datetime_human': 'datetime_human' => Carbon::make($value),
return Carbon::make($value); 'datetime' => Carbon::createFromFormat(Carbon::ISO8601, $value),
case 'datetime': default => $value,
return Carbon::createFromFormat(Carbon::ISO8601, $value); };
}
} }
return $value; return $value;
@ -70,16 +69,13 @@ class EventConfig extends BaseModel
public function setValueAttribute(mixed $value): static public function setValueAttribute(mixed $value): static
{ {
if (!empty($value)) { if (!empty($value)) {
switch ($this->getValueCast($this->name)) { $value = match ($this->getValueCast($this->name)) {
case 'datetime_human': /** @var Carbon $value */
/** @var Carbon $value */ 'datetime_human' => $value->toDateTimeString('minute'),
$value = $value->toDateTimeString('minute'); /** @var Carbon $value */
break; 'datetime' => $value->toIso8601String(),
case 'datetime': default => $value,
/** @var Carbon $value */ };
$value = $value->toIso8601String();
break;
}
} }
$value = $this->castAttributeAsJson('value', $value); $value = $this->castAttributeAsJson('value', $value);

View File

@ -11,8 +11,9 @@ use PHPUnit\Framework\TestCase;
class LegacyDevelopmentTest extends TestCase class LegacyDevelopmentTest extends TestCase
{ {
/** /**
* @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::formatStackTrace() * @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::formatStackTrace
* @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::render() * @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::render
* @covers \Engelsystem\Exceptions\Handlers\LegacyDevelopment::getDisplayNameOfValue
*/ */
public function testRender(): void public function testRender(): void
{ {