From 9c684c80d63d08194c6d1d175a64e8f91ec07f84 Mon Sep 17 00:00:00 2001 From: Andre Klapper Date: Wed, 14 Aug 2024 21:52:57 +0200 Subject: [PATCH] Fix various potential PHP 8.1 "strlen(null)" exceptions Summary: `strlen()` was used in Phabricator to check if a generic value is a non-empty string. This behavior is deprecated since PHP 8.1. Phorge adopts `phutil_nonempty_string()` as a replacement. Note: this may highlight other absurd input values that might be worth correcting instead of just ignoring. If phutil_nonempty_string() throws an exception in your instance, report it to Phorge to evaluate and fix that specific corner case. All lines changed in this patch had `Parameter #1 $string of function strlen expects string, string|null given` reported by PHPStan. Thus these should be safe to replace with `phutil_nonempty_string` as no calls care about the actual `strlen()` return value (length of a string). Test Plan: Run static code analysis via `phpstan analyse -l 9` Reviewers: O1 Blessed Committers, valerio.bozzolan Reviewed By: O1 Blessed Committers, valerio.bozzolan Subscribers: tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Differential Revision: https://we.phorge.it/D25778 --- .../auth/adapter/PhutilBitbucketAuthAdapter.php | 2 +- src/applications/auth/adapter/PhutilGitHubAuthAdapter.php | 2 +- src/applications/auth/adapter/PhutilTwitterAuthAdapter.php | 2 +- .../query/lowlevel/DiffusionLowLevelCommitQuery.php | 6 +++--- .../markup/rule/PhabricatorKeyboardRemarkupRule.php | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/applications/auth/adapter/PhutilBitbucketAuthAdapter.php b/src/applications/auth/adapter/PhutilBitbucketAuthAdapter.php index 1384548d5e..baf9d5407e 100644 --- a/src/applications/auth/adapter/PhutilBitbucketAuthAdapter.php +++ b/src/applications/auth/adapter/PhutilBitbucketAuthAdapter.php @@ -14,7 +14,7 @@ final class PhutilBitbucketAuthAdapter extends PhutilOAuth1AuthAdapter { public function getAccountURI() { $name = $this->getAccountID(); - if (strlen($name)) { + if (phutil_nonempty_string($name)) { return 'https://bitbucket.org/'.$name; } return null; diff --git a/src/applications/auth/adapter/PhutilGitHubAuthAdapter.php b/src/applications/auth/adapter/PhutilGitHubAuthAdapter.php index a9d4c849d5..5d85a2ac21 100644 --- a/src/applications/auth/adapter/PhutilGitHubAuthAdapter.php +++ b/src/applications/auth/adapter/PhutilGitHubAuthAdapter.php @@ -31,7 +31,7 @@ final class PhutilGitHubAuthAdapter extends PhutilOAuthAuthAdapter { public function getAccountURI() { $name = $this->getAccountName(); - if (strlen($name)) { + if (phutil_nonempty_string($name)) { return 'https://github.com/'.$name; } return null; diff --git a/src/applications/auth/adapter/PhutilTwitterAuthAdapter.php b/src/applications/auth/adapter/PhutilTwitterAuthAdapter.php index 6f738c75f6..02a1f59a67 100644 --- a/src/applications/auth/adapter/PhutilTwitterAuthAdapter.php +++ b/src/applications/auth/adapter/PhutilTwitterAuthAdapter.php @@ -17,7 +17,7 @@ final class PhutilTwitterAuthAdapter extends PhutilOAuth1AuthAdapter { public function getAccountURI() { $name = $this->getAccountName(); - if (strlen($name)) { + if (phutil_nonempty_string($name)) { return 'https://twitter.com/'.$name; } return null; diff --git a/src/applications/diffusion/query/lowlevel/DiffusionLowLevelCommitQuery.php b/src/applications/diffusion/query/lowlevel/DiffusionLowLevelCommitQuery.php index 8b9233f128..0b5df04a41 100644 --- a/src/applications/diffusion/query/lowlevel/DiffusionLowLevelCommitQuery.php +++ b/src/applications/diffusion/query/lowlevel/DiffusionLowLevelCommitQuery.php @@ -126,11 +126,11 @@ final class DiffusionLowLevelCommitQuery $head = $parts[6]; $tail = $parts[7]; - if (strlen($head) && strlen($tail)) { + if (phutil_nonempty_string($head) && phutil_nonempty_string($tail)) { $body = $head."\n\n".$tail; - } else if (strlen($head)) { + } else if (phutil_nonempty_string($head)) { $body = $head; - } else if (strlen($tail)) { + } else if (phutil_nonempty_string($tail)) { $body = $tail; } else { $body = ''; diff --git a/src/infrastructure/markup/rule/PhabricatorKeyboardRemarkupRule.php b/src/infrastructure/markup/rule/PhabricatorKeyboardRemarkupRule.php index c5344a692c..a28735cfe9 100644 --- a/src/infrastructure/markup/rule/PhabricatorKeyboardRemarkupRule.php +++ b/src/infrastructure/markup/rule/PhabricatorKeyboardRemarkupRule.php @@ -22,7 +22,7 @@ final class PhabricatorKeyboardRemarkupRule extends PhutilRemarkupRule { foreach ($keys as $k => $v) { $v = trim($v, " \n"); $v = preg_replace('/\\\\(.)/', '\\1', $v); - if (!strlen($v)) { + if ($v === '') { unset($keys[$k]); continue; }