From 8b3d6c972ea8fcee3a02757a8cd635d54e7e6f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Thu, 26 Oct 2023 22:26:51 +0700 Subject: [PATCH] Use `::class` syntax to fetch class name instead of `get_class()` function --- .../flags/src/Api/Serializer/FlagSerializer.php | 2 +- .../src/Api/Serializer/TaskSerializer.php | 2 +- extensions/tags/src/Api/Serializer/TagSerializer.php | 2 +- .../src/Api/Serializer/AccessTokenSerializer.php | 2 +- .../src/Api/Serializer/BasicDiscussionSerializer.php | 2 +- .../core/src/Api/Serializer/BasicPostSerializer.php | 2 +- .../core/src/Api/Serializer/BasicUserSerializer.php | 2 +- .../src/Api/Serializer/CurrentUserSerializer.php | 2 +- .../core/src/Api/Serializer/GroupSerializer.php | 2 +- .../src/Api/Serializer/NotificationSerializer.php | 2 +- framework/core/src/Bus/Dispatcher.php | 2 +- .../src/Extension/Exception/ExtensionBootError.php | 2 +- framework/core/src/Foundation/Application.php | 4 ++-- .../core/src/Foundation/ApplicationInfoProvider.php | 4 ++-- .../src/Foundation/Concerns/InteractsWithLaravel.php | 2 +- .../core/src/Foundation/ErrorHandling/Registry.php | 4 ++-- .../src/Frontend/Compiler/Source/SourceCollector.php | 2 +- framework/core/src/Http/Server.php | 2 +- framework/core/src/Notification/Notification.php | 2 +- framework/core/src/Search/GambitManager.php | 2 +- framework/core/src/User/Access/Gate.php | 2 +- .../tests/integration/extenders/FilesystemTest.php | 12 ++++++------ .../core/tests/integration/extenders/SessionTest.php | 10 +++++----- php-packages/phpstan/src/Extender/Extender.php | 2 +- php-packages/phpstan/src/Extender/Resolver.php | 2 +- 25 files changed, 37 insertions(+), 37 deletions(-) diff --git a/extensions/flags/src/Api/Serializer/FlagSerializer.php b/extensions/flags/src/Api/Serializer/FlagSerializer.php index f68a8bfd38..c1d76354d1 100644 --- a/extensions/flags/src/Api/Serializer/FlagSerializer.php +++ b/extensions/flags/src/Api/Serializer/FlagSerializer.php @@ -24,7 +24,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Flag)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Flag::class + $this::class.' can only serialize instances of '.Flag::class ); } diff --git a/extensions/package-manager/src/Api/Serializer/TaskSerializer.php b/extensions/package-manager/src/Api/Serializer/TaskSerializer.php index 04f01532a3..95c385e609 100644 --- a/extensions/package-manager/src/Api/Serializer/TaskSerializer.php +++ b/extensions/package-manager/src/Api/Serializer/TaskSerializer.php @@ -24,7 +24,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Task)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Task::class + $this::class.' can only serialize instances of '.Task::class ); } diff --git a/extensions/tags/src/Api/Serializer/TagSerializer.php b/extensions/tags/src/Api/Serializer/TagSerializer.php index deac6768ee..bb6e0aba11 100644 --- a/extensions/tags/src/Api/Serializer/TagSerializer.php +++ b/extensions/tags/src/Api/Serializer/TagSerializer.php @@ -29,7 +29,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Tag)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Tag::class + $this::class.' can only serialize instances of '.Tag::class ); } diff --git a/framework/core/src/Api/Serializer/AccessTokenSerializer.php b/framework/core/src/Api/Serializer/AccessTokenSerializer.php index 3831220960..6b8c8e7448 100644 --- a/framework/core/src/Api/Serializer/AccessTokenSerializer.php +++ b/framework/core/src/Api/Serializer/AccessTokenSerializer.php @@ -27,7 +27,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof AccessToken)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.AccessToken::class + $this::class.' can only serialize instances of '.AccessToken::class ); } diff --git a/framework/core/src/Api/Serializer/BasicDiscussionSerializer.php b/framework/core/src/Api/Serializer/BasicDiscussionSerializer.php index 5f18cbb73f..f1f58ce177 100644 --- a/framework/core/src/Api/Serializer/BasicDiscussionSerializer.php +++ b/framework/core/src/Api/Serializer/BasicDiscussionSerializer.php @@ -30,7 +30,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Discussion)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Discussion::class + $this::class.' can only serialize instances of '.Discussion::class ); } diff --git a/framework/core/src/Api/Serializer/BasicPostSerializer.php b/framework/core/src/Api/Serializer/BasicPostSerializer.php index c9aee3f0eb..25502ff870 100644 --- a/framework/core/src/Api/Serializer/BasicPostSerializer.php +++ b/framework/core/src/Api/Serializer/BasicPostSerializer.php @@ -34,7 +34,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Post)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Post::class + $this::class.' can only serialize instances of '.Post::class ); } diff --git a/framework/core/src/Api/Serializer/BasicUserSerializer.php b/framework/core/src/Api/Serializer/BasicUserSerializer.php index 4f2f553847..a7aef27a78 100644 --- a/framework/core/src/Api/Serializer/BasicUserSerializer.php +++ b/framework/core/src/Api/Serializer/BasicUserSerializer.php @@ -30,7 +30,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof User)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.User::class + $this::class.' can only serialize instances of '.User::class ); } diff --git a/framework/core/src/Api/Serializer/CurrentUserSerializer.php b/framework/core/src/Api/Serializer/CurrentUserSerializer.php index 4328c15a73..6082609592 100644 --- a/framework/core/src/Api/Serializer/CurrentUserSerializer.php +++ b/framework/core/src/Api/Serializer/CurrentUserSerializer.php @@ -18,7 +18,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof User)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.User::class + $this::class.' can only serialize instances of '.User::class ); } diff --git a/framework/core/src/Api/Serializer/GroupSerializer.php b/framework/core/src/Api/Serializer/GroupSerializer.php index 86eeda86ee..4a27d33b41 100644 --- a/framework/core/src/Api/Serializer/GroupSerializer.php +++ b/framework/core/src/Api/Serializer/GroupSerializer.php @@ -29,7 +29,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Group)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Group::class + $this::class.' can only serialize instances of '.Group::class ); } diff --git a/framework/core/src/Api/Serializer/NotificationSerializer.php b/framework/core/src/Api/Serializer/NotificationSerializer.php index 0c6ee7e51d..9726a50010 100644 --- a/framework/core/src/Api/Serializer/NotificationSerializer.php +++ b/framework/core/src/Api/Serializer/NotificationSerializer.php @@ -30,7 +30,7 @@ protected function getDefaultAttributes(object|array $model): array { if (! ($model instanceof Notification)) { throw new InvalidArgumentException( - get_class($this).' can only serialize instances of '.Notification::class + $this::class.' can only serialize instances of '.Notification::class ); } diff --git a/framework/core/src/Bus/Dispatcher.php b/framework/core/src/Bus/Dispatcher.php index 31af9bce8c..2ab6da1ef2 100644 --- a/framework/core/src/Bus/Dispatcher.php +++ b/framework/core/src/Bus/Dispatcher.php @@ -15,7 +15,7 @@ class Dispatcher extends BaseDispatcher { public function getCommandHandler($command) { - $handler = get_class($command).'Handler'; + $handler = $command::class.'Handler'; if (class_exists($handler)) { return $this->container->make($handler); diff --git a/framework/core/src/Extension/Exception/ExtensionBootError.php b/framework/core/src/Extension/Exception/ExtensionBootError.php index ee7a9657d8..2a75698e24 100644 --- a/framework/core/src/Extension/Exception/ExtensionBootError.php +++ b/framework/core/src/Extension/Exception/ExtensionBootError.php @@ -20,7 +20,7 @@ public function __construct( public object $extender, Throwable $previous = null ) { - $extenderClass = get_class($extender); + $extenderClass = $extender::class; parent::__construct("Experienced an error while booting extension: {$extension->getTitle()}.\n\nError occurred while applying an extender of type: $extenderClass.", 0, $previous); } diff --git a/framework/core/src/Foundation/Application.php b/framework/core/src/Foundation/Application.php index 90b2bb7241..dece4954c3 100644 --- a/framework/core/src/Foundation/Application.php +++ b/framework/core/src/Foundation/Application.php @@ -115,7 +115,7 @@ public function register($provider, $force = false): ServiceProvider public function getProvider(string|ServiceProvider $provider): ?ServiceProvider { - $name = is_string($provider) ? $provider : get_class($provider); + $name = is_string($provider) ? $provider : $provider::class; return Arr::first($this->serviceProviders, function ($key, $value) use ($name) { return $value instanceof $name; @@ -134,7 +134,7 @@ public function resolveProvider($provider): ServiceProvider protected function markAsRegistered(ServiceProvider $provider): void { - $this['events']->dispatch($class = get_class($provider), [$provider]); + $this['events']->dispatch($class = $provider::class, [$provider]); $this->serviceProviders[] = $provider; diff --git a/framework/core/src/Foundation/ApplicationInfoProvider.php b/framework/core/src/Foundation/ApplicationInfoProvider.php index 563f3d55cf..b881906167 100644 --- a/framework/core/src/Foundation/ApplicationInfoProvider.php +++ b/framework/core/src/Foundation/ApplicationInfoProvider.php @@ -57,7 +57,7 @@ public function getSchedulerStatus(): string public function identifyQueueDriver(): string { // Get class name - $queue = get_class($this->queue); + $queue = $this->queue::class; // Drop the namespace $queue = Str::afterLast($queue, '\\'); // Lowercase the class name @@ -103,7 +103,7 @@ public function identifySessionDriver(bool $forWeb = false): string * And compare that to the current configured driver. */ // Get class name - $handlerName = get_class($this->sessionHandler); + $handlerName = $this->sessionHandler::class; // Drop the namespace $handlerName = Str::afterLast($handlerName, '\\'); // Lowercase the class name diff --git a/framework/core/src/Foundation/Concerns/InteractsWithLaravel.php b/framework/core/src/Foundation/Concerns/InteractsWithLaravel.php index 58ea69cd76..b0bfbd3527 100644 --- a/framework/core/src/Foundation/Concerns/InteractsWithLaravel.php +++ b/framework/core/src/Foundation/Concerns/InteractsWithLaravel.php @@ -184,7 +184,7 @@ public function getNamespace(): string public function getProviders($provider): array { - $name = is_string($provider) ? $provider : get_class($provider); + $name = is_string($provider) ? $provider : $provider::class; return Arr::where($this->serviceProviders, fn ($value) => $value instanceof $name); } diff --git a/framework/core/src/Foundation/ErrorHandling/Registry.php b/framework/core/src/Foundation/ErrorHandling/Registry.php index d7a36f8b07..6c99131730 100644 --- a/framework/core/src/Foundation/ErrorHandling/Registry.php +++ b/framework/core/src/Foundation/ErrorHandling/Registry.php @@ -54,7 +54,7 @@ private function handleKnownTypes(Throwable $error): ?HandledError if ($error instanceof KnownError) { $errorType = $error->getType(); } else { - $errorClass = get_class($error); + $errorClass = $error::class; if (isset($this->classMap[$errorClass])) { $errorType = $this->classMap[$errorClass]; } @@ -73,7 +73,7 @@ private function handleKnownTypes(Throwable $error): ?HandledError private function handleCustomTypes(Throwable $error): ?HandledError { - $errorClass = get_class($error); + $errorClass = $error::class; if (isset($this->handlerMap[$errorClass])) { $handler = new $this->handlerMap[$errorClass]; diff --git a/framework/core/src/Frontend/Compiler/Source/SourceCollector.php b/framework/core/src/Frontend/Compiler/Source/SourceCollector.php index 86619905c4..4ebf78fdca 100644 --- a/framework/core/src/Frontend/Compiler/Source/SourceCollector.php +++ b/framework/core/src/Frontend/Compiler/Source/SourceCollector.php @@ -77,7 +77,7 @@ protected function validateSourceType(SourceInterface $source): SourceInterface if (! empty($this->allowedSourceTypes) && ! $isInstanceOfOneOfTheAllowedSourceTypes) { throw new \InvalidArgumentException(sprintf( 'Source type %s is not allowed for this collector. Allowed types are: %s', - get_class($source), + $source::class, implode(', ', $this->allowedSourceTypes) )); } diff --git a/framework/core/src/Http/Server.php b/framework/core/src/Http/Server.php index 4fff8e0a09..d8c43208bb 100644 --- a/framework/core/src/Http/Server.php +++ b/framework/core/src/Http/Server.php @@ -89,7 +89,7 @@ private function cleanBootExceptionLog(Throwable $error): void $message = $error->getMessage(); $file = $error->getFile(); $line = $error->getLine(); - $type = get_class($error); + $type = $error::class; echo << diff --git a/framework/core/src/Notification/Notification.php b/framework/core/src/Notification/Notification.php index 183d13337d..7f9621b167 100644 --- a/framework/core/src/Notification/Notification.php +++ b/framework/core/src/Notification/Notification.php @@ -137,7 +137,7 @@ public function scopeWhereSubjectVisibleTo(Builder $query, User $actor): Builder */ public function scopeWhereSubject(Builder $query, AbstractModel $model): Builder { - return $query->whereSubjectModel(get_class($model)) + return $query->whereSubjectModel($model::class) ->where('subject_id', $model->getAttribute('id')); } diff --git a/framework/core/src/Search/GambitManager.php b/framework/core/src/Search/GambitManager.php index d66d637172..5b5c0d1f49 100644 --- a/framework/core/src/Search/GambitManager.php +++ b/framework/core/src/Search/GambitManager.php @@ -60,7 +60,7 @@ protected function applyGambits(SearchState $search, string $query): string foreach ($this->gambits as $gambit) { if (! $gambit instanceof GambitInterface) { throw new LogicException( - 'Gambit '.get_class($gambit).' does not implement '.GambitInterface::class + 'Gambit '.$gambit::class.' does not implement '.GambitInterface::class ); } diff --git a/framework/core/src/User/Access/Gate.php b/framework/core/src/User/Access/Gate.php index 66e1e5771e..8d6fa10459 100644 --- a/framework/core/src/User/Access/Gate.php +++ b/framework/core/src/User/Access/Gate.php @@ -50,7 +50,7 @@ public function allows(User $actor, string $ability, string|AbstractModel|null $ $appliedPolicies = []; if ($model) { - $modelClasses = is_string($model) ? [$model] : array_merge(class_parents($model), [get_class($model)]); + $modelClasses = is_string($model) ? [$model] : array_merge(class_parents($model), [$model::class]); foreach ($modelClasses as $class) { $appliedPolicies = array_merge($appliedPolicies, $this->getPolicies($class)); diff --git a/framework/core/tests/integration/extenders/FilesystemTest.php b/framework/core/tests/integration/extenders/FilesystemTest.php index 1d146a6a59..fb532119ea 100644 --- a/framework/core/tests/integration/extenders/FilesystemTest.php +++ b/framework/core/tests/integration/extenders/FilesystemTest.php @@ -51,7 +51,7 @@ public function custom_disk_exists_if_added_and_uses_local_adapter_by_default() /** @var FilesystemAdapter $uploadsDisk */ $uploadsDisk = $this->app()->getContainer()->make('filesystem')->disk('flarum-uploads'); - $this->assertEquals(get_class($uploadsDisk->getAdapter()), LocalFilesystemAdapter::class); + $this->assertEquals($uploadsDisk->getAdapter()::class, LocalFilesystemAdapter::class); } /** @@ -64,7 +64,7 @@ public function custom_disk_exists_if_added_via_invokable_class_and_uses_local_a /** @var FilesystemAdapter $uploadsDisk */ $uploadsDisk = $this->app()->getContainer()->make('filesystem')->disk('flarum-uploads'); - $this->assertEquals(get_class($uploadsDisk->getAdapter()), LocalFilesystemAdapter::class); + $this->assertEquals($uploadsDisk->getAdapter()::class, LocalFilesystemAdapter::class); } /** @@ -77,7 +77,7 @@ public function disk_uses_local_adapter_if_configured_adapter_unavailable() /** @var FilesystemAdapter $assetsDisk */ $assetsDisk = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets'); - $this->assertEquals(get_class($assetsDisk->getAdapter()), LocalFilesystemAdapter::class); + $this->assertEquals($assetsDisk->getAdapter()::class, LocalFilesystemAdapter::class); } /** @@ -90,7 +90,7 @@ public function disk_uses_local_adapter_if_configured_adapter_from_config_file_u /** @var FilesystemAdapter $assetsDisk */ $assetsDisk = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets'); - $this->assertEquals(get_class($assetsDisk->getAdapter()), LocalFilesystemAdapter::class); + $this->assertEquals($assetsDisk->getAdapter()::class, LocalFilesystemAdapter::class); } /** @@ -107,7 +107,7 @@ public function disk_uses_custom_adapter_if_configured_and_available() /** @var FilesystemAdapter $assetsDisk */ $assetsDisk = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets'); - $this->assertEquals(get_class($assetsDisk->getAdapter()), InMemoryFilesystemAdapter::class); + $this->assertEquals($assetsDisk->getAdapter()::class, InMemoryFilesystemAdapter::class); } /** @@ -124,7 +124,7 @@ public function disk_uses_custom_adapter_from_config_file_if_configured_and_avai /** @var FilesystemAdapter $assetsDisk */ $assetsDisk = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets'); - $this->assertEquals(get_class($assetsDisk->getAdapter()), InMemoryFilesystemAdapter::class); + $this->assertEquals($assetsDisk->getAdapter()::class, InMemoryFilesystemAdapter::class); } } diff --git a/framework/core/tests/integration/extenders/SessionTest.php b/framework/core/tests/integration/extenders/SessionTest.php index 82efc54564..0470b8e39a 100644 --- a/framework/core/tests/integration/extenders/SessionTest.php +++ b/framework/core/tests/integration/extenders/SessionTest.php @@ -51,7 +51,7 @@ public function custom_driver_exists_if_added() $driver = $this->app()->getContainer()->make('session')->driver('flarum-acme'); - $this->assertEquals(NullSessionHandler::class, get_class($driver->getHandler())); + $this->assertEquals(NullSessionHandler::class, $driver->getHandler()::class); } /** @@ -63,7 +63,7 @@ public function custom_driver_overrides_laravel_defined_drivers_if_added() $driver = $this->app()->getContainer()->make('session')->driver('redis'); - $this->assertEquals(NullSessionHandler::class, get_class($driver->getHandler())); + $this->assertEquals(NullSessionHandler::class, $driver->getHandler()::class); } /** @@ -75,7 +75,7 @@ public function uses_default_driver_if_driver_from_config_file_not_configured() $handler = $this->app()->getContainer()->make('session.handler'); - $this->assertEquals(FileSessionHandler::class, get_class($handler)); + $this->assertEquals(FileSessionHandler::class, $handler::class); } /** @@ -87,7 +87,7 @@ public function uses_default_driver_if_configured_driver_from_config_file_unavai $handler = $this->app()->getContainer()->make('session.handler'); - $this->assertEquals(FileSessionHandler::class, get_class($handler)); + $this->assertEquals(FileSessionHandler::class, $handler::class); } /** @@ -103,7 +103,7 @@ public function uses_custom_driver_from_config_file_if_configured_and_available( $handler = $this->app()->getContainer()->make('session.handler'); - $this->assertEquals(NullSessionHandler::class, get_class($handler)); + $this->assertEquals(NullSessionHandler::class, $handler::class); } } diff --git a/php-packages/phpstan/src/Extender/Extender.php b/php-packages/phpstan/src/Extender/Extender.php index 61e351d186..1ef6ab4f73 100644 --- a/php-packages/phpstan/src/Extender/Extender.php +++ b/php-packages/phpstan/src/Extender/Extender.php @@ -38,7 +38,7 @@ public function extends(...$args): bool foreach ($this->constructorArguments as $index => $constructorArgument) { $string = null; - switch (get_class($constructorArgument)) { + switch ($constructorArgument::class) { case Expr\ClassConstFetch::class: $string = $constructorArgument->class->toString(); break; diff --git a/php-packages/phpstan/src/Extender/Resolver.php b/php-packages/phpstan/src/Extender/Resolver.php index 804c6807d7..7042883dc7 100644 --- a/php-packages/phpstan/src/Extender/Resolver.php +++ b/php-packages/phpstan/src/Extender/Resolver.php @@ -147,7 +147,7 @@ private function resolveExtender(MethodCall $value): Extender $methodStack = array_reverse($methodStack); if (! $value->var instanceof New_) { - throw new \Exception('Unable to resolve extender for '.get_class($value->var)); + throw new \Exception('Unable to resolve extender for '.$value->var::class); } return $this->resolveExtenderNew($value->var, $methodStack);