Просмотр исходного кода

PHPStan: finalise strictArrayFilter (#7794)

As well as reportPossiblyNonexistentConstantArrayOffset.
And disable PHPStan-next from GitHub Action, since the work is completed for now.
Alexandre Alapetite 7 месяцев назад
Родитель
Сommit
62f32ccadf

+ 2 - 2
.github/workflows/tests.yml

@@ -49,8 +49,8 @@ jobs:
     - name: PHPStan
       run: composer run-script phpstan
 
-    - name: PHPStan Next
-      run: composer run-script phpstan-next
+    # - name: PHPStan Next
+    #   run: composer run-script phpstan-next
 
     # NPM tests
 

+ 1 - 1
app/Controllers/configureController.php

@@ -386,7 +386,7 @@ class FreshRSS_configure_Controller extends FreshRSS_ActionController {
 		$this->view->tags = FreshRSS_Context::labels();
 
 		if (Minz_Request::isPost()) {
-			$params = array_filter(Minz_Request::paramArray('query'));
+			$params = Minz_Request::paramArray('query');
 			$queryParams = [];
 			$name = Minz_Request::paramString('name') ?: _t('conf.query.number', $id + 1);
 			if ('' === $name) {

+ 1 - 1
app/Controllers/feedController.php

@@ -221,7 +221,7 @@ class FreshRSS_feed_Controller extends FreshRSS_ActionController {
 				}
 			}
 
-			$headers = array_filter(array_map('trim', $headers));
+			$headers = array_filter($headers, fn(string $header): bool => trim($header) !== '');
 			if (!empty($headers)) {
 				$opts[CURLOPT_HTTPHEADER] = array_merge($headers, $opts[CURLOPT_HTTPHEADER] ?? []);
 				$opts[CURLOPT_HTTPHEADER] = array_unique($opts[CURLOPT_HTTPHEADER]);

+ 2 - 2
app/Controllers/subscriptionController.php

@@ -207,7 +207,7 @@ class FreshRSS_subscription_Controller extends FreshRSS_ActionController {
 				}
 			}
 
-			$headers = array_filter(array_map('trim', $headers));
+			$headers = array_filter($headers, fn(string $header): bool => trim($header) !== '');
 			if (!empty($headers)) {
 				$opts[CURLOPT_HTTPHEADER] = array_merge($headers, $opts[CURLOPT_HTTPHEADER] ?? []);
 				$opts[CURLOPT_HTTPHEADER] = array_unique($opts[CURLOPT_HTTPHEADER]);
@@ -318,7 +318,7 @@ class FreshRSS_subscription_Controller extends FreshRSS_ActionController {
 			}
 
 			$conditions = Minz_Request::paramTextToArray('path_entries_conditions', plaintext: true);
-			$conditions = array_filter(array_map('trim', $conditions));
+			$conditions = array_filter($conditions, fn(string $condition): bool => trim($condition) !== '');
 			$feed->_attribute('path_entries_conditions', empty($conditions) ? null : $conditions);
 			$feed->_attribute('path_entries_filter', Minz_Request::paramString('path_entries_filter', true));
 

+ 1 - 1
app/Models/Entry.php

@@ -860,7 +860,7 @@ HTML;
 		}
 
 		$conditions = $feed->attributeArray('path_entries_conditions') ?? [];
-		$conditions = array_filter(array_map(fn($v) => is_string($v) ? trim($v) : '', $conditions));
+		$conditions = array_filter($conditions, fn($v): bool => (is_string($v) ? trim($v) : '') !== '');
 		if (count($conditions) > 0) {
 			$found = false;
 			foreach ($conditions as $condition) {

+ 1 - 1
app/Models/UserQuery.php

@@ -124,7 +124,7 @@ class FreshRSS_UserQuery {
 			'shareOpml' => $this->shareOpml,
 			'description' => $this->description,
 			'imageUrl' => $this->imageUrl,
-		]);
+		], fn($v): bool => $v !== '' && $v !== 0 && $v !== false);
 	}
 
 	/**

+ 3 - 1
cli/CliOption.php

@@ -5,6 +5,7 @@ final class CliOption {
 	public const VALUE_NONE = 'none';
 	public const VALUE_REQUIRED = 'required';
 	public const VALUE_OPTIONAL = 'optional';
+	/** @var 'none'|'required'|'optional' $valueTaken */
 	private string $valueTaken = self::VALUE_REQUIRED;
 	/** @var array{type:string,isArray:bool} $types */
 	private array $types = ['type' => 'string', 'isArray' => false];
@@ -61,6 +62,7 @@ final class CliOption {
 		return $this;
 	}
 
+	/** @return 'none'|'required'|'optional' */
 	public function getValueTaken(): string {
 		return $this->valueTaken;
 	}
@@ -94,6 +96,6 @@ final class CliOption {
 			$this->deprecatedAlias,
 		];
 
-		return array_filter($aliases);
+		return array_filter($aliases, fn(?string $alias): bool => $alias !== null && trim($alias) !== '');
 	}
 }

+ 2 - 2
cli/CliOptionsParser.php

@@ -216,8 +216,8 @@ abstract class CliOptionsParser {
 		}
 
 		return [
-			'long' => array_filter($long),
-			'short' => $short
+			'long' => array_filter($long, fn(string $v): bool => trim($v) !== ''),
+			'short' => $short,
 		];
 	}
 

+ 1 - 1
cli/create-user.php

@@ -75,7 +75,7 @@ $values = [
 	'max_posts_per_rss' => $cliOptions->maxPostsPerRss ?? null,
 ];
 
-$values = array_filter($values);
+$values = array_filter($values, fn($v): bool => $v !== null && $v !== '');
 
 $ok = FreshRSS_user_Controller::createUser(
 	$username,

+ 2 - 1
cli/reconfigure.php

@@ -113,7 +113,8 @@ foreach ($values as $name => $value) {
 	}
 }
 
-$db = array_merge(FreshRSS_Context::systemConf()->db, array_filter($dbValues));
+$db = array_merge(FreshRSS_Context::systemConf()->db,
+	array_filter($dbValues, fn(?string $v): bool => $v !== null && trim($v) !== ''));
 
 performRequirementCheck($db['type']);
 

+ 1 - 1
cli/update-user.php

@@ -66,7 +66,7 @@ $values = [
 	'max_posts_per_rss' => $cliOptions->maxPostsPerRss ?? null,
 ];
 
-$values = array_filter($values);
+$values = array_filter($values, fn($value): bool => $value !== null && $value !== '');
 
 $ok = FreshRSS_user_Controller::updateUser(
 	$username,

+ 1 - 2
composer.json

@@ -77,8 +77,7 @@
             "@phtml-lint",
             "@phpunit",
             "@phpcs",
-            "@phpstan",
-            "@phpstan-next"
+            "@phpstan"
         ],
         "fix": [
             "@translations",

+ 1 - 1
lib/Minz/Migrator.php

@@ -33,7 +33,7 @@ class Minz_Migrator
 		if ($applied_migrations === false) {
 			return "Cannot open the {$applied_migrations_path} file";
 		}
-		$applied_migrations = array_filter(explode("\n", $applied_migrations));
+		$applied_migrations = array_filter(explode("\n", $applied_migrations), fn(string $migration): bool => trim($migration) !== '');
 
 		$migration_files = scandir($migrations_path) ?: [];
 		$migration_files = array_filter($migration_files, static function (string $filename) {

+ 2 - 2
lib/lib_rss.php

@@ -231,8 +231,8 @@ function format_bytes(int $bytes, int $precision = 2, string $system = 'IEC'): s
 		return format_number($bytes, $precision);
 	}
 	$bytes = max(intval($bytes), 0);
-	$pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
-	$pow = min($pow, count($units) - 1);
+	$pow = $bytes === 0 ? 0 : (int)floor(log($bytes) / log($base));
+	$pow = min(max(0, $pow), count($units) - 1);
 	$bytes /= pow($base, $pow);
 	return format_number($bytes, $precision) . ' ' . $units[$pow];
 }

+ 1 - 13
phpstan-next.neon

@@ -4,19 +4,7 @@ includes:
 
 parameters:
 	level: max
-	strictRules:
-		strictArrayFilter: true	# TODO pass
+	reportPossiblyNonexistentGeneralArrayOffset: false	# TODO: pass maybe
 	excludePaths:
 		analyse:
 			# TODO: Update files below and remove them from this list
-			- app/Controllers/configureController.php
-			- app/Controllers/feedController.php
-			- app/Controllers/subscriptionController.php
-			- app/Models/Entry.php
-			- app/Models/UserQuery.php
-			- cli/CliOption.php
-			- cli/CliOptionsParser.php
-			- cli/create-user.php
-			- cli/reconfigure.php
-			- cli/update-user.php
-			- lib/Minz/Migrator.php

+ 3 - 2
phpstan.dist.neon

@@ -41,15 +41,16 @@ parameters:
 	checkMissingOverrideMethodAttribute: true
 	checkTooWideReturnTypesInProtectedAndPublicMethods: true
 	reportAnyTypeWideningInVarTag: true
+	reportPossiblyNonexistentConstantArrayOffset: true
 	treatPhpDocTypesAsCertain: false
 	strictRules:
 		disallowedEmpty: false
 		disallowedLooseComparison: false
 		disallowedShortTernary: false
-		strictArrayFilter: false	# TODO pass
+		strictArrayFilter: true
 	exceptions:
 		check:
-			missingCheckedExceptionInThrows: false	# TODO pass
+			missingCheckedExceptionInThrows: false	# TODO pass maybe
 			tooWideThrowType: true
 		implicitThrows: false
 		checkedExceptionClasses: