aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2023-12-18 17:59:16 +0100
committerGravatar GitHub <noreply@github.com> 2023-12-18 17:59:16 +0100
commita80a5f48a16e7d232168a7aaa68e9a1804235ce1 (patch)
treea515b88592629dea7e83b96e26e2452d3f98a98e /lib/Minz
parent6bb45a87268157aab961a6a4a728d9a9bbe043b0 (diff)
Pass PHPStan level 8 (#5946)
* Pass PHPStan level 8 And prepare for PHPStan level 9 https://phpstan.org/user-guide/rule-levels * Revert wrong replace in comment * Fix PHPStan level 8 * Update PHPStan and other dev dependencies * Remove obsolete comment * noVariableVariables and towards bleedingEdge https://github.com/phpstan/phpstan-strict-rules https://phpstan.org/blog/what-is-bleeding-edge * More bleedingEdge * A bit more PHPStan level 9 * More PHPStan level 9 * Prepare for booleansInConditions Ignore int and null * Revert wrong line * More fixes * Fix keep_max_n_unread * Stricter attribute functions * Stricter callHooks and more PHPStan level 9 * More typing * A tiny more
Diffstat (limited to 'lib/Minz')
-rw-r--r--lib/Minz/Configuration.php4
-rw-r--r--lib/Minz/Extension.php100
-rw-r--r--lib/Minz/ExtensionManager.php14
-rw-r--r--lib/Minz/Helper.php6
-rw-r--r--lib/Minz/ModelPdo.php2
-rw-r--r--lib/Minz/Request.php10
-rw-r--r--lib/Minz/Session.php2
-rw-r--r--lib/Minz/Translate.php7
-rw-r--r--lib/Minz/Url.php10
-rw-r--r--lib/Minz/View.php2
10 files changed, 86 insertions, 71 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php
index 1aa4cae86..18fa6e7df 100644
--- a/lib/Minz/Configuration.php
+++ b/lib/Minz/Configuration.php
@@ -6,8 +6,8 @@ declare(strict_types=1);
* @property string $base_url
* @property array{'type':string,'host':string,'user':string,'password':string,'base':string,'prefix':string,
* 'connection_uri_params':string,'pdo_options':array<int,int|string|bool>} $db
- * @property-read string $disable_update
- * @property-read string $environment
+ * @property bool $disable_update
+ * @property string $environment
* @property array<string,bool> $extensions_enabled
* @property-read string $mailer
* @property-read array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,'secure':string,'port':int,'from':string} $smtp
diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php
index a8f883eb6..5386a64e7 100644
--- a/lib/Minz/Extension.php
+++ b/lib/Minz/Extension.php
@@ -19,8 +19,6 @@ abstract class Minz_Extension {
private $version;
/** @var 'system'|'user' */
private $type;
- /** @var string */
- private $config_key = 'extensions';
/** @var array<string,mixed>|null */
private $user_configuration;
/** @var array<string,mixed>|null */
@@ -175,8 +173,11 @@ abstract class Minz_Extension {
$mtime = @filemtime("{$this->path}/static/{$filename}");
} else {
$username = Minz_User::name();
- $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
- $file_name_url = urlencode("{$username}/{$this->config_key}/{$this->getName()}/{$filename}");
+ if ($username == null) {
+ return '';
+ }
+ $path = USERS_PATH . "/{$username}/extensions/{$this->getName()}/{$filename}";
+ $file_name_url = urlencode("{$username}/extensions/{$this->getName()}/{$filename}");
$mtime = @filemtime($path);
}
@@ -224,8 +225,8 @@ abstract class Minz_Extension {
}
switch ($type) {
- case 'system': return FreshRSS_Context::$system_conf !== null;
- case 'user': return FreshRSS_Context::$user_conf !== null;
+ case 'system': return FreshRSS_Context::hasSystemConf();
+ case 'user': return FreshRSS_Context::hasUserConf();
}
}
@@ -233,50 +234,40 @@ abstract class Minz_Extension {
private function isExtensionConfigured(string $type): bool {
switch ($type) {
case 'user':
- $conf = FreshRSS_Context::$user_conf;
+ $conf = FreshRSS_Context::userConf();
break;
case 'system':
- $conf = FreshRSS_Context::$system_conf;
+ $conf = FreshRSS_Context::systemConf();
break;
+ default:
+ return false;
}
- if ($conf === null || !$conf->hasParam($this->config_key)) {
+ if (!$conf->hasParam('extensions')) {
return false;
}
- $extensions = $conf->{$this->config_key};
- return array_key_exists($this->getName(), $extensions);
- }
-
- /**
- * @phpstan-param 'system'|'user' $type
- * @return array<string,mixed>
- */
- private function getConfiguration(string $type): array {
- if (!$this->isConfigurationEnabled($type)) {
- return [];
- }
-
- if (!$this->isExtensionConfigured($type)) {
- return [];
- }
-
- $conf = "{$type}_conf";
- return FreshRSS_Context::$$conf->{$this->config_key}[$this->getName()];
+ return array_key_exists($this->getName(), $conf->extensions);
}
/**
* @return array<string,mixed>
*/
public final function getSystemConfiguration(): array {
- return $this->getConfiguration('system');
+ if ($this->isConfigurationEnabled('system') && $this->isExtensionConfigured('system')) {
+ return FreshRSS_Context::systemConf()->extensions[$this->getName()];
+ }
+ return [];
}
/**
* @return array<string,mixed>
*/
public final function getUserConfiguration(): array {
- return $this->getConfiguration('user');
+ if ($this->isConfigurationEnabled('user') && $this->isExtensionConfigured('user')) {
+ return FreshRSS_Context::userConf()->extensions[$this->getName()];
+ }
+ return [];
}
/**
@@ -314,17 +305,26 @@ abstract class Minz_Extension {
* @param array<string,mixed> $configuration
*/
private function setConfiguration(string $type, array $configuration): void {
- $conf = "{$type}_conf";
+ switch ($type) {
+ case 'system':
+ $conf = FreshRSS_Context::systemConf();
+ break;
+ case 'user':
+ $conf = FreshRSS_Context::userConf();
+ break;
+ default:
+ return;
+ }
- if (FreshRSS_Context::$$conf->hasParam($this->config_key)) {
- $extensions = FreshRSS_Context::$$conf->{$this->config_key};
+ if ($conf->hasParam('extensions')) {
+ $extensions = $conf->extensions;
} else {
$extensions = [];
}
$extensions[$this->getName()] = $configuration;
- FreshRSS_Context::$$conf->{$this->config_key} = $extensions;
- FreshRSS_Context::$$conf->save();
+ $conf->extensions = $extensions;
+ $conf->save();
}
/** @param array<string,mixed> $configuration */
@@ -341,23 +341,28 @@ abstract class Minz_Extension {
/** @phpstan-param 'system'|'user' $type */
private function removeConfiguration(string $type): void {
- if (!$this->isConfigurationEnabled($type)) {
+ if (!$this->isConfigurationEnabled($type) || !$this->isExtensionConfigured($type)) {
return;
}
- if (!$this->isExtensionConfigured($type)) {
- return;
+ switch ($type) {
+ case 'system':
+ $conf = FreshRSS_Context::systemConf();
+ break;
+ case 'user':
+ $conf = FreshRSS_Context::userConf();
+ break;
+ default:
+ return;
}
- $conf = "{$type}_conf";
- $extensions = FreshRSS_Context::$$conf->{$this->config_key};
+ $extensions = $conf->extensions;
unset($extensions[$this->getName()]);
if (empty($extensions)) {
- $extensions = null;
+ $extensions = [];
}
-
- FreshRSS_Context::$$conf->{$this->config_key} = $extensions;
- FreshRSS_Context::$$conf->save();
+ $conf->extensions = $extensions;
+ $conf->save();
}
public final function removeSystemConfiguration(): void {
@@ -372,7 +377,7 @@ abstract class Minz_Extension {
public final function saveFile(string $filename, string $content): void {
$username = Minz_User::name();
- $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}";
+ $path = USERS_PATH . "/{$username}/extensions/{$this->getName()}";
if (!file_exists($path)) {
mkdir($path, 0777, true);
@@ -383,7 +388,10 @@ abstract class Minz_Extension {
public final function removeFile(string $filename): void {
$username = Minz_User::name();
- $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
+ if ($username == null) {
+ return;
+ }
+ $path = USERS_PATH . "/{$username}/extensions/{$this->getName()}/{$filename}";
if (file_exists($path)) {
unlink($path);
diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php
index e1443b9c3..9d8b94cbd 100644
--- a/lib/Minz/ExtensionManager.php
+++ b/lib/Minz/ExtensionManager.php
@@ -147,7 +147,7 @@ final class Minz_ExtensionManager {
$meta_raw_content = file_get_contents($metadata_filename) ?: '';
/** @var array{'name':string,'entrypoint':string,'path':string,'author'?:string,'description'?:string,'version'?:string,'type'?:'system'|'user'}|null $meta_json */
$meta_json = json_decode($meta_raw_content, true);
- if (!$meta_json || !self::isValidMetadata($meta_json)) {
+ if (!is_array($meta_json) || !self::isValidMetadata($meta_json)) {
// metadata.json is not a json file? Invalid!
// or metadata.json is invalid (no required information), invalid!
Minz_Log::warning('`' . $metadata_filename . '` is not a valid metadata file');
@@ -347,9 +347,9 @@ final class Minz_ExtensionManager {
call_user_func($function, ...$args);
}
} elseif ($signature === 'NoneToString') {
- return self::callNoneToString($hook_name);
+ return self::callHookString($hook_name);
} elseif ($signature === 'NoneToNone') {
- self::callNoneToNone($hook_name);
+ self::callHookVoid($hook_name);
}
return;
}
@@ -391,9 +391,9 @@ final class Minz_ExtensionManager {
* @param string $hook_name is the hook to call.
* @return string concatenated result of the call to all the hooks.
*/
- private static function callNoneToString(string $hook_name): string {
+ public static function callHookString(string $hook_name): string {
$result = '';
- foreach (self::$hook_list[$hook_name]['list'] as $function) {
+ foreach (self::$hook_list[$hook_name]['list'] ?? [] as $function) {
$result = $result . call_user_func($function);
}
return $result;
@@ -407,8 +407,8 @@ final class Minz_ExtensionManager {
*
* @param string $hook_name is the hook to call.
*/
- private static function callNoneToNone(string $hook_name): void {
- foreach (self::$hook_list[$hook_name]['list'] as $function) {
+ public static function callHookVoid(string $hook_name): void {
+ foreach (self::$hook_list[$hook_name]['list'] ?? [] as $function) {
call_user_func($function);
}
}
diff --git a/lib/Minz/Helper.php b/lib/Minz/Helper.php
index 642b9304c..04539ec40 100644
--- a/lib/Minz/Helper.php
+++ b/lib/Minz/Helper.php
@@ -19,12 +19,12 @@ class Minz_Helper {
* @phpstan-param T $var
* @phpstan-return T
*
- * @param string|array<string> $var
- * @return string|array<string>
+ * @param string|array<mixed> $var
+ * @return string|array<mixed>
*/
public static function htmlspecialchars_utf8($var) {
if (is_array($var)) {
- return array_map(array('Minz_Helper', 'htmlspecialchars_utf8'), $var);
+ return array_map(['Minz_Helper', 'htmlspecialchars_utf8'], $var);
} elseif (is_string($var)) {
return htmlspecialchars($var, ENT_COMPAT, 'UTF-8');
} else {
diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php
index 69b2357d2..b107d3df9 100644
--- a/lib/Minz/ModelPdo.php
+++ b/lib/Minz/ModelPdo.php
@@ -129,7 +129,7 @@ class Minz_ModelPdo {
$db = Minz_Configuration::get('system')->db;
throw new Minz_PDOConnectionException(
- $ex->getMessage(),
+ $ex === null ? '' : $ex->getMessage(),
$db['user'], Minz_Exception::ERROR
);
}
diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php
index 0ac1a9fe3..f39982be1 100644
--- a/lib/Minz/Request.php
+++ b/lib/Minz/Request.php
@@ -58,7 +58,7 @@ class Minz_Request {
}
}
- /** @return array<string|int,string|array<string,string>> */
+ /** @return array<string|int,string|array<string,string|int>> */
public static function paramArray(string $key, bool $specialchars = false): array {
if (empty(self::$params[$key]) || !is_array(self::$params[$key])) {
return [];
@@ -89,8 +89,8 @@ class Minz_Request {
}
public static function paramInt(string $key): int {
- if (!empty(self::$params[$key])) {
- return intval(self::$params[$key]);
+ if (!empty(self::$params[$key]) && is_numeric(self::$params[$key])) {
+ return (int)self::$params[$key];
}
return 0;
}
@@ -119,7 +119,7 @@ class Minz_Request {
* @return array<string>
*/
public static function paramTextToArray(string $key, array $default = []): array {
- if (isset(self::$params[$key])) {
+ if (isset(self::$params[$key]) && is_string(self::$params[$key])) {
return preg_split('/\R/', self::$params[$key]) ?: [];
}
return $default;
@@ -431,7 +431,7 @@ class Minz_Request {
if ($ORIGINAL_INPUT == false) {
return;
}
- if (null === $json = json_decode($ORIGINAL_INPUT, true)) {
+ if (!is_array($json = json_decode($ORIGINAL_INPUT, true))) {
return;
}
diff --git a/lib/Minz/Session.php b/lib/Minz/Session.php
index 4553d3083..99b7fef45 100644
--- a/lib/Minz/Session.php
+++ b/lib/Minz/Session.php
@@ -159,7 +159,7 @@ class Minz_Session {
* @param bool $force if false, does not clear the language parameter
*/
public static function unset_session(bool $force = false): void {
- $language = self::param('language');
+ $language = self::paramString('language');
if (!self::$volatile) {
session_destroy();
diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php
index 8f2e2527a..183fa48ca 100644
--- a/lib/Minz/Translate.php
+++ b/lib/Minz/Translate.php
@@ -186,7 +186,7 @@ class Minz_Translate {
/**
* Translate a key into its corresponding value based on selected language.
* @param string $key the key to translate.
- * @param mixed ...$args additional parameters for variable keys.
+ * @param bool|float|int|string ...$args additional parameters for variable keys.
* @return string value corresponding to the key.
* If no value is found, return the key itself.
*/
@@ -211,6 +211,9 @@ class Minz_Translate {
// Go through the i18n keys to get the correct translation value.
$translates = self::$translates[$top_level];
+ if (!is_array($translates)) {
+ $translates = [];
+ }
$size_group = count($group);
$level_processed = 0;
$translation_value = $key;
@@ -253,7 +256,7 @@ class Minz_Translate {
/**
* Alias for Minz_Translate::t()
* @param string $key
- * @param mixed ...$args
+ * @param bool|float|int|string ...$args
*/
function _t(string $key, ...$args): string {
return Minz_Translate::t($key, ...$args);
diff --git a/lib/Minz/Url.php b/lib/Minz/Url.php
index 0c17e3ec7..2104759cd 100644
--- a/lib/Minz/Url.php
+++ b/lib/Minz/Url.php
@@ -72,7 +72,7 @@ class Minz_Url {
$and = '&';
}
- if (!empty($url['params']['#'])) {
+ if (!empty($url['params']) && is_array($url['params']) && !empty($url['params']['#'])) {
$anchor = '#' . ($encodage === 'html' ? htmlspecialchars($url['params']['#'], ENT_QUOTES, 'UTF-8') : $url['params']['#']);
unset($url['params']['#']);
}
@@ -89,7 +89,7 @@ class Minz_Url {
$separator = $and;
}
- if (isset($url['params'])) {
+ if (isset($url['params']) && is_array($url['params'])) {
unset($url['params']['c']);
unset($url['params']['a']);
foreach ($url['params'] as $key => $param) {
@@ -101,7 +101,7 @@ class Minz_Url {
}
}
- if (!empty($url['#'])) {
+ if (!empty($url['#']) && is_string($url['#'])) {
$uri .= '#' . ($encodage === 'html' ? htmlspecialchars($url['#'], ENT_QUOTES, 'UTF-8') : $url['#']);
}
@@ -141,7 +141,9 @@ class Minz_Url {
*/
public static function unserialize(string $url = ''): array {
try {
- return json_decode(base64_decode($url, true) ?: '', true, JSON_THROW_ON_ERROR) ?? [];
+ $result = json_decode(base64_decode($url, true) ?: '', true, JSON_THROW_ON_ERROR) ?? [];
+ /** @var array{'c'?:string,'a'?:string,'params'?:array<string,mixed>} $result */
+ return $result;
} catch (\Throwable $exception) {
return [];
}
diff --git a/lib/Minz/View.php b/lib/Minz/View.php
index f67cf6277..c215ecdab 100644
--- a/lib/Minz/View.php
+++ b/lib/Minz/View.php
@@ -345,6 +345,8 @@ class Minz_View {
public function attributeParams(): void {
foreach (Minz_View::$params as $key => $value) {
+ // TODO: Do not use variable variable (noVariableVariables)
+ /** @phpstan-ignore-next-line */
$this->$key = $value;
}
}