Browse Source

PHPStan 6 for CLI (#5258)

* PHPStan 6 for CLI
Except `./cli/i18n/`

* Bool

* One type forgotten
Alexandre Alapetite 3 năm trước cách đây
mục cha
commit
3f1695db03

+ 17 - 7
cli/_cli.php

@@ -18,12 +18,13 @@ Minz_Translate::init('en');
 
 FreshRSS_Context::$isCli = true;
 
-function fail($message, $exitCode = 1) {
+/** @return never */
+function fail(string $message, int $exitCode = 1) {
 	fwrite(STDERR, $message . "\n");
 	die($exitCode);
 }
 
-function cliInitUser($username) {
+function cliInitUser(string $username): string {
 	if (!FreshRSS_user_Controller::checkUsername($username)) {
 		fail('FreshRSS error: invalid username: ' . $username . "\n");
 	}
@@ -42,19 +43,20 @@ function cliInitUser($username) {
 	return $username;
 }
 
-function accessRights() {
+function accessRights(): void {
 	echo 'ℹ️ Remember to re-apply the appropriate access rights, such as:',
 		"\t", 'sudo cli/access-permissions.sh', "\n";
 }
 
-function done($ok = true) {
+/** @return never */
+function done(bool $ok = true) {
 	if (!$ok) {
 		fwrite(STDERR, (empty($_SERVER['argv'][0]) ? 'Process' : basename($_SERVER['argv'][0])) . ' failed!' . "\n");
 	}
 	exit($ok ? 0 : 1);
 }
 
-function performRequirementCheck($databaseType) {
+function performRequirementCheck(string $databaseType): void {
 	$requirements = checkRequirements($databaseType);
 	if ($requirements['all'] !== 'ok') {
 		$message = 'FreshRSS failed requirements:' . "\n";
@@ -70,7 +72,11 @@ function performRequirementCheck($databaseType) {
 	}
 }
 
-function getLongOptions($options, $regex) {
+/**
+ * @param array<string> $options
+ * @return array<string>
+ */
+function getLongOptions(array $options, string $regex): array {
 	$longOptions = array_filter($options, function($a) use ($regex) {
 		return preg_match($regex, $a);
 	});
@@ -79,7 +85,11 @@ function getLongOptions($options, $regex) {
 	}, $longOptions);
 }
 
-function validateOptions($input, $params) {
+/**
+ * @param array<string> $input
+ * @param array<string> $params
+ */
+function validateOptions(array $input, array $params): bool {
 	$sanitizeInput = getLongOptions($input, REGEX_INPUT_OPTIONS);
 	$sanitizeParams = getLongOptions($params, REGEX_PARAM_OPTIONS);
 	$unknownOptions = array_diff($sanitizeInput, $sanitizeParams);

+ 2 - 2
cli/_update-or-create-user.php

@@ -34,12 +34,12 @@ if (!validateOptions($argv, $params) || empty($options['user'])) {
 		" --since_hours_posts_per_rss 168 --max_posts_per_rss 400 )");
 }
 
-function strParam($name) {
+function strParam(string $name): ?string {
 	global $options;
 	return isset($options[$name]) ? strval($options[$name]) : null;
 }
 
-function intParam($name) {
+function intParam(string $name): ?int {
 	global $options;
 	return isset($options[$name]) && ctype_digit($options[$name]) ? intval($options[$name]) : null;
 }

+ 5 - 4
cli/check.translation.php

@@ -12,7 +12,7 @@ $i18nData = new I18nData($i18nFile->load());
 $options = getopt("dhl:r");
 
 if (array_key_exists('h', $options)) {
-	help();
+	checkHelp();
 }
 if (array_key_exists('l', $options)) {
 	$languages = array($options['l']);
@@ -63,9 +63,9 @@ if (!$isValidated) {
  * Iterates through all php and phtml files in the whole project and extracts all
  * translation keys used.
  *
- * @return array
+ * @return array<string>
  */
-function findUsedTranslations() {
+function findUsedTranslations(): array {
 	$directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
 	$iterator = new RecursiveIteratorIterator($directory);
 	$regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
@@ -80,8 +80,9 @@ function findUsedTranslations() {
 
 /**
  * Output help message.
+ * @return never
  */
-function help() {
+function checkHelp() {
 	$file = str_replace(__DIR__ . '/', '', __FILE__);
 
 	echo <<<HELP

+ 5 - 5
cli/manipulate.translation.php

@@ -9,7 +9,7 @@ require_once __DIR__ . '/../constants.php';
 $options = getopt("a:hk:l:o:rv:");
 
 if (array_key_exists('h', $options)) {
-	help();
+	manipulateHelp();
 }
 
 if (!array_key_exists('a', $options)) {
@@ -76,7 +76,7 @@ switch ($options['a']) {
 		}
 		break;
 	default :
-		help();
+		manipulateHelp();
 		exit;
 }
 
@@ -85,19 +85,19 @@ $data->dump($i18nData->getData());
 /**
  * Output error message.
  */
-function error($message) {
+function error(string $message): void {
 	$error = <<<ERROR
 WARNING
 	%s\n\n
 ERROR;
 	echo sprintf($error, $message);
-	help();
+	manipulateHelp();
 }
 
 /**
  * Output help message.
  */
-function help() {
+function manipulateHelp(): void {
 	$file = str_replace(__DIR__ . '/', '', __FILE__);
 	echo <<<HELP
 NAME

+ 1 - 1
cli/prepare.php

@@ -39,4 +39,4 @@ file_put_contents(DATA_PATH . '/.htaccess',
 
 accessRights();
 
-done($ok);
+done((bool)$ok);

+ 0 - 4
tests/phpstan-next.txt

@@ -24,13 +24,9 @@
 ./app/Models/View.php
 ./app/Services/ExportService.php
 ./app/Services/ImportService.php
-./cli/_cli.php
-./cli/_update-or-create-user.php
-./cli/check.translation.php
 ./cli/i18n/I18nData.php
 ./cli/i18n/I18nFile.php
 ./cli/i18n/I18nValue.php
-./cli/manipulate.translation.php
 ./lib/http-conditional.php
 ./lib/Minz/ActionController.php
 ./lib/Minz/Configuration.php