From d5ec816415df1b25dc203396ed10ccb7fd070ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 10 Dec 2022 12:30:58 +0100 Subject: [PATCH] chore: Remove unused classes and fix types. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/SP/Domain/Account/Services/AccountAcl.php | 9 -- lib/SP/Mvc/Model/QueryAssignment.php | 85 ---------- lib/SP/Mvc/Model/QueryJoin.php | 69 --------- lib/SP/Util/Connection.php | 146 ------------------ lib/SP/Util/ConnectionInterface.php | 59 ------- lib/SP/Util/DateUtil.php | 8 +- lib/SP/Util/ErrorUtil.php | 5 +- lib/SP/Util/Util.php | 17 +- lib/SP/Util/VersionUtil.php | 22 ++- 9 files changed, 26 insertions(+), 394 deletions(-) delete mode 100644 lib/SP/Mvc/Model/QueryAssignment.php delete mode 100644 lib/SP/Mvc/Model/QueryJoin.php delete mode 100644 lib/SP/Util/Connection.php delete mode 100644 lib/SP/Util/ConnectionInterface.php diff --git a/lib/SP/Domain/Account/Services/AccountAcl.php b/lib/SP/Domain/Account/Services/AccountAcl.php index 460c4e0e..3a151041 100644 --- a/lib/SP/Domain/Account/Services/AccountAcl.php +++ b/lib/SP/Domain/Account/Services/AccountAcl.php @@ -432,13 +432,4 @@ class AccountAcl return $this; } - - public function reset(): void - { - foreach (get_class_vars(__CLASS__) as $property => $value) { - if (str_starts_with($property, 'show')) { - $this->{$property} = false; - } - } - } } diff --git a/lib/SP/Mvc/Model/QueryAssignment.php b/lib/SP/Mvc/Model/QueryAssignment.php deleted file mode 100644 index 4ab04599..00000000 --- a/lib/SP/Mvc/Model/QueryAssignment.php +++ /dev/null @@ -1,85 +0,0 @@ -. - */ - -namespace SP\Mvc\Model; - -/** - * Class QueryAssignment - * - * @package SP\Mvc\Model - */ -final class QueryAssignment -{ - protected array $fields = []; - protected array $values = []; - - /** - * @param string $field - * @param $value - * - * @return $this - */ - public function addField(string $field, $value): QueryAssignment - { - if (strpos($field, '=') === false) { - $this->fields[] = $field . ' = ?'; - $this->values[] = $value; - } - - return $this; - } - - public function setFields(array $fields, array $values): QueryAssignment - { - $this->fields = array_map( - static function ($value) { - return strpos($value, '=') === false - ? "$value = ?" - : $value; - }, - $fields - ); - - $this->values = array_merge($this->values, $values); - - return $this; - } - - public function getAssignments(): ?string - { - return $this->hasFields() - ? implode(',', $this->fields) - : null; - } - - public function hasFields(): bool - { - return count($this->fields) > 0; - } - - public function getValues(): array - { - return $this->values; - } -} \ No newline at end of file diff --git a/lib/SP/Mvc/Model/QueryJoin.php b/lib/SP/Mvc/Model/QueryJoin.php deleted file mode 100644 index e2bdf3f0..00000000 --- a/lib/SP/Mvc/Model/QueryJoin.php +++ /dev/null @@ -1,69 +0,0 @@ -. - */ - -namespace SP\Mvc\Model; - -/** - * Class QueryJoin - * - * @package SP\Mvc\Model - */ -final class QueryJoin -{ - protected array $join = []; - protected array $param = []; - - public function addJoin(string $join, ?array $params = null): QueryJoin - { - $this->join[] = $join; - - if ($params !== null) { - $this->param = array_merge($this->param, $params); - } - - return $this; - } - - public function getJoins(): ?string - { - return $this->hasJoins() - ? implode(PHP_EOL, $this->join) - : null; - } - - public function hasJoins(): bool - { - return count($this->join) !== 0; - } - - public function getParams(): array - { - return $this->param; - } - - public function getJoinsCount(): int - { - return count($this->join); - } -} \ No newline at end of file diff --git a/lib/SP/Util/Connection.php b/lib/SP/Util/Connection.php deleted file mode 100644 index 0f079446..00000000 --- a/lib/SP/Util/Connection.php +++ /dev/null @@ -1,146 +0,0 @@ -. - */ - -namespace SP\Util; - -use SP\Core\Exceptions\SPException; - -/** - * Class Connection para crear conexiones TCP o UDP - * - * @package SP\Util - */ -final class Connection implements ConnectionInterface -{ - /** - * @var resource|false - */ - protected $socket; - - protected string $host; - protected int $port; - protected int $errorno = 0; - protected string $errorstr = ''; - - /** - * @param $host string El host a conectar - * @param $port int El puerto a conectar - */ - public function __construct(string $host, int $port) - { - $this->host = gethostbyname($host); - $this->port = $port; - } - - /** - * Obtener un socket - * - * @param $type int EL tipo de socket TCP/UDP - * - * @return resource - * @throws SPException - */ - public function getSocket(int $type) - { - if ($type === self::TYPE_UDP) { - $this->socket = $this->getUDPSocket(); - } else { - $this->socket = $this->getTCPSocket(); - } - - if ($this->socket === false) { - throw new SPException($this->getSocketError(), SPException::WARNING); - } - - stream_set_timeout($this->socket, self::SOCKET_TIMEOUT); - - return $this->socket; - } - - /** - * Obtener un socket del tipo UDP - * - * @return resource - */ - private function getUDPSocket() - { - return stream_socket_client( - 'udp://'.$this->host.':'.$this->port, - $this->errorno, - $this->errorstr, - self::SOCKET_TIMEOUT - ); - } - - /** - * Obtener un socket del tipo TCP - * - * @return resource - */ - private function getTCPSocket() - { - return stream_socket_client( - 'tcp://'.$this->host.':'.$this->port, - $this->errorno, - $this->errorstr, - self::SOCKET_TIMEOUT - ); - } - - /** - * Obtener el último error del socket - */ - public function getSocketError(): string - { - return sprintf('%s (%d)', $this->errorstr, $this->errorno); - } - - /** - * Cerrar el socket - */ - public function closeSocket() - { - fclose($this->socket); - } - - /** - * Enviar un mensaje al socket - * - * @throws SPException - */ - public function send(string $message): int - { - if (!is_resource($this->socket)) { - throw new SPException(__u('Socket not initialized'), SPException::WARNING); - } - - $nBytes = @fwrite($this->socket, $message); - - if ($nBytes === false) { - throw new SPException(__u('Error while sending the data'), SPException::WARNING, $this->getSocketError()); - } - - return $nBytes; - } -} \ No newline at end of file diff --git a/lib/SP/Util/ConnectionInterface.php b/lib/SP/Util/ConnectionInterface.php deleted file mode 100644 index 93f36753..00000000 --- a/lib/SP/Util/ConnectionInterface.php +++ /dev/null @@ -1,59 +0,0 @@ -. - */ - -namespace SP\Util; - -/** - * Class ConnectionInterface - * - * @package SP\Util - */ -interface ConnectionInterface -{ - public const TYPE_TCP = 1; - public const TYPE_UDP = 2; - public const SOCKET_TIMEOUT = 10; - - /** - * Obtener un socket - * - * @return resource - */ - public function getSocket(int $type); - - /** - * Cerrar un socket - */ - public function closeSocket(); - - /** - * Obtener el último error del socket - */ - public function getSocketError(): string; - - /** - * Enviar un mensaje al socket - */ - public function send(string $message); -} \ No newline at end of file diff --git a/lib/SP/Util/DateUtil.php b/lib/SP/Util/DateUtil.php index 9fa07677..a677f35f 100644 --- a/lib/SP/Util/DateUtil.php +++ b/lib/SP/Util/DateUtil.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -37,10 +37,10 @@ final class DateUtil * @param int|string $date * @param string $format * - * @return false|string + * @return int|string */ - public static function getDateFromUnix($date, string $format = 'Y-m-d H:i') + public static function getDateFromUnix(int|string $date, string $format = 'Y-m-d H:i'): int|string { return is_numeric($date) ? date($format, $date) : $date; } -} \ No newline at end of file +} diff --git a/lib/SP/Util/ErrorUtil.php b/lib/SP/Util/ErrorUtil.php index 143fd1cd..c5981ca4 100644 --- a/lib/SP/Util/ErrorUtil.php +++ b/lib/SP/Util/ErrorUtil.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -32,6 +32,7 @@ use SP\Core\Exceptions\SPException; use SP\Domain\User\Services\UpdatedMasterPassException; use SP\Mvc\View\Template; use SP\Mvc\View\TemplateInterface; +use function SP\processException; /** * Class ErrorUtil @@ -176,4 +177,4 @@ final class ErrorUtil 'hint' => __('Please contact to the administrator'), ]; } -} \ No newline at end of file +} diff --git a/lib/SP/Util/Util.php b/lib/SP/Util/Util.php index 6a6a5b78..6fff6965 100644 --- a/lib/SP/Util/Util.php +++ b/lib/SP/Util/Util.php @@ -24,8 +24,10 @@ namespace SP\Util; +use JetBrains\PhpStorm\NoReturn; use SP\Infrastructure\File\FileException; use SP\Infrastructure\File\FileHandler; +use function SP\logger; defined('APP_ROOT') || die(); @@ -39,12 +41,13 @@ final class Util * * @return bool|string */ - public static function getTempDir() + public static function getTempDir(): bool|string { $sysTmp = sys_get_temp_dir(); - $file = 'syspass.test'; - $checkDir = static function ($dir) use ($file) { + $checkDir = static function ($dir) { + $file = 'syspass.test'; + if (file_exists($dir.DIRECTORY_SEPARATOR.$file)) { return $dir; } @@ -70,7 +73,7 @@ final class Util * * FIXME */ - public static function logout(): void + #[NoReturn] public static function logout(): void { exit(''); } @@ -116,7 +119,7 @@ final class Util * @author Samuel Levy * */ - public static function boolval($in, bool $strict = false): bool + public static function boolval(mixed $in, bool $strict = false): bool { $in = is_string($in) ? strtolower($in) : $in; @@ -240,7 +243,7 @@ final class Util * @return bool|string * @throws \JsonException */ - public static function getAppLock() + public static function getAppLock(): bool|string { try { $file = new FileHandler(LOCK_FILE); @@ -251,7 +254,7 @@ final class Util 512, JSON_THROW_ON_ERROR ); - } catch (FileException $e) { + } catch (FileException) { return false; } } diff --git a/lib/SP/Util/VersionUtil.php b/lib/SP/Util/VersionUtil.php index aa3ec0f4..9dd49b5b 100644 --- a/lib/SP/Util/VersionUtil.php +++ b/lib/SP/Util/VersionUtil.php @@ -49,7 +49,7 @@ final class VersionUtil * * @return bool True if $currentVersion is lower than $upgradeableVersion */ - public static function checkVersion(string $currentVersion, $upgradeableVersion): bool + public static function checkVersion(string $currentVersion, array|string $upgradeableVersion): bool { if (is_array($upgradeableVersion)) { $upgradeableVersion = array_pop($upgradeableVersion); @@ -78,9 +78,11 @@ final class VersionUtil /** * Return a normalized version string to be compared * - * @param string|array $versionIn + * @param array|string $versionIn + * + * @return string */ - public static function normalizeVersionForCompare($versionIn): string + public static function normalizeVersionForCompare(array|string $versionIn): string { if (!empty($versionIn)) { if (is_string($versionIn)) { @@ -105,9 +107,11 @@ final class VersionUtil } /** + * @param string $version + * * @return float|int */ - public static function versionToInteger(string $version) + public static function versionToInteger(string $version): float|int { $intVersion = 0; @@ -139,12 +143,4 @@ final class VersionUtil return $version; } - - /** - * Devolver versión normalizada en array - */ - public static function getVersionArrayNormalized(): array - { - return [implode('', InstallerService::VERSION), InstallerService::BUILD]; - } -} \ No newline at end of file +}