Browse Source

fix many "Only booleans are allowed in an if condition" (#5501)

* fix many "Only booleans are allowed in an if condition"

* Update cli/create-user.php

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>

* Update cli/i18n/I18nUsageValidator.php

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>

* Fix several regressions and other minor things

* Fix another regression

* Update lib/http-conditional.php

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>

---------

Co-authored-by: Luc <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Luc SANCHEZ 2 years ago
parent
commit
7f9594b8c7

+ 2 - 2
cli/create-user.php

@@ -10,7 +10,7 @@ if (!FreshRSS_user_Controller::checkUsername($username)) {
 }
 
 $usernames = listUsers();
-if (preg_grep("/^$username$/i", $usernames)) {
+if (preg_grep("/^$username$/i", $usernames) !== false) {
 	fail('FreshRSS warning: username already exists “' . $username . '”', EXIT_CODE_ALREADY_EXISTS);
 }
 
@@ -31,7 +31,7 @@ if (!$ok) {
 if (!empty($options['api_password'])) {
 	$username = cliInitUser($username);
 	$error = FreshRSS_api_Controller::updatePassword($options['api_password']);
-	if ($error) {
+	if ($error !== false) {
 		fail($error);
 	}
 }

+ 1 - 1
cli/delete-user.php

@@ -19,7 +19,7 @@ if (!FreshRSS_user_Controller::checkUsername($username)) {
 }
 
 $usernames = listUsers();
-if (!preg_grep("/^$username$/i", $usernames)) {
+if (preg_grep("/^$username$/i", $usernames) === false) {
 	fail('FreshRSS error: username not found “' . $username . '”');
 }
 

+ 1 - 1
cli/i18n/I18nUsageValidator.php

@@ -42,7 +42,7 @@ class I18nUsageValidator implements I18nValidatorInterface {
 		foreach ($this->reference as $file => $data) {
 			foreach ($data as $key => $value) {
 				$this->totalEntries++;
-				if (preg_match('/\._$/', $key) && in_array(preg_replace('/\._$/', '', $key), $this->code, true)) {
+				if (preg_match('/\._$/', $key) === 1 && in_array(preg_replace('/\._$/', '', $key), $this->code, true)) {
 					continue;
 				}
 				if (!in_array($key, $this->code, true)) {

+ 2 - 2
lib/Minz/ExtensionException.php

@@ -1,8 +1,8 @@
 <?php
 
 class Minz_ExtensionException extends Minz_Exception {
-	public function __construct(string $message, ?string $extension_name = null, int $code = self::ERROR) {
-		if ($extension_name) {
+	public function __construct(string $message, string $extension_name = '', int $code = self::ERROR) {
+		if ($extension_name !== '') {
 			$message = 'An error occurred in `' . $extension_name . '` extension with the message: ' . $message;
 		} else {
 			$message = 'An error occurred in an unnamed extension with the message: ' . $message;

+ 1 - 1
lib/Minz/ExtensionManager.php

@@ -145,7 +145,7 @@ final class Minz_ExtensionManager {
 				continue;
 			}
 			$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 */
+			/** @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)) {
 				// metadata.json is not a json file? Invalid!

+ 3 - 3
lib/Minz/Log.php

@@ -81,12 +81,12 @@ class Minz_Log {
 		$maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576;
 		if ($maxSize > 0 && @filesize($file_name) > $maxSize) {
 			$fp = fopen($file_name, 'c+');
-			if ($fp && flock($fp, LOCK_EX)) {
-				fseek($fp, -intval($maxSize / 2), SEEK_END);
+			if (is_resource($fp) && flock($fp, LOCK_EX)) {
+				fseek($fp, -(int)($maxSize / 2), SEEK_END);
 				$content = fread($fp, $maxSize);
 				rewind($fp);
 				ftruncate($fp, 0);
-				fwrite($fp, $content ? $content : '');
+				fwrite($fp, $content ?: '');
 				fwrite($fp, sprintf("[%s] [notice] --- Log rotate.\n", date('r')));
 				fflush($fp);
 				flock($fp, LOCK_UN);

+ 1 - 3
lib/Minz/Migrator.php

@@ -70,9 +70,7 @@ class Minz_Migrator
 		}
 
 		$migrator = new self($migrations_path);
-		if ($applied_migrations) {
-			$migrator->setAppliedVersions($applied_migrations);
-		}
+		$migrator->setAppliedVersions($applied_migrations);
 		$results = $migrator->migrate();
 
 		foreach ($results as $migration => $result) {

+ 1 - 1
lib/Minz/ModelPdo.php

@@ -69,7 +69,7 @@ class Minz_ModelPdo {
 				$this->pdo->setPrefix($db['prefix'] . $this->current_user . '_');
 				break;
 			case 'sqlite':
-				$dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite');
+				$dsn = 'sqlite:' . DATA_PATH . '/users/' . $this->current_user . '/db.sqlite';
 				$this->pdo = new Minz_PdoSqlite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
 				$this->pdo->setPrefix('');
 				break;

+ 1 - 1
lib/Minz/Pdo.php

@@ -28,7 +28,7 @@ abstract class Minz_Pdo extends PDO {
 	}
 
 	protected function preSql(string $statement): string {
-		if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
+		if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement) === 1) {
 			invalidateHttpCache();
 		}
 		return $this->autoPrefix($statement);

+ 3 - 3
lib/Minz/Request.php

@@ -306,7 +306,7 @@ class Minz_Request {
 			return false;
 		}
 		$host = parse_url($address, PHP_URL_HOST);
-		if (!$host) {
+		if (!is_string($host)) {
 			return false;
 		}
 
@@ -358,7 +358,7 @@ class Minz_Request {
 		$notif = null;
 		Minz_Session::lock();
 		$requests = Minz_Session::param('requests');
-		if ($requests) {
+		if (is_array($requests)) {
 			//Delete abandoned notifications
 			$requests = array_filter($requests, static function (array $r) { return isset($r['time']) && $r['time'] > time() - 3600; });
 
@@ -454,7 +454,7 @@ class Minz_Request {
 	 * @return array<string>
 	 */
 	public static function getPreferredLanguages(): array {
-		if (preg_match_all('/(^|,)\s*(?P<lang>[^;,]+)/', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', $matches)) {
+		if (preg_match_all('/(^|,)\s*(?P<lang>[^;,]+)/', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', $matches) > 0) {
 			return $matches['lang'];
 		}
 		return array('en');

+ 4 - 3
lib/favicons.php

@@ -78,13 +78,14 @@ function searchFavicon(string &$url): string {
 					$href = trim($link->getAttribute('href'));
 					if (substr($href, 0, 2) === '//') {
 						// Case of protocol-relative URLs
-						if (preg_match('%^(https?:)//%i', $url, $matches)) {
+						if (preg_match('%^(https?:)//%i', $url, $matches) === 1) {
 							$href = $matches[1] . $href;
 						} else {
 							$href = 'https:' . $href;
 						}
 					}
-					if (!checkUrl($href, false)) {
+					$checkUrl = checkUrl($href, false);
+					if (is_string($checkUrl)) {
 						$href = SimplePie_IRI::absolutize($url, $href);
 					}
 					$favicon = downloadHttp($href, array(
@@ -119,6 +120,6 @@ function download_favicon(string $url, string $dest): bool {
 			}
 		}
 	}
-	return ($favicon != '' && file_put_contents($dest, $favicon)) ||
+	return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
 		@copy(DEFAULT_FAVICON, $dest);
 }

+ 1 - 1
lib/http-conditional.php

@@ -182,7 +182,7 @@ function httpConditional(int $UnixTimeStamp, int $cacheSeconds = 0, int $cachePr
  * Reference rfc2616-sec14.html#sec14.11
  */
 function _httpConditionalCallBack(string $buffer, int $mode = 5): string {
-	if (extension_loaded('zlib') && (!ini_get('zlib.output_compression'))) {
+	if (extension_loaded('zlib') && (ini_get('zlib.output_compression') == false)) {
 		$buffer2 = ob_gzhandler($buffer, $mode) ?: ''; //Will check HTTP_ACCEPT_ENCODING and put correct headers such as Vary //rfc2616-sec14.html#sec14.44
 		if (strlen($buffer2) > 1) //When ob_gzhandler succeeded
 			$buffer = $buffer2;

+ 6 - 5
lib/lib_rss.php

@@ -116,14 +116,14 @@ function checkUrl(string $url, bool $fixScheme = true) {
 	if ($url == '') {
 		return '';
 	}
-	if ($fixScheme && !preg_match('#^https?://#i', $url)) {
+	if ($fixScheme && preg_match('#^https?://#i', $url) !== 1) {
 		$url = 'https://' . ltrim($url, '/');
 	}
 
 	$url = idn_to_puny($url);	//PHP bug #53474 IDN
 	$urlRelaxed = str_replace('_', 'z', $url);	//PHP discussion #64948 Underscore
 
-	if (filter_var($urlRelaxed, FILTER_VALIDATE_URL)) {
+	if (is_string(filter_var($urlRelaxed, FILTER_VALIDATE_URL))) {
 		return $url;
 	} else {
 		return false;
@@ -244,6 +244,7 @@ function sensitive_log($log) {
 
 /**
  * @param array<string,mixed> $attributes
+ * @throws FreshRSS_Context_Exception
  */
 function customSimplePie(array $attributes = array()): SimplePie {
 	if (FreshRSS_Context::$system_conf === null) {
@@ -258,13 +259,13 @@ function customSimplePie(array $attributes = array()): SimplePie {
 	$simplePie->set_cache_duration($limits['cache_duration']);
 	$simplePie->enable_order_by_date(false);
 
-	$feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
+	$feed_timeout = empty($attributes['timeout']) ? 0 : (int)$attributes['timeout'];
 	$simplePie->set_timeout($feed_timeout > 0 ? $feed_timeout : $limits['timeout']);
 
 	$curl_options = FreshRSS_Context::$system_conf->curl_options;
 	if (isset($attributes['ssl_verify'])) {
 		$curl_options[CURLOPT_SSL_VERIFYHOST] = $attributes['ssl_verify'] ? 2 : 0;
-		$curl_options[CURLOPT_SSL_VERIFYPEER] = $attributes['ssl_verify'] ? true : false;
+		$curl_options[CURLOPT_SSL_VERIFYPEER] = (bool)$attributes['ssl_verify'];
 		if (!$attributes['ssl_verify']) {
 			$curl_options[CURLOPT_SSL_CIPHER_LIST] = 'DEFAULT@SECLEVEL=1';
 		}
@@ -468,7 +469,7 @@ function httpGet(string $url, string $cachePath, string $type = 'html', array $a
 
 	if (isset($attributes['ssl_verify'])) {
 		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
-		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $attributes['ssl_verify'] ? true : false);
+		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (bool)$attributes['ssl_verify']);
 		if (!$attributes['ssl_verify']) {
 			curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1');
 		}

+ 1 - 1
p/api/greader.php

@@ -1069,7 +1069,7 @@ final class GReaderAPI {
 								$include_target = $pathInfos[7];
 								if ($include_target != '' && !ctype_digit($include_target)) {
 									$include_target = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
-									if (preg_match('#/reader/api/0/stream/contents/feed/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches)) {
+									if (preg_match('#/reader/api/0/stream/contents/feed/([A-Za-z0-9\'!*()%$_.~+-]+)#', $include_target, $matches) === 1) {
 										$include_target = urldecode($matches[1]);
 									} else {
 										$include_target = '';

+ 1 - 1
p/i/index.php

@@ -62,7 +62,7 @@ if (!file_exists($applied_migrations_path)) {
 		$error = $e->getMessage();
 	}
 
-	if ($error) {
+	if ($error !== false) {
 		syslog(LOG_INFO, 'FreshRSS Fatal error! ' . $error);
 		FreshRSS::killApp($error);
 	}