From f43df4c7c2a5f6a612ade39f74a2f5b6c4681f03 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 10 Oct 2021 16:07:06 +0200 Subject: [PATCH] phpstan fixes --- Account/Account.php | 2 +- Application/ApplicationManager.php | 21 ++++++++++++++++----- Application/InstallerAbstract.php | 18 ++++++++++++++++-- DataStorage/Database/DataMapperAbstract.php | 8 ++++---- DataStorage/Session/FileSession.php | 2 +- DataStorage/Session/FileSessionHandler.php | 2 +- DataStorage/Session/HttpSession.php | 2 +- Module/InstallerAbstract.php | 16 ++++++++++++---- Module/ModuleManager.php | 6 +++++- Module/UninstallerAbstract.php | 8 ++++++-- Stdlib/Base/SmartDateTime.php | 8 +++++++- Stdlib/Graph/Graph.php | 2 +- 12 files changed, 71 insertions(+), 24 deletions(-) diff --git a/Account/Account.php b/Account/Account.php index 402288b5a..cf357e102 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -211,7 +211,7 @@ class Account implements \JsonSerializable, ArrayableInterface * * @param int $id Group id * - * @return void + * @return bool * * @since 1.0.0 */ diff --git a/Application/ApplicationManager.php b/Application/ApplicationManager.php index 2d1114338..c9bce9eef 100644 --- a/Application/ApplicationManager.php +++ b/Application/ApplicationManager.php @@ -98,7 +98,7 @@ final class ApplicationManager $destination = \rtrim($destination, '\\/'); $source = \rtrim($source, '/\\'); - if (!\is_dir($source) || \is_dir($destination) || !\is_file($source . '/Admin/Installer.php')) { + if (!\is_dir($source) || ($path = \realpath($destination)) === false || !\is_file($source . '/Admin/Installer.php')) { return false; } @@ -109,8 +109,9 @@ final class ApplicationManager $this->installFiles($source, $destination); $this->replacePlaceholder($destination); - $classPath = \substr(\realpath($destination) . '/Admin/Installer', \strlen(\realpath(__DIR__ . '/../../'))); + $classPath = \substr($path . '/Admin/Installer', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); + // @var class-string $class $class = \str_replace('/', '\\', $classPath); $class::install($this->app->dbPool, $info, $this->app->appSettings); @@ -132,7 +133,7 @@ final class ApplicationManager public function uninstall(string $source) : bool { $source = \rtrim($source, '/\\'); - if (!\is_dir($source) || !\is_file($source . '/Admin/Uninstaller.php')) { + if (($path = \realpath($source)) === false || !\is_file($source . '/Admin/Uninstaller.php')) { return false; } @@ -140,8 +141,9 @@ final class ApplicationManager $info = $this->loadInfo($source . '/info.json'); $this->installed[$info->getInternalName()] = $info; - $classPath = \substr(\realpath($source) . '/Admin/Uninstaller', $t = \strlen(\realpath(__DIR__ . '/../../'))); + $classPath = \substr($path . '/Admin/Uninstaller', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); + // @var class-string $class $class = \str_replace('/', '\\', $classPath); $class::uninstall($this->app->dbPool, $info, $this->app->appSettings); @@ -169,7 +171,12 @@ final class ApplicationManager return; } - $classPath = \substr(\realpath($appPath) . '/Admin/Installer', \strlen(\realpath(__DIR__ . '/../../'))); + if (($path = \realpath($appPath)) === false) { + return; // @codeCoverageIgnore + } + + // @var class-string $class + $classPath = \substr($path . '/Admin/Installer', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); $class = \str_replace('/', '\\', $classPath); /** @var $class InstallerAbstract */ @@ -223,6 +230,10 @@ final class ApplicationManager if (empty($this->installed) || !$useCache) { $apps = \scandir($basePath); + if ($apps === false) { + return $this->installed; // @codeCoverageIgnore + } + foreach ($apps as $app) { if ($app === '.' || $app === '..' || !\is_file($basePath . '/' . $app . '/info.json')) { continue; diff --git a/Application/InstallerAbstract.php b/Application/InstallerAbstract.php index e4a802af5..dfbdb42f2 100644 --- a/Application/InstallerAbstract.php +++ b/Application/InstallerAbstract.php @@ -73,6 +73,10 @@ abstract class InstallerAbstract } $dirs = \scandir($path); + if ($dirs === false) { + return; // @codeCoverageIgnore + } + foreach ($dirs as $dir) { if (!\is_dir($path. '/' . $dir) || $dir === '.' || $dir === '..') { continue; @@ -130,8 +134,13 @@ abstract class InstallerAbstract */ protected static function activate(DatabasePool $dbPool, ApplicationInfo $info) : void { - $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + if (($path = \realpath(static::PATH)) === false) { + return; // @codeCoverageIgnore + } + $classPath = \substr($path . '/Status', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); + + // @var class-string $class $class = \str_replace('/', '\\', $classPath); if (!Autoloader::exists($class)) { @@ -152,8 +161,13 @@ abstract class InstallerAbstract */ public static function reInit(ApplicationInfo $info) : void { - $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + if (($path = \realpath(static::PATH)) === false) { + return; // @codeCoverageIgnore + } + $classPath = \substr($path . '/Status', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); + + // @var class-string $class $class = \str_replace('/', '\\', $classPath); if (!Autoloader::exists($class)) { diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index e88f5c566..483992be1 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -2113,7 +2113,7 @@ class DataMapperAbstract implements DataMapperInterface */ public static function populateOwnsOne(string $member, array $result, int $depth = 3, mixed $default = null) : mixed { - /** @var self|string $mapper */ + /** @var class-string $mapper */ $mapper = static::$ownsOne[$member]['mapper']; if ($depth < 1) { @@ -2153,7 +2153,7 @@ class DataMapperAbstract implements DataMapperInterface */ public static function populateOwnsOneArray(string $member, array $result, int $depth = 3, mixed $default = null) : array { - /** @var self|string $mapper */ + /** @var class-string $mapper */ $mapper = static::$ownsOne[$member]['mapper']; if ($depth < 1) { @@ -2194,7 +2194,7 @@ class DataMapperAbstract implements DataMapperInterface */ public static function populateBelongsTo(string $member, array $result, int $depth = 3, mixed $default = null) : mixed { - /** @var self|string $mapper */ + /** @var class-string $mapper */ $mapper = static::$belongsTo[$member]['mapper']; if ($depth < 1) { @@ -2242,7 +2242,7 @@ class DataMapperAbstract implements DataMapperInterface */ public static function populateBelongsToArray(string $member, array $result, int $depth = 3, mixed $default = null) : array { - /** @var self|string $mapper */ + /** @var class-string $mapper */ $mapper = static::$belongsTo[$member]['mapper']; if ($depth < 1) { diff --git a/DataStorage/Session/FileSession.php b/DataStorage/Session/FileSession.php index 12eb67169..13162df3a 100644 --- a/DataStorage/Session/FileSession.php +++ b/DataStorage/Session/FileSession.php @@ -99,7 +99,7 @@ class FileSession implements SessionInterface $this->sessionData = $_SESSION ?? []; $_SESSION = null; $this->sessionData['lastActivity'] = \time(); - $this->sid = \session_id(); + $this->sid = (string) \session_id(); } /** diff --git a/DataStorage/Session/FileSessionHandler.php b/DataStorage/Session/FileSessionHandler.php index 8c7f6f64e..9849aaf74 100644 --- a/DataStorage/Session/FileSessionHandler.php +++ b/DataStorage/Session/FileSessionHandler.php @@ -59,7 +59,7 @@ final class FileSessionHandler implements \SessionHandlerInterface, \SessionIdIn */ public function create_sid() : string { - return \session_create_id('s-'); + return ($sid = \session_create_id('s-')) === false ? '' : $sid; } /** diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php index eaf0c08c9..b52a601f2 100644 --- a/DataStorage/Session/HttpSession.php +++ b/DataStorage/Session/HttpSession.php @@ -105,7 +105,7 @@ final class HttpSession implements SessionInterface $this->sessionData = $_SESSION ?? []; $_SESSION = null; $this->sessionData['lastActivity'] = \time(); - $this->sid = \session_id(); + $this->sid = (string) \session_id(); $this->setCsrfProtection(); } diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index f6c0a82de..a50a6d4e3 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -95,9 +95,13 @@ abstract class InstallerAbstract */ protected static function activate(DatabasePool $dbPool, ModuleInfo $info) : void { - $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + if (($path = \realpath(static::PATH)) === false) { + return; // @codeCoverageIgnore + } - /** @var StatusAbstract $class */ + $classPath = \substr($path . '/Status', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); + + /** @var class-string $class */ $class = \str_replace('/', '\\', $classPath); if (!Autoloader::exists($class)) { @@ -119,9 +123,13 @@ abstract class InstallerAbstract */ public static function reInit(ModuleInfo $info, ApplicationInfo $appInfo = null) : void { - $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + if (($path = \realpath(static::PATH)) === false) { + return; // @codeCoverageIgnore + } - /** @var StatusAbstract $class */ + $classPath = \substr($path . '/Status', \strlen((string) \realpath(__DIR__ . '/../../'))); + + /** @var class-string $class */ $class = \str_replace('/', '\\', $classPath); if (!Autoloader::exists($class)) { diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 8970d4e1d..7a4cf39bc 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -266,7 +266,11 @@ final class ModuleManager \chdir($this->modulePath); $files = \glob('*', \GLOB_ONLYDIR); - $c = $files === false ? 0 : \count($files); + if ($files === false) { + return $this->all; // @codeCoverageIgnore + } + + $c = \count($files); for ($i = 0; $i < $c; ++$i) { $info = $this->loadInfo($files[$i]); diff --git a/Module/UninstallerAbstract.php b/Module/UninstallerAbstract.php index 61433bb59..b97fd5299 100644 --- a/Module/UninstallerAbstract.php +++ b/Module/UninstallerAbstract.php @@ -65,8 +65,12 @@ abstract class UninstallerAbstract */ protected static function deactivate(DatabasePool $dbPool, ModuleInfo $info) : void { - /** @var StatusAbstract $class */ - $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + if (($path = \realpath(static::PATH)) === false) { + return; // @codeCoverageIgnore; + } + + /** @var string $classPath */ + $classPath = \substr($path . '/Status', \strlen((string) \realpath(__DIR__ . '/../../'))); /** @var StatusAbstract $class */ $class = \str_replace('/', '\\', $classPath); diff --git a/Stdlib/Base/SmartDateTime.php b/Stdlib/Base/SmartDateTime.php index b49df8a72..bb23bc7d6 100644 --- a/Stdlib/Base/SmartDateTime.php +++ b/Stdlib/Base/SmartDateTime.php @@ -211,7 +211,13 @@ class SmartDateTime extends \DateTime */ public function getFirstDayOfMonth() : int { - return \getdate(\mktime(0, 0, 0, (int) $this->format('m'), 1, (int) $this->format('Y')))['wday']; + $time = \mktime(0, 0, 0, (int) $this->format('m'), 1, (int) $this->format('Y')); + + if ($time === false) { + return -1; // @codeCoverageIgnore + } + + return \getdate($time)['wday']; } /** diff --git a/Stdlib/Graph/Graph.php b/Stdlib/Graph/Graph.php index c5a04005e..9c356443c 100644 --- a/Stdlib/Graph/Graph.php +++ b/Stdlib/Graph/Graph.php @@ -336,7 +336,7 @@ class Graph ->setWeight($this->getEdge( $node1->getId(), $node2->getId() - )->getWeight() + )?->getWeight() ?? 0.0 ); }