From 373b79c555f1a945b697cc89f3986df09d09b0ee Mon Sep 17 00:00:00 2001 From: Alexandr Chernyaev Date: Mon, 11 Nov 2024 01:32:40 +0300 Subject: [PATCH] The code has been refactored to align with modern PHP standards. --- src/Metrics/Chartable.php | 2 +- src/Metrics/GroupCollection.php | 2 +- src/Metrics/TimeCollection.php | 6 ++-- src/Platform/Commands/InstallCommand.php | 36 +++++++++++++--------- src/Platform/Events/AddRoleEvent.php | 10 +----- src/Platform/Events/InstallEvent.php | 8 +---- src/Platform/Events/RemoveRoleEvent.php | 11 +------ src/Platform/Events/ReplicateFileEvent.php | 16 +--------- src/Platform/Events/UploadedFileEvent.php | 8 +---- 9 files changed, 30 insertions(+), 69 deletions(-) diff --git a/src/Metrics/Chartable.php b/src/Metrics/Chartable.php index 499f75bc4..5205ca01b 100644 --- a/src/Metrics/Chartable.php +++ b/src/Metrics/Chartable.php @@ -34,7 +34,7 @@ public function scopeCountForGroup(Builder $builder, string $groupColumn): Group */ private function groupByDays(Builder $builder, string $value, $startDate = null, $stopDate = null, ?string $dateColumn = null): TimeCollection { - $dateColumn = $dateColumn ?? $builder->getModel()->getCreatedAtColumn(); + $dateColumn ??= $builder->getModel()->getCreatedAtColumn(); $startDate = empty($startDate) ? Carbon::now()->subMonth() diff --git a/src/Metrics/GroupCollection.php b/src/Metrics/GroupCollection.php index 7ab24d6be..b58fc8aec 100644 --- a/src/Metrics/GroupCollection.php +++ b/src/Metrics/GroupCollection.php @@ -16,7 +16,7 @@ class GroupCollection extends Collection public function toChart(?\Closure $closure = null): array { // If the closure is not set, we define a default one that returns the original label. - $closure = $closure ?? static fn ($label) => $label; + $closure ??= static fn ($label) => $label; return $this ->sortByDesc('value') diff --git a/src/Metrics/TimeCollection.php b/src/Metrics/TimeCollection.php index 4d3d63368..6ca81d554 100644 --- a/src/Metrics/TimeCollection.php +++ b/src/Metrics/TimeCollection.php @@ -34,7 +34,7 @@ public function makeFromKeyValue($values, string $format = 'Y-m-d'): TimeCollect */ public function toChart(string $name, ?\Closure $closure = null): array { - $closure = $closure ?? static fn ($label) => $label; + $closure ??= static fn ($label) => $label; return [ 'name' => $name, @@ -94,8 +94,6 @@ public function transformLabel(callable $callback): TimeCollection */ public function withoutZeroValues(): TimeCollection { - return $this->filter(function (array $item) { - return $item['value'] !== 0; - }); + return $this->filter(fn (array $item) => $item['value'] !== 0); } } diff --git a/src/Platform/Commands/InstallCommand.php b/src/Platform/Commands/InstallCommand.php index e0114d026..041933083 100644 --- a/src/Platform/Commands/InstallCommand.php +++ b/src/Platform/Commands/InstallCommand.php @@ -169,24 +169,30 @@ private function replaceInFiles(string $directory, string $search, string $repla return $this; } - $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); + $files = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($directory) + ); - // Проходим по всем файлам в директории + // Iterate through all files in the directory foreach ($files as $file) { - // Проверяем, что это файл и имеет расширение .php - if ($file->isFile() && $file->getExtension() === 'php') { - $filePath = $file->getRealPath(); - $fileContents = file_get_contents($filePath); - - // Если содержимое файла содержит старый namespace - if (strpos($fileContents, $search) !== false) { - // Заменяем старый namespace на новый - $updatedContents = str_replace($search, $replace, $fileContents); - - // Сохраняем изменения - file_put_contents($filePath, $updatedContents); - } + // Skip if not a .php file + if ($file->getExtension() !== 'php') { + continue; } + + $filePath = $file->getRealPath(); + $fileContents = file_get_contents($filePath); + + // Skip if the file does not contain the old namespace + if (! str_contains($fileContents, $search)) { + continue; + } + + // Replace the old namespace with the new one + $updatedContents = str_replace($search, $replace, $fileContents); + + // Save the changes + file_put_contents($filePath, $updatedContents); } return $this; diff --git a/src/Platform/Events/AddRoleEvent.php b/src/Platform/Events/AddRoleEvent.php index c3a3c3e97..ce40f833d 100644 --- a/src/Platform/Events/AddRoleEvent.php +++ b/src/Platform/Events/AddRoleEvent.php @@ -15,13 +15,6 @@ class AddRoleEvent { use SerializesModels; - /** - * The authenticated user. - * - * @var User - */ - public $user; - /** * The role(s) that were added to the user. * @@ -35,9 +28,8 @@ class AddRoleEvent * @param mixed $user The user to whom the role(s) is added * @param mixed $role The role(s) to be added */ - public function __construct($user, $role) + public function __construct(public mixed $user, mixed $role) { - $this->user = $user; $this->roles = collect($role); } } diff --git a/src/Platform/Events/InstallEvent.php b/src/Platform/Events/InstallEvent.php index 6cc7ca330..2527666a0 100644 --- a/src/Platform/Events/InstallEvent.php +++ b/src/Platform/Events/InstallEvent.php @@ -11,16 +11,10 @@ */ class InstallEvent { - /** - * @var Command - */ - public $command; - /** * InstallEvent constructor. */ - public function __construct(Command $command) + public function __construct(public Command $command) { - $this->command = $command; } } diff --git a/src/Platform/Events/RemoveRoleEvent.php b/src/Platform/Events/RemoveRoleEvent.php index ac2ba41e0..00a858eaa 100644 --- a/src/Platform/Events/RemoveRoleEvent.php +++ b/src/Platform/Events/RemoveRoleEvent.php @@ -6,19 +6,11 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; -use Orchid\Platform\Models\User; class RemoveRoleEvent { use SerializesModels; - /** - * The authenticated user. - * - * @var User - */ - public $user; - /** * Roles that will be removed. * @@ -32,9 +24,8 @@ class RemoveRoleEvent * @param mixed $user The user object this event relates to. * @param mixed $role The role(s) to remove. Can accept either a Collection or an array. */ - public function __construct($user, $role) + public function __construct(public mixed $user, mixed $role) { - $this->user = $user; $this->roles = collect($role); } } diff --git a/src/Platform/Events/ReplicateFileEvent.php b/src/Platform/Events/ReplicateFileEvent.php index 5414abed3..f7396d3fe 100644 --- a/src/Platform/Events/ReplicateFileEvent.php +++ b/src/Platform/Events/ReplicateFileEvent.php @@ -14,24 +14,10 @@ class ReplicateFileEvent { use SerializesModels; - /** - * @var Attachment - */ - public $attachment; - - /** - * The timestamp when the event occurred. - * - * @var int - */ - public $time; - /** * ReplicateFileEvent constructor. */ - public function __construct(Attachment $attachment, int $time) + public function __construct(public Attachment $attachment, public int $time) { - $this->attachment = $attachment; - $this->time = $time; } } diff --git a/src/Platform/Events/UploadedFileEvent.php b/src/Platform/Events/UploadedFileEvent.php index 5cf2ed593..1c2f28ac3 100644 --- a/src/Platform/Events/UploadedFileEvent.php +++ b/src/Platform/Events/UploadedFileEvent.php @@ -14,16 +14,10 @@ class UploadedFileEvent { use SerializesModels; - /** - * @var Attachment - */ - public $attachment; - /** * UploadedFileEvent constructor. */ - public function __construct(Attachment $attachment) + public function __construct(public Attachment $attachment) { - $this->attachment = $attachment; } }