diff options
| author | 2024-12-27 12:12:49 +0100 | |
|---|---|---|
| committer | 2024-12-27 12:12:49 +0100 | |
| commit | b1d24fbdb7d1cc948c946295035dad6df550fb7e (patch) | |
| tree | 7b4365a04097a779659474fbb9281a9661512522 /cli | |
| parent | 897e4a3f4a273d50c28157edb67612b2d7fa2e6f (diff) | |
PHPStan 2.0 (#7131)
* PHPStan 2.0
fix https://github.com/FreshRSS/FreshRSS/issues/6989
https://github.com/phpstan/phpstan/releases/tag/2.0.0
https://github.com/phpstan/phpstan/blob/2.0.x/UPGRADING.md
* More
* More
* Done
* fix i18n CLI
* Restore a PHPStan Next test
For work towards PHPStan Level 10
* 4 more on Level 10
* fix getTagsForEntry
* API at Level 10
* More Level 10
* Finish Minz at Level 10
* Finish CLI at Level 10
* Finish Controllers at Level 10
* More Level 10
* More
* Pass bleedingEdge
* Clean PHPStan options and add TODOs
* Level 10 for main config
* More
* Consitency array vs. list
* Sanitize themes get_infos
* Simplify TagDAO->getTagsForEntries()
* Finish reportAnyTypeWideningInVarTag
* Prepare checkBenevolentUnionTypes and checkImplicitMixed
* Fixes
* Refix
* Another fix
* Casing of __METHOD__ constant
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/CliOptionsParser.php | 2 | ||||
| -rwxr-xr-x | cli/_cli.php | 3 | ||||
| -rwxr-xr-x | cli/check.translation.php | 5 | ||||
| -rwxr-xr-x | cli/db-restore.php | 2 | ||||
| -rwxr-xr-x | cli/do-install.php | 13 | ||||
| -rw-r--r-- | cli/i18n/I18nData.php | 12 | ||||
| -rw-r--r-- | cli/i18n/I18nFile.php | 27 |
7 files changed, 47 insertions, 17 deletions
diff --git a/cli/CliOptionsParser.php b/cli/CliOptionsParser.php index 5a2d353db..933575393 100644 --- a/cli/CliOptionsParser.php +++ b/cli/CliOptionsParser.php @@ -129,7 +129,7 @@ abstract class CliOptionsParser { /** * @param array<string> $userInputs - * @return array<string> + * @return list<string> */ private function getAliasesUsed(array $userInputs, string $regex): array { $foundAliases = []; diff --git a/cli/_cli.php b/cli/_cli.php index 9486405aa..b4df51dd8 100755 --- a/cli/_cli.php +++ b/cli/_cli.php @@ -52,7 +52,8 @@ function accessRights(): void { function done(bool $ok = true): never { if (!$ok) { - fwrite(STDERR, (empty($_SERVER['argv'][0]) ? 'Process' : basename($_SERVER['argv'][0])) . ' failed!' . "\n"); + fwrite(STDERR, (isset($_SERVER['argv']) && is_array($_SERVER['argv']) && !empty($_SERVER['argv'][0]) && is_string($_SERVER['argv'][0]) ? + basename($_SERVER['argv'][0]) : 'Process') . ' failed!' . "\n"); } exit($ok ? 0 : 1); } diff --git a/cli/check.translation.php b/cli/check.translation.php index b452054ed..d209383a8 100755 --- a/cli/check.translation.php +++ b/cli/check.translation.php @@ -82,7 +82,7 @@ if (!$isValidated) { * Iterates through all php and phtml files in the whole project and extracts all * translation keys used. * - * @return array<string> + * @return list<string> */ function findUsedTranslations(): array { $directory = new RecursiveDirectoryIterator(__DIR__ . '/..'); @@ -90,6 +90,9 @@ function findUsedTranslations(): array { $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH); $usedI18n = []; foreach (array_keys(iterator_to_array($regex)) as $file) { + if (!is_string($file) || $file === '') { + continue; + } $fileContent = file_get_contents($file); if ($fileContent === false) { continue; diff --git a/cli/db-restore.php b/cli/db-restore.php index 6ea6f4a7d..32f43708f 100755 --- a/cli/db-restore.php +++ b/cli/db-restore.php @@ -34,7 +34,7 @@ try { $_SESSION['bd_error'] = $ex->getMessage(); } if (!$ok) { - fail('FreshRSS database error: ' . (empty($_SESSION['bd_error']) ? 'Unknown error' : $_SESSION['bd_error'])); + fail('FreshRSS database error: ' . (is_string($_SESSION['bd_error'] ?? null) ? $_SESSION['bd_error'] : 'Unknown error')); } foreach (listUsers() as $username) { diff --git a/cli/do-install.php b/cli/do-install.php index 8591f2299..cab4b1462 100755 --- a/cli/do-install.php +++ b/cli/do-install.php @@ -100,7 +100,7 @@ $config = [ $customConfigPath = DATA_PATH . '/config.custom.php'; if (file_exists($customConfigPath)) { $customConfig = include($customConfigPath); - if (is_array($customConfig)) { + if (is_array($customConfig) && is_array_keys_string($customConfig)) { $config = array_merge($customConfig, $config); } } @@ -132,8 +132,14 @@ if ((!empty($config['base_url'])) && is_string($config['base_url']) && Minz_Requ $config['pubsubhubbub_enabled'] = true; } +if (!is_array($config['db'])) { + $config['db'] = []; +} $config['db'] = array_merge($config['db'], array_filter($dbValues, static fn($value) => $value !== null)); +if (!is_string($config['db']['type'] ?? null)) { + $config['db']['type'] = ''; +} performRequirementCheck($config['db']['type']); if (file_put_contents(join_path(DATA_PATH, 'config.php'), @@ -162,9 +168,12 @@ try { if (!$ok) { @unlink(join_path(DATA_PATH, 'config.php')); - fail('FreshRSS database error: ' . (empty($_SESSION['bd_error']) ? 'Unknown error' : $_SESSION['bd_error'])); + fail('FreshRSS database error: ' . (is_string($_SESSION['bd_error'] ?? null) ? $_SESSION['bd_error'] : 'Unknown error')); } +if (!is_string($config['default_user'] ?? null)) { + fail('FreshRSS default user not set!'); +} echo 'ℹ️ Remember to create the default user: ', $config['default_user'], "\t", './cli/create-user.php --user ', $config['default_user'], " --password 'password' --more-options\n"; diff --git a/cli/i18n/I18nData.php b/cli/i18n/I18nData.php index 6f841e947..7170765b7 100644 --- a/cli/i18n/I18nData.php +++ b/cli/i18n/I18nData.php @@ -3,6 +3,7 @@ declare(strict_types=1); class I18nData { + /** @var string */ public const REFERENCE_LANGUAGE = 'en'; /** @param array<string,array<string,array<string,I18nValue>>> $data */ @@ -74,22 +75,21 @@ class I18nData { /** * Return the available languages - * @return array<string> + * @return list<string> */ public function getAvailableLanguages(): array { $languages = array_keys($this->data); sort($languages); - return $languages; } /** * Return all available languages without the reference language - * @return array<string> + * @return list<string> */ private function getNonReferenceLanguages(): array { - return array_filter(array_keys($this->data), - static fn(string $value) => static::REFERENCE_LANGUAGE !== $value); + return array_values(array_filter(array_keys($this->data), + static fn(string $value) => static::REFERENCE_LANGUAGE !== $value)); } /** @@ -129,7 +129,7 @@ class I18nData { * Return the siblings for a specified key. * To get the siblings, we need to find all matches with the parent. * - * @return array<string> + * @return list<string> */ private function getSiblings(string $key): array { if (!array_key_exists($this->getFilenamePrefix($key), $this->data[static::REFERENCE_LANGUAGE])) { diff --git a/cli/i18n/I18nFile.php b/cli/i18n/I18nFile.php index 5d310d6bf..6771dfbff 100644 --- a/cli/i18n/I18nFile.php +++ b/cli/i18n/I18nFile.php @@ -4,6 +4,23 @@ declare(strict_types=1); require_once __DIR__ . '/I18nValue.php'; class I18nFile { + + /** + * @param array<mixed,mixed> $array + * @phpstan-assert-if-true array<string,string|array<string,mixed>> $array + */ + public static function is_array_recursive_string(array $array): bool { + foreach ($array as $key => $value) { + if (!is_string($key)) { + return false; + } + if (!is_string($value) && !(is_array($value) && self::is_array_recursive_string($value))) { + return false; + } + } + return true; + } + /** * @return array<string,array<string,array<string,I18nValue>>> */ @@ -45,7 +62,7 @@ class I18nFile { /** * Process the content of an i18n file - * @return array<string,array<string,I18nValue>> + * @return array<string,string|array<string,mixed>> */ private function process(string $filename): array { $fileContent = file_get_contents($filename) ?: []; @@ -71,7 +88,7 @@ class I18nFile { die(1); } - if (is_array($content)) { + if (is_array($content) && self::is_array_recursive_string($content)) { return $content; } @@ -81,7 +98,7 @@ class I18nFile { /** * Flatten an array of translation * - * @param array<string,I18nValue|array<string,I18nValue>> $translation + * @param array<string,I18nValue|string|array<string,I18nValue>|mixed> $translation * @return array<string,I18nValue> */ private function flatten(array $translation, string $prefix = ''): array { @@ -92,9 +109,9 @@ class I18nFile { } foreach ($translation as $key => $value) { - if (is_array($value)) { + if (is_array($value) && is_array_keys_string($value)) { $a += $this->flatten($value, $prefix . $key); - } else { + } elseif (is_string($value) || $value instanceof I18nValue) { $a[$prefix . $key] = new I18nValue($value); } } |
