Explicitly cast int to string when expected by PHP functions

Summary: Avoid `Parameter $foo of function bar expects string, int[|string] given` output in static code analysis by explicitly casting to string when a return value is an int, or by passing a string by encapsulating an int with apostrophes.

Test Plan: Run static code analysis.

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/D26074
This commit is contained in:
Andre Klapper 2025-06-09 22:51:55 +02:00
parent 6e43f89668
commit 6bcc10fbf1
14 changed files with 15 additions and 14 deletions

View file

@ -33,7 +33,7 @@ abstract class AphrontHTTPSink extends Phobject {
* @return void
*/
final public function writeHTTPStatus($code, $message = '') {
if (!preg_match('/^\d{3}$/', $code)) {
if (!preg_match('/^\d{3}$/', (string)$code)) {
throw new Exception(pht("Malformed HTTP status code '%s'!", $code));
}

View file

@ -392,7 +392,7 @@ abstract class PhabricatorAphlictManagementWorkflow
"%s\n",
pht('Starting Aphlict server in foreground...'));
} else {
Filesystem::writeFile($this->getPIDPath(), getmypid());
Filesystem::writeFile($this->getPIDPath(), (string)getmypid());
}
$command = $this->getStartCommand($this->getServerArgv());

View file

@ -403,7 +403,7 @@ final class PhabricatorTOTPAuthFactor extends PhabricatorAuthFactor {
((ord($hash[$offset + 3]) ) );
$code = ($code % 1000000);
$code = str_pad($code, 6, '0', STR_PAD_LEFT);
$code = str_pad((string)$code, 6, '0', STR_PAD_LEFT);
return $code;
}

View file

@ -43,7 +43,7 @@ final class PhabricatorWebServerSetupCheck extends PhabricatorSetupCheck {
->replaceQueryParam($expect_key, $expect_value);
$self_future = id(new HTTPSFuture($base_uri))
->addHeader('X-Setup-SelfCheck', 1)
->addHeader('X-Setup-SelfCheck', '1')
->addHeader('Accept-Encoding', 'gzip')
->setDisableContentDecoding(true)
->setHTTPBasicAuthCredentials(
@ -56,7 +56,7 @@ final class PhabricatorWebServerSetupCheck extends PhabricatorSetupCheck {
$gzip_compressed = gzencode($gzip_uncompressed);
$gzip_future = id(new HTTPSFuture($base_uri))
->addHeader('X-Setup-SelfCheck', 1)
->addHeader('X-Setup-SelfCheck', '1')
->addHeader('Content-Encoding', 'gzip')
->setMethod('POST')
->setTimeout(5)

View file

@ -87,7 +87,7 @@ final class DifferentialRevisionAffectedPathsController
}
// Sort rows by path name.
$rows = isort($rows, 3);
$rows = isort($rows, '3');
$table_view = id(new AphrontTableView($rows))
->setNoDataString(pht('This revision has no indexed affected paths.'))

View file

@ -321,7 +321,7 @@ final class PhabricatorFeedStoryPublisher extends Phobject {
// We're on a 32-bit machine.
if (function_exists('bcadd')) {
// Try to use the 'bc' extension.
return bcadd(bcmul($time, bcpow(2, 32)), $rand);
return bcadd(bcmul($time, bcpow('2', '32')), $rand);
} else {
// Do the math in MySQL. TODO: If we formalize a bc dependency, get
// rid of this.

View file

@ -45,7 +45,7 @@ final class PhabricatorFeedStoryData
// We're on a 32-bit machine.
if (function_exists('bcadd')) {
// Try to use the 'bc' extension.
return bcdiv($this->chronologicalKey, bcpow(2, 32));
return bcdiv($this->chronologicalKey, bcpow('2', '32'));
} else {
// Do the math in MySQL. TODO: If we formalize a bc dependency, get
// rid of this.

View file

@ -47,7 +47,7 @@ final class PhabricatorLocalDiskFileStorageEngine
// have one directory with a zillion files in it, since this is generally
// bad news.
do {
$name = md5(mt_rand());
$name = md5((string)mt_rand());
$name = preg_replace('/^(..)(..)(.*)$/', '\\1/\\2/\\3', $name);
if (!Filesystem::pathExists($root.'/'.$name)) {
break;

View file

@ -194,7 +194,7 @@ final class PhabricatorMetaMTAMailViewController
}
// Sort headers by name.
$headers = isort($headers, 0);
$headers = isort($headers, '0');
foreach ($headers as $header) {
list($key, $value) = $header;

View file

@ -259,7 +259,7 @@ final class PhrequentTimeBlock extends Phobject {
* @return list<pair<int, int>> Nonoverlapping time ranges.
*/
public static function mergeTimeRanges(array $ranges) {
$ranges = isort($ranges, 0);
$ranges = isort($ranges, '0');
$result = array();

View file

@ -35,6 +35,7 @@ final class PhabricatorSearchNgramEngine
$ngrams = array();
foreach ($unique_tokens as $token => $ignored) {
$token = (string)$token;
$token_v = phutil_utf8v($token);
$length = count($token_v);

View file

@ -120,7 +120,7 @@ final class PhabricatorEmailPreferencesSettingsPanel
}
// Sort them, then put "Common" at the top.
$tag_groups = isort($tag_groups, 0);
$tag_groups = isort($tag_groups, '0');
if ($common_tags) {
array_unshift($tag_groups, array(pht('Common'), $common_tags));
}

View file

@ -93,7 +93,7 @@ final class PhabricatorXHProfProfileSymbolView
$data[$key]['wt'] / $flat[$symbol]['wt'],
);
}
$child_rows = isort($child_rows, 2);
$child_rows = isort($child_rows, '2');
$child_rows = array_reverse($child_rows);
$rows = array_merge(
$rows,

View file

@ -122,7 +122,7 @@ final class PhabricatorEdgeQuery extends PhabricatorQuery {
* use case.
*
* @param string $src_phid Source PHID.
* @param string $edge_type Edge type constant.
* @param string $edge_type Edge type constant.
* @return list<string> List of destination PHIDs.
*/
public static function loadDestinationPHIDs($src_phid, $edge_type) {