Skip to content

Commit f6214f0

Browse files
committed
Addressing some PHP 8 incompatibilities
Summary: Starting with a new instance running PHP 8.2, address all exceptions that come up through some basic browsing/usage. For `strlen(null)` issues I generally tried to resolve if the value should be non-null at the point of issue, and attempt to address at earlier call-site. There were not many of these that I could determine. In the rest of those cases I would replace with a null-and-strlen check, or use `phutil_nonempty_string` if I was certain the value was a string and it was more convenient. Hitting all code-paths is challenging, so I would search for `strlen` within radius of files I was modifying and evaluate to address those uses in the same manner. Notes: - `AphrontRequest::getStr` only ever returns a string, and is safe to use `phutil_nonempty_string`. - `PhabricatorEnv::getEnvConfig` can return non-string things so any values coming from there should never use `phutil_nonempty_string`. - `AphrontRequest::getHTTPHeader` indicates it could return wild so `phutil_nonempty_string` should not be used. - `AphrontRequest::getURIData` isn't clear if it could return non-string data, so never use `phutil_nonempty_string`. Refs T13588 Test Plan: I'm running an instance on 8.2 and went through the basic setup/installation, startup and usage, including setup issues and configurations/settings. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: aklapper, Korvin, epriestley Maniphest Tasks: T13588 Differential Revision: https://secure.phabricator.com/D21862
1 parent bc6f478 commit f6214f0

File tree

71 files changed

+201
-145
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+201
-145
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
# NPM local packages
3838
/support/aphlict/server/node_modules/
39+
/support/aphlict/server/package.json
40+
/support/aphlict/server/package-lock.json
3941

4042
# Places for users to add custom resources.
4143
/resources/cows/custom/*

src/aphront/AphrontRequest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getURILineRange($key, $limit) {
6666
}
6767

6868
public static function parseURILineRange($range, $limit) {
69-
if (!strlen($range)) {
69+
if ($range === null || !strlen($range)) {
7070
return null;
7171
}
7272

@@ -448,11 +448,10 @@ public function setCookiePrefix($prefix) {
448448
}
449449

450450
private function getPrefixedCookieName($name) {
451-
if (strlen($this->cookiePrefix)) {
451+
if ($this->cookiePrefix !== null && strlen($this->cookiePrefix)) {
452452
return $this->cookiePrefix.'_'.$name;
453-
} else {
454-
return $name;
455453
}
454+
return $name;
456455
}
457456

458457
public function getCookie($name, $default = null) {
@@ -499,7 +498,7 @@ private function getCookieDomainURI() {
499498
// domain is. This makes setup easier, and we'll tell administrators to
500499
// configure a base domain during the setup process.
501500
$base_uri = PhabricatorEnv::getEnvConfig('phabricator.base-uri');
502-
if (!strlen($base_uri)) {
501+
if ($base_uri === null || !strlen($base_uri)) {
503502
return new PhutilURI('http://'.$host.'/');
504503
}
505504

@@ -956,7 +955,7 @@ public function updateEphemeralCookies() {
956955
$submit_cookie = PhabricatorCookies::COOKIE_SUBMIT;
957956

958957
$submit_key = $this->getCookie($submit_cookie);
959-
if (strlen($submit_key)) {
958+
if ($submit_key !== null && strlen($submit_key)) {
960959
$this->clearCookie($submit_cookie);
961960
$this->submitKey = $submit_key;
962961
}

src/aphront/response/AphrontFileResponse.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function addAllowOrigin($origin) {
1919
}
2020

2121
public function setDownload($download) {
22-
if (!strlen($download)) {
22+
if ($download === null || !strlen($download)) {
2323
$download = 'untitled';
2424
}
2525
$this->download = $download;
@@ -113,7 +113,8 @@ public function getHeaders() {
113113
$headers[] = array('Content-Length', $content_len);
114114
}
115115

116-
if (strlen($this->getDownload())) {
116+
$download = $this->getDownload();
117+
if ($download !== null && strlen($download)) {
117118
$headers[] = array('X-Download-Options', 'noopen');
118119

119120
$filename = $this->getDownload();
@@ -150,7 +151,7 @@ public function parseHTTPRange($range) {
150151
$begin = (int)$matches[1];
151152

152153
// The "Range" may be "200-299" or "200-", meaning "until end of file".
153-
if (strlen($matches[2])) {
154+
if ($matches[2] !== null && strlen($matches[2])) {
154155
$range_end = (int)$matches[2];
155156
$end = $range_end + 1;
156157
} else {

src/aphront/response/AphrontWebpageResponse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function getUnexpectedOutput() {
2121

2222
public function buildResponseString() {
2323
$unexpected_output = $this->getUnexpectedOutput();
24-
if (strlen($unexpected_output)) {
24+
if ($unexpected_output !== null && strlen($unexpected_output)) {
2525
$style = array(
2626
'background: linear-gradient(180deg, #eeddff, #ddbbff);',
2727
'white-space: pre-wrap;',

src/applications/auth/constants/PhabricatorCookies.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function setClientIDCookie(AphrontRequest $request) {
8989
// temporary and clearing it when users log out.
9090

9191
$value = $request->getCookie(self::COOKIE_CLIENTID);
92-
if (!strlen($value)) {
92+
if ($value === null || !strlen($value)) {
9393
$request->setTemporaryCookie(
9494
self::COOKIE_CLIENTID,
9595
Filesystem::readRandomCharacters(16));

src/applications/auth/controller/PhabricatorAuthController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ final protected function newCustomStartMessage() {
282282
$viewer,
283283
PhabricatorAuthLoginMessageType::MESSAGEKEY);
284284

285-
if (!strlen($text)) {
285+
if ($text === null || !strlen($text)) {
286286
return null;
287287
}
288288

src/applications/auth/controller/PhabricatorAuthRegisterController.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function handleRequest(AphrontRequest $request) {
1818
$invite = $this->loadInvite();
1919

2020
$is_setup = false;
21-
if (strlen($account_key)) {
21+
if ($account_key !== null && strlen($account_key)) {
2222
$result = $this->loadAccountForRegistrationOrLinking($account_key);
2323
list($account, $provider, $response) = $result;
2424
$is_default = false;
@@ -244,9 +244,9 @@ public function handleRequest(AphrontRequest $request) {
244244

245245
$require_real_name = PhabricatorEnv::getEnvConfig('user.require-real-name');
246246

247-
$e_username = strlen($value_username) ? null : true;
247+
$e_username = phutil_nonempty_string($value_username) ? null : true;
248248
$e_realname = $require_real_name ? true : null;
249-
$e_email = strlen($value_email) ? null : true;
249+
$e_email = phutil_nonempty_string($value_email) ? null : true;
250250
$e_password = true;
251251
$e_captcha = true;
252252

@@ -288,7 +288,7 @@ public function handleRequest(AphrontRequest $request) {
288288

289289
if ($can_edit_username) {
290290
$value_username = $request->getStr('username');
291-
if (!strlen($value_username)) {
291+
if (!phutil_nonempty_string($value_username)) {
292292
$e_username = pht('Required');
293293
$errors[] = pht('Username is required.');
294294
} else if (!PhabricatorUser::validateUsername($value_username)) {
@@ -323,7 +323,7 @@ public function handleRequest(AphrontRequest $request) {
323323

324324
if ($can_edit_email) {
325325
$value_email = $request->getStr('email');
326-
if (!strlen($value_email)) {
326+
if (!phutil_nonempty_string($value_email)) {
327327
$e_email = pht('Required');
328328
$errors[] = pht('Email is required.');
329329
} else if (!PhabricatorUserEmail::isValidAddress($value_email)) {
@@ -339,7 +339,7 @@ public function handleRequest(AphrontRequest $request) {
339339

340340
if ($can_edit_realname) {
341341
$value_realname = $request->getStr('realName');
342-
if (!strlen($value_realname) && $require_real_name) {
342+
if (!phutil_nonempty_string($value_realname) && $require_real_name) {
343343
$e_realname = pht('Required');
344344
$errors[] = pht('Real name is required.');
345345
} else {

src/applications/auth/controller/PhabricatorAuthStartController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handleRequest(AphrontRequest $request) {
3131
$session_token = $request->getCookie(PhabricatorCookies::COOKIE_SESSION);
3232
$did_clear = $request->getStr('cleared');
3333

34-
if (strlen($session_token)) {
34+
if ($session_token !== null && strlen($session_token)) {
3535
$kind = PhabricatorAuthSessionEngine::getSessionKindFromToken(
3636
$session_token);
3737
switch ($kind) {
@@ -98,7 +98,7 @@ public function handleRequest(AphrontRequest $request) {
9898
}
9999

100100
$next_uri = $request->getStr('next');
101-
if (!strlen($next_uri)) {
101+
if (phutil_nonempty_string($next_uri)) {
102102
if ($this->getDelegatingController()) {
103103
// Only set a next URI from the request path if this controller was
104104
// delegated to, which happens when a user tries to view a page which
@@ -112,7 +112,7 @@ public function handleRequest(AphrontRequest $request) {
112112
}
113113

114114
if (!$request->isFormPost()) {
115-
if (strlen($next_uri)) {
115+
if (phutil_nonempty_string($next_uri)) {
116116
PhabricatorCookies::setNextURICookie($request, $next_uri);
117117
}
118118
PhabricatorCookies::setClientIDCookie($request);
@@ -226,7 +226,7 @@ private function processAjaxRequest() {
226226

227227
$via_header = AphrontRequest::getViaHeaderName();
228228
$via_uri = AphrontRequest::getHTTPHeader($via_header);
229-
if (strlen($via_uri)) {
229+
if ($via_uri !== null && strlen($via_uri)) {
230230
PhabricatorCookies::setNextURICookie($request, $via_uri, $force = true);
231231
}
232232

src/applications/auth/password/PhabricatorAuthPasswordException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ final class PhabricatorAuthPasswordException
44
extends Exception {
55

66
private $passwordError;
7-
private $confirmErorr;
7+
private $confirmError;
88

99
public function __construct(
1010
$message,

src/applications/base/controller/PhabricatorController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function willBeginExecution() {
7474
$session_engine = new PhabricatorAuthSessionEngine();
7575

7676
$phsid = $request->getCookie(PhabricatorCookies::COOKIE_SESSION);
77-
if (strlen($phsid)) {
77+
if ($phsid !== null && strlen($phsid)) {
7878
$session_user = $session_engine->loadUserForSession(
7979
PhabricatorAuthSession::TYPE_WEB,
8080
$phsid);

src/applications/celerity/controller/CelerityResourceController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function serveResource(array $spec) {
113113

114114
$range = AphrontRequest::getHTTPHeader('Range');
115115

116-
if (strlen($range)) {
116+
if ($range !== null && strlen($range)) {
117117
$response->setContentLength(strlen($data));
118118

119119
list($range_begin, $range_end) = $response->parseHTTPRange($range);

src/applications/config/check/PhabricatorBaseURISetupCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected function executeChecks() {
1111

1212
$host_header = AphrontRequest::getHTTPHeader('Host');
1313
if (strpos($host_header, '.') === false) {
14-
if (!strlen(trim($host_header))) {
14+
if ($host_header === null || !strlen(trim($host_header))) {
1515
$name = pht('No "Host" Header');
1616
$summary = pht('No "Host" header present in request.');
1717
$message = pht(

src/applications/config/check/PhabricatorDaemonsSetupCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function executeChecks() {
5353
}
5454

5555
$expect_user = PhabricatorEnv::getEnvConfig('phd.user');
56-
if (strlen($expect_user)) {
56+
if ($expect_user !== null && strlen($expect_user)) {
5757

5858
try {
5959
$all_daemons = id(new PhabricatorDaemonLogQuery())

src/applications/config/check/PhabricatorPHPPreflightSetupCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function executeChecks() {
124124
}
125125

126126
$open_basedir = ini_get('open_basedir');
127-
if (strlen($open_basedir)) {
127+
if ($open_basedir !== null && strlen($open_basedir)) {
128128
// If `open_basedir` is set, just fatal. It's technically possible for
129129
// us to run with certain values of `open_basedir`, but: we can only
130130
// raise fatal errors from preflight steps, so we'd have to do this check

src/applications/config/check/PhabricatorSetupCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ final public static function getOpenSetupIssueKeysFromDatabase() {
122122
$db_cache = new PhabricatorKeyValueDatabaseCache();
123123
try {
124124
$value = $db_cache->getKey('phabricator.setup.issue-keys');
125-
if (!strlen($value)) {
125+
if ($value === null || !strlen($value)) {
126126
return null;
127127
}
128128
return phutil_json_decode($value);

src/applications/config/check/PhabricatorStorageSetupCheck.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,19 @@ private function checkS3() {
151151

152152
$how_many = 0;
153153

154-
if (strlen($access_key)) {
154+
if ($access_key !== null && strlen($access_key)) {
155155
$how_many++;
156156
}
157157

158-
if (strlen($secret_key)) {
158+
if ($secret_key !== null && strlen($secret_key)) {
159159
$how_many++;
160160
}
161161

162-
if (strlen($region)) {
162+
if ($region !== null && strlen($region)) {
163163
$how_many++;
164164
}
165165

166-
if (strlen($endpoint)) {
166+
if ($endpoint !== null && strlen($endpoint)) {
167167
$how_many++;
168168
}
169169

src/applications/config/check/PhabricatorWebServerSetupCheck.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function executeChecks() {
2323
}
2424

2525
$base_uri = PhabricatorEnv::getEnvConfig('phabricator.base-uri');
26-
if (!strlen($base_uri)) {
26+
if ($base_uri === null || !strlen($base_uri)) {
2727
// If `phabricator.base-uri` is not set then we can't really do
2828
// anything.
2929
return;

src/applications/config/controller/PhabricatorConfigConsoleController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public function newLibraryVersionTable() {
8585
$rows = array();
8686
foreach ($versions as $name => $info) {
8787
$branchpoint = $info['branchpoint'];
88-
if (strlen($branchpoint)) {
88+
if ($branchpoint !== null && strlen($branchpoint)) {
8989
$branchpoint = substr($branchpoint, 0, 12);
9090
} else {
9191
$branchpoint = null;
9292
}
9393

9494
$version = $info['hash'];
95-
if (strlen($version)) {
95+
if ($version !== null && strlen($version)) {
9696
$version = substr($version, 0, 12);
9797
} else {
9898
$version = pht('Unknown');

src/applications/config/controller/services/PhabricatorConfigDatabaseStatusController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ private function getURI(array $properties) {
837837

838838
$parts = array();
839839
foreach ($properties as $key => $property) {
840-
if (!strlen($property)) {
840+
if ($property === null || !strlen($property)) {
841841
continue;
842842
}
843843

src/applications/config/controller/settings/PhabricatorConfigSettingsListController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public function handleRequest(AphrontRequest $request) {
77
$viewer = $request->getViewer();
88

99
$filter = $request->getURIData('filter');
10-
if (!strlen($filter)) {
10+
if ($filter === null || !strlen($filter)) {
1111
$filter = 'settings';
1212
}
1313

src/applications/console/controller/DarkConsoleController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public function handleRequest(AphrontRequest $request) {
2626
}
2727

2828
$visible = $request->getStr('visible');
29-
if (strlen($visible)) {
29+
if (phutil_nonempty_string($visible)) {
3030
$this->writeDarkConsoleSetting(
3131
PhabricatorDarkConsoleVisibleSetting::SETTINGKEY,
3232
(int)$visible);
3333
return $response;
3434
}
3535

3636
$tab = $request->getStr('tab');
37-
if (strlen($tab)) {
37+
if (phutil_nonempty_string($tab)) {
3838
$this->writeDarkConsoleSetting(
3939
PhabricatorDarkConsoleTabSetting::SETTINGKEY,
4040
$tab);

src/applications/console/core/DarkConsoleCore.php

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public function render(AphrontRequest $request) {
114114
* need to convert it to UTF-8.
115115
*/
116116
private function sanitizeForJSON($data) {
117+
if ($data === null) {
118+
return '<null>';
119+
}
117120
if (is_object($data)) {
118121
return '<object:'.get_class($data).'>';
119122
} else if (is_array($data)) {

src/applications/diffusion/conduit/DiffusionHistoryQueryConduitAPIMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function getGitResult(ConduitAPIRequest $request) {
5555
$limit = $request->getValue('limit');
5656

5757
if (strlen($against_hash)) {
58-
$commit_range = "${against_hash}..${commit_hash}";
58+
$commit_range = "{$against_hash}..{$commit_hash}";
5959
} else {
6060
$commit_range = $commit_hash;
6161
}

src/applications/diffusion/controller/DiffusionController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,19 @@ protected function getRepositoryIdentifierFromRequest(
9797
AphrontRequest $request) {
9898

9999
$short_name = $request->getURIData('repositoryShortName');
100-
if (strlen($short_name)) {
100+
if ($short_name !== null && strlen($short_name)) {
101101
// If the short name ends in ".git", ignore it.
102102
$short_name = preg_replace('/\\.git\z/', '', $short_name);
103103
return $short_name;
104104
}
105105

106106
$identifier = $request->getURIData('repositoryCallsign');
107-
if (strlen($identifier)) {
107+
if ($identifier !== null && strlen($identifier)) {
108108
return $identifier;
109109
}
110110

111111
$id = $request->getURIData('repositoryID');
112-
if (strlen($id)) {
112+
if ($id !== null && strlen($id)) {
113113
return (int)$id;
114114
}
115115

src/applications/files/builtin/PhabricatorFilesComposeAvatarBuiltinFile.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ private function composeImage($color, $image, $border) {
183183
'image/png');
184184
}
185185

186-
private static function rgba2gd($rgba) {
187-
$r = $rgba[0];
188-
$g = $rgba[1];
189-
$b = $rgba[2];
190-
$a = $rgba[3];
186+
private static function rgba2gd(array $rgba) {
187+
$r = (int)$rgba[0];
188+
$g = (int)$rgba[1];
189+
$b = (int)$rgba[2];
190+
$a = (int)$rgba[3];
191191
$a = (1 - $a) * 255;
192192
return ($a << 24) | ($r << 16) | ($g << 8) | $b;
193193
}

0 commit comments

Comments
 (0)