UpdateChecker: improve type handling

This commit is contained in:
Dylan K. Taylor 2021-08-20 20:06:09 +01:00
parent e113981750
commit e43d39c0bc
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 10 additions and 32 deletions

View File

@ -39,8 +39,6 @@ class UpdateChecker{
protected $endpoint;
/** @var UpdateInfo|null */
protected $updateInfo = null;
/** @var VersionString|null */
protected $newVersion;
/** @var \Logger */
private $logger;
@ -63,8 +61,7 @@ class UpdateChecker{
* Callback used at the end of the update checking task
*/
public function checkUpdateCallback(UpdateInfo $updateInfo) : void{
$this->updateInfo = $updateInfo;
$this->checkUpdate();
$this->checkUpdate($updateInfo);
if($this->hasUpdate()){
(new UpdateNotifyEvent($this))->call();
if($this->server->getConfigGroup()->getPropertyBool("auto-updater.on-update.warn-console", true)){
@ -83,15 +80,19 @@ class UpdateChecker{
* Returns whether there is an update available.
*/
public function hasUpdate() : bool{
return $this->newVersion !== null;
return $this->updateInfo !== null;
}
/**
* Posts a warning to the console to tell the user there is an update available
*/
public function showConsoleUpdate() : void{
if($this->updateInfo === null){
return;
}
$newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
$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 " . $newVersion->getFullVersion(true) . " was released on " . date("D M j h:i:s Y", $this->updateInfo->date)
];
$messages[] = "Details: " . $this->updateInfo->details_url;
@ -140,13 +141,10 @@ class UpdateChecker{
/**
* Checks the update information against the current server version to decide if there's an update
*/
protected function checkUpdate() : void{
if($this->updateInfo === null){
return;
}
protected function checkUpdate(UpdateInfo $updateInfo) : void{
$currentVersion = VersionInfo::VERSION();
try{
$newVersion = new VersionString($this->updateInfo->base_version, $this->updateInfo->is_dev, $this->updateInfo->build);
$newVersion = new VersionString($updateInfo->base_version, $updateInfo->is_dev, $updateInfo->build);
}catch(\InvalidArgumentException $e){
//Invalid version returned from API, assume there's no update
$this->logger->debug("Assuming no update because \"" . $e->getMessage() . "\"");
@ -154,7 +152,7 @@ class UpdateChecker{
}
if($currentVersion->compare($newVersion) > 0 and ($currentVersion->getFullVersion() !== $newVersion->getFullVersion() or $currentVersion->getBuild() > 0)){
$this->newVersion = $newVersion;
$this->updateInfo = $updateInfo;
}
}

View File

@ -245,26 +245,6 @@ parameters:
count: 1
path: ../../../src/scheduler/DumpWorkerMemoryTask.php
-
message: "#^Cannot access property \\$date on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#"
count: 1
path: ../../../src/updater/UpdateChecker.php
-
message: "#^Cannot access property \\$details_url on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#"
count: 1
path: ../../../src/updater/UpdateChecker.php
-
message: "#^Cannot access property \\$download_url on pocketmine\\\\updater\\\\UpdateInfo\\|null\\.$#"
count: 1
path: ../../../src/updater/UpdateChecker.php
-
message: "#^Cannot call method getFullVersion\\(\\) on pocketmine\\\\utils\\\\VersionString\\|null\\.$#"
count: 1
path: ../../../src/updater/UpdateChecker.php
-
message: "#^Method pocketmine\\\\utils\\\\Config\\:\\:fixYAMLIndexes\\(\\) should return string but returns string\\|null\\.$#"
count: 1