From 7484f0d89cc0fc140ad227c127d8f97e6fad3d55 Mon Sep 17 00:00:00 2001 From: nmtho <144540995+toby7002@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:05:03 +0700 Subject: [PATCH] Fix CS --- src/thebigcrafter/omp/OhMyPMMP.php | 2 +- .../omp/helpers/UpdateChecker.php | 34 +++-- .../omp/tasks/CheckForUpdates.php | 117 ++++++++++-------- .../omp/tasks/RemovePluginTask.php | 44 +++---- src/thebigcrafter/omp/utils/Filesystem.php | 15 ++- 5 files changed, 122 insertions(+), 90 deletions(-) diff --git a/src/thebigcrafter/omp/OhMyPMMP.php b/src/thebigcrafter/omp/OhMyPMMP.php index 84b0ad8..b7e1da9 100644 --- a/src/thebigcrafter/omp/OhMyPMMP.php +++ b/src/thebigcrafter/omp/OhMyPMMP.php @@ -34,7 +34,7 @@ public function onLoad() : void public function onEnable() : void { - UpdateChecker::init($this); + UpdateChecker::init($this); $this->fetchData(); $this->saveDefaultConfig(); diff --git a/src/thebigcrafter/omp/helpers/UpdateChecker.php b/src/thebigcrafter/omp/helpers/UpdateChecker.php index 201489d..7557d48 100644 --- a/src/thebigcrafter/omp/helpers/UpdateChecker.php +++ b/src/thebigcrafter/omp/helpers/UpdateChecker.php @@ -1,18 +1,26 @@ + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\helpers; + use pocketmine\plugin\Plugin; -use pocketmine\Server; use thebigcrafter\omp\OhMyPMMP; -use thebigcrafter\omp\tasks\CheckForUpdates; - +use thebigcrafter\omp\tasks\CheckForUpdates; + class UpdateChecker { - public static function init(Plugin $plugin): void - { - OhMyPMMP::getInstance()->getServer()->getAsyncPool()->submitTask(new CheckForUpdates($plugin->getDescription()->getName(), $plugin->getDescription()->getVersion())); - } -} \ No newline at end of file + public static function init(Plugin $plugin) : void + { + OhMyPMMP::getInstance()->getServer()->getAsyncPool()->submitTask(new CheckForUpdates($plugin->getDescription()->getName(), $plugin->getDescription()->getVersion())); + } +} diff --git a/src/thebigcrafter/omp/tasks/CheckForUpdates.php b/src/thebigcrafter/omp/tasks/CheckForUpdates.php index a1b1a8d..9bf8fb1 100644 --- a/src/thebigcrafter/omp/tasks/CheckForUpdates.php +++ b/src/thebigcrafter/omp/tasks/CheckForUpdates.php @@ -1,57 +1,70 @@ - + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\tasks; + use pocketmine\scheduler\AsyncTask; use pocketmine\utils\Internet; use thebigcrafter\omp\Language; -use thebigcrafter\omp\OhMyPMMP; - +use thebigcrafter\omp\OhMyPMMP; +use function json_decode; +use function version_compare; + final class CheckForUpdates extends AsyncTask { - private string $highestVersion; - private string $artifactUrl; - public function __construct(private readonly string $name, private string $currentVersion) - { - $this->highestVersion = $currentVersion; - $this->artifactUrl = ""; - } - - public function onRun(): void - { - $res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name); - - $releases = (array) json_decode($res->getBody(), true); - - if ($releases !== null) { - /** - * @var array{'version': string, 'artifact_url': string} $release - */ - foreach ($releases as $release) { - if (version_compare($this->highestVersion, $release["version"], ">=")) { - continue; - } - - $this->highestVersion = $release["version"]; - $this->artifactUrl = $release["artifact_url"]; - } - } - - if ($this->highestVersion !== $this->currentVersion) { - $this->setResult(false); - } - } - - public function onCompletion(): void - { - if (!$this->getResult()) { - $this->artifactUrl .= "/{$this->name}_{$this->highestVersion}.phar"; - OhMyPMMP::getInstance()->getLogger()->notice( - Language::translate( - "messages.new_update", - ["name" => $this->name, "version" => $this->highestVersion, "download_url" => $this->artifactUrl] - ) - ); - } - } -} \ No newline at end of file + private string $highestVersion; + private string $artifactUrl; + public function __construct(private readonly string $name, private string $currentVersion) + { + $this->highestVersion = $currentVersion; + $this->artifactUrl = ""; + } + + public function onRun() : void + { + $res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name); + + $releases = (array) json_decode($res->getBody(), true); + + if ($releases !== null) { + /** + * @var array{'version': string, 'artifact_url': string} $release + */ + foreach ($releases as $release) { + if (version_compare($this->highestVersion, $release["version"], ">=")) { + continue; + } + + $this->highestVersion = $release["version"]; + $this->artifactUrl = $release["artifact_url"]; + } + } + + if ($this->highestVersion !== $this->currentVersion) { + $this->setResult(false); + } + } + + public function onCompletion() : void + { + if (!$this->getResult()) { + $this->artifactUrl .= "/{$this->name}_{$this->highestVersion}.phar"; + OhMyPMMP::getInstance()->getLogger()->notice( + Language::translate( + "messages.new_update", + ["name" => $this->name, "version" => $this->highestVersion, "download_url" => $this->artifactUrl] + ) + ); + } + } +} diff --git a/src/thebigcrafter/omp/tasks/RemovePluginTask.php b/src/thebigcrafter/omp/tasks/RemovePluginTask.php index 613d201..1fd5f9f 100644 --- a/src/thebigcrafter/omp/tasks/RemovePluginTask.php +++ b/src/thebigcrafter/omp/tasks/RemovePluginTask.php @@ -21,28 +21,28 @@ class RemovePluginTask extends Task { - public function __construct(private readonly string $name, private readonly bool $wipeData) - { - } - public function execute(): bool - { - $name = $this->name; - $wipeData = $this->wipeData; + public function __construct(private readonly string $name, private readonly bool $wipeData) + { + } + public function execute() : bool + { + $name = $this->name; + $wipeData = $this->wipeData; - $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); - $pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); + $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); + $pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); - if (Filesystem::exists($pluginFilePath)) { - Await::g2c(Filesystem::remove($pluginFilePath)); - } elseif (Filesystem::exists($pluginFolderPath)) { - Await::g2c(Filesystem::remove($pluginFolderPath)); - } else { - return false; - } - if ($wipeData) { - $pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); - Await::g2c(Filesystem::remove($pluginDataFolder)); - } - return true; - } + if (Filesystem::exists($pluginFilePath)) { + Await::g2c(Filesystem::remove($pluginFilePath)); + } elseif (Filesystem::exists($pluginFolderPath)) { + Await::g2c(Filesystem::remove($pluginFolderPath)); + } else { + return false; + } + if ($wipeData) { + $pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); + Await::g2c(Filesystem::remove($pluginDataFolder)); + } + return true; + } } diff --git a/src/thebigcrafter/omp/utils/Filesystem.php b/src/thebigcrafter/omp/utils/Filesystem.php index 81ff6cd..282cf8a 100644 --- a/src/thebigcrafter/omp/utils/Filesystem.php +++ b/src/thebigcrafter/omp/utils/Filesystem.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + declare(strict_types=1); namespace thebigcrafter\omp\utils; @@ -16,6 +25,7 @@ public static function remove(string $path): Generator return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path) { $fs = new \Symfony\Component\Filesystem\Filesystem(); try { + // @phpstan-ignore-next-line $resolve($fs->remove($path)); } catch (IOException $e) { $reject($e); @@ -23,9 +33,10 @@ public static function remove(string $path): Generator }); } - public static function exists(\Traversable|array|string $files): bool + // @phpstan-ignore-next-line + public static function exists(iterable|string $files): bool { $fs = new \Symfony\Component\Filesystem\Filesystem(); return $fs->exists($files); } -} \ No newline at end of file +}