diff --git a/src/updater/AutoUpdater.php b/src/updater/AutoUpdater.php index 5c987ad69..1851b0eda 100644 --- a/src/updater/AutoUpdater.php +++ b/src/updater/AutoUpdater.php @@ -41,10 +41,7 @@ class AutoUpdater{ protected $server; /** @var string */ protected $endpoint; - /** - * @var mixed[]|null - * @phpstan-var array|null - */ + /** @var UpdateInfo|null */ protected $updateInfo = null; /** @var VersionString|null */ protected $newVersion; @@ -69,10 +66,9 @@ class AutoUpdater{ /** * Callback used at the end of the update checking task * - * @param mixed[] $updateInfo - * @phpstan-param array $updateInfo + * @param UpdateInfo $updateInfo */ - public function checkUpdateCallback(array $updateInfo) : void{ + public function checkUpdateCallback(UpdateInfo $updateInfo) : void{ $this->updateInfo = $updateInfo; $this->checkUpdate(); if($this->hasUpdate()){ @@ -101,12 +97,12 @@ class AutoUpdater{ */ public function showConsoleUpdate() : void{ $messages = [ - "Your version of " . $this->server->getName() . " is out of date. Version " . $this->newVersion->getFullVersion(true) . " was released on " . date("D M j h:i:s Y", $this->updateInfo["date"]) + "Your version of " . $this->server->getName() . " is out of date. Version " . $this->newVersion->getFullVersion(true) . " was released on " . date("D M j h:i:s Y", $this->updateInfo->date) ]; - if($this->updateInfo["details_url"] !== null){ - $messages[] = "Details: " . $this->updateInfo["details_url"]; + if($this->updateInfo->details_url !== null){ + $messages[] = "Details: " . $this->updateInfo->details_url; } - $messages[] = "Download: " . $this->updateInfo["download_url"]; + $messages[] = "Download: " . $this->updateInfo->download_url; $this->printConsoleMessage($messages, \LogLevel::WARNING); } @@ -148,10 +144,9 @@ class AutoUpdater{ /** * Returns the last retrieved update data. * - * @return mixed[]|null - * @phpstan-return array|null + * @return UpdateInfo|null */ - public function getUpdateInfo() : ?array{ + public function getUpdateInfo() : ?UpdateInfo{ return $this->updateInfo; } @@ -171,7 +166,7 @@ class AutoUpdater{ } $currentVersion = new VersionString(\pocketmine\BASE_VERSION, \pocketmine\IS_DEVELOPMENT_BUILD, \pocketmine\BUILD_NUMBER); try{ - $newVersion = new VersionString($this->updateInfo["base_version"], $this->updateInfo["is_dev"], $this->updateInfo["build"]); + $newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build); }catch(\InvalidArgumentException $e){ //Invalid version returned from API, assume there's no update $this->logger->debug("Assuming no update because \"" . $e->getMessage() . "\""); diff --git a/src/updater/UpdateCheckTask.php b/src/updater/UpdateCheckTask.php index 012905785..22b1f5acb 100644 --- a/src/updater/UpdateCheckTask.php +++ b/src/updater/UpdateCheckTask.php @@ -26,6 +26,7 @@ namespace pocketmine\updater; use pocketmine\scheduler\AsyncTask; use pocketmine\utils\Internet; use function is_array; +use function is_string; use function json_decode; class UpdateCheckTask extends AsyncTask{ @@ -52,19 +53,19 @@ class UpdateCheckTask extends AsyncTask{ if($response !== false){ $response = json_decode($response, true); if(is_array($response)){ - if( - isset($response["base_version"]) and - isset($response["is_dev"]) and - isset($response["build"]) and - isset($response["date"]) and - isset($response["download_url"]) - ){ - $response["details_url"] = $response["details_url"] ?? null; - $this->setResult($response); - }elseif(isset($response["error"])){ + if(isset($response["error"]) and is_string($response["error"])){ $this->error = $response["error"]; }else{ - $this->error = "Invalid response data"; + $mapper = new \JsonMapper(); + $mapper->bExceptionOnMissingData = true; + $mapper->bEnforceMapType = false; + try{ + /** @var UpdateInfo $responseObj */ + $responseObj = $mapper->map($response, new UpdateInfo()); + $this->setResult($responseObj); + }catch(\JsonMapper_Exception $e){ + $this->error = "Invalid JSON response data: " . $e->getMessage(); + } } }else{ $this->error = "Invalid response data"; @@ -76,7 +77,9 @@ class UpdateCheckTask extends AsyncTask{ /** @var AutoUpdater $updater */ $updater = $this->fetchLocal(self::TLS_KEY_UPDATER); if($this->hasResult()){ - $updater->checkUpdateCallback($this->getResult()); + /** @var UpdateInfo $response */ + $response = $this->getResult(); + $updater->checkUpdateCallback($response); }else{ $updater->checkUpdateError($this->error); } diff --git a/src/updater/UpdateInfo.php b/src/updater/UpdateInfo.php new file mode 100644 index 000000000..2b0d1eb4c --- /dev/null +++ b/src/updater/UpdateInfo.php @@ -0,0 +1,96 @@ +, mixed given\\.$#" - count: 1 - path: ../../../src/updater/UpdateCheckTask.php - - message: "#^Parameter \\#3 \\$length of function substr expects int, mixed given\\.$#" count: 1 diff --git a/tests/phpstan/configs/l8-baseline.neon b/tests/phpstan/configs/l8-baseline.neon index 4b7cfbd5a..b8b48f977 100644 --- a/tests/phpstan/configs/l8-baseline.neon +++ b/tests/phpstan/configs/l8-baseline.neon @@ -800,23 +800,23 @@ parameters: count: 1 path: ../../../src/thread/Worker.php + - + message: "#^Cannot access property \\$date on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#" + count: 1 + path: ../../../src/updater/AutoUpdater.php + - message: "#^Cannot call method getFullVersion\\(\\) on pocketmine\\\\utils\\\\VersionString\\|null\\.$#" count: 1 path: ../../../src/updater/AutoUpdater.php - - message: "#^Offset 'date' does not exist on array\\\\|null\\.$#" - count: 1 - path: ../../../src/updater/AutoUpdater.php - - - - message: "#^Offset 'details_url' does not exist on array\\\\|null\\.$#" + message: "#^Cannot access property \\$details_url on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#" count: 2 path: ../../../src/updater/AutoUpdater.php - - message: "#^Offset 'download_url' does not exist on array\\\\|null\\.$#" + message: "#^Cannot access property \\$download_url on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#" count: 1 path: ../../../src/updater/AutoUpdater.php