From 5833dda61d64afdcacf5e6d73aaae2800cc8e496 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Wed, 27 Mar 2019 11:13:08 +0100 Subject: [PATCH] Replace strlen() calls with strict string comparisons Note there is an important difference between the two ways to express this: strlen() does a string cast, but the `=== ''` and `!== ''` comparisons will only detect empty strings, but not null, false, or any other falsy value that becomes an empty string when cast to be one. I am only touching code where I'm sure the variable is guaranteed to be a string. This change is done because I find the strict comparisons much more readable. The code does exactly one thing now, and no magic casts any more. Change-Id: I3e908a0c7c7b6c29b0e5a1414f2ba9062a215b93 --- includes/MovePage.php | 2 +- includes/SiteConfiguration.php | 2 +- includes/api/ApiQuerySiteinfo.php | 4 ++-- includes/content/WikiTextStructure.php | 2 +- includes/http/PhpHttpRequest.php | 2 +- includes/jobqueue/jobs/ThumbnailRenderJob.php | 2 +- includes/libs/rdbms/database/DatabaseDomain.php | 4 ++-- includes/specials/SpecialSearch.php | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/MovePage.php b/includes/MovePage.php index bcec0a188d..db5750a1be 100644 --- a/includes/MovePage.php +++ b/includes/MovePage.php @@ -106,7 +106,7 @@ class MovePage { $oldid = $this->oldTitle->getArticleID(); - if ( strlen( $this->newTitle->getDBkey() ) < 1 ) { + if ( $this->newTitle->getDBkey() === '' ) { $status->fatal( 'articleexists' ); } if ( diff --git a/includes/SiteConfiguration.php b/includes/SiteConfiguration.php index 7af80dcfd6..b400797c58 100644 --- a/includes/SiteConfiguration.php +++ b/includes/SiteConfiguration.php @@ -562,7 +562,7 @@ class SiteConfiguration { ->execute(); $data = trim( $result->getStdout() ); - if ( $result->getExitCode() != 0 || !strlen( $data ) ) { + if ( $result->getExitCode() || $data === '' ) { throw new MWException( "Failed to run getConfiguration.php: {$result->getStdout()}" ); } $res = unserialize( $data ); diff --git a/includes/api/ApiQuerySiteinfo.php b/includes/api/ApiQuerySiteinfo.php index 96fa8a1380..82a52b40be 100644 --- a/includes/api/ApiQuerySiteinfo.php +++ b/includes/api/ApiQuerySiteinfo.php @@ -664,8 +664,8 @@ class ApiQuerySiteinfo extends ApiQueryBase { } $data = [ - 'url' => strlen( $url ) ? $url : '', - 'text' => strlen( $text ) ? $text : '', + 'url' => (string)$url, + 'text' => (string)$text, ]; return $this->getResult()->addValue( 'query', $property, $data ); diff --git a/includes/content/WikiTextStructure.php b/includes/content/WikiTextStructure.php index 0e03e72ef6..2f3a6f61d1 100644 --- a/includes/content/WikiTextStructure.php +++ b/includes/content/WikiTextStructure.php @@ -154,7 +154,7 @@ class WikiTextStructure { 'enableSectionEditTokens' => false, 'allowTOC' => false, ] ); - if ( strlen( $text ) == 0 ) { + if ( $text === '' ) { $this->allText = ""; // empty text - nothing to seek here return; diff --git a/includes/http/PhpHttpRequest.php b/includes/http/PhpHttpRequest.php index 30ab181e1d..d2af8c8568 100644 --- a/includes/http/PhpHttpRequest.php +++ b/includes/http/PhpHttpRequest.php @@ -217,7 +217,7 @@ class PhpHttpRequest extends MWHttpRequest { break; } - if ( strlen( $buf ) ) { + if ( $buf !== '' ) { call_user_func( $this->callback, $fh, $buf ); } } diff --git a/includes/jobqueue/jobs/ThumbnailRenderJob.php b/includes/jobqueue/jobs/ThumbnailRenderJob.php index 63575ebeca..eb8b1a2780 100644 --- a/includes/jobqueue/jobs/ThumbnailRenderJob.php +++ b/includes/jobqueue/jobs/ThumbnailRenderJob.php @@ -93,7 +93,7 @@ class ThumbnailRenderJob extends Job { if ( $wgUploadThumbnailRenderHttpCustomDomain ) { $parsedUrl = wfParseUrl( $thumbUrl ); - if ( !$parsedUrl || !isset( $parsedUrl['path'] ) || !strlen( $parsedUrl['path'] ) ) { + if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) { $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" ); return false; } diff --git a/includes/libs/rdbms/database/DatabaseDomain.php b/includes/libs/rdbms/database/DatabaseDomain.php index 8d8285426a..ca57938f2e 100644 --- a/includes/libs/rdbms/database/DatabaseDomain.php +++ b/includes/libs/rdbms/database/DatabaseDomain.php @@ -42,11 +42,11 @@ class DatabaseDomain { * @param string $prefix Table prefix */ public function __construct( $database, $schema, $prefix ) { - if ( $database !== null && ( !is_string( $database ) || !strlen( $database ) ) ) { + if ( $database !== null && ( !is_string( $database ) || $database === '' ) ) { throw new InvalidArgumentException( 'Database must be null or a non-empty string.' ); } $this->database = $database; - if ( $schema !== null && ( !is_string( $schema ) || !strlen( $schema ) ) ) { + if ( $schema !== null && ( !is_string( $schema ) || $schema === '' ) ) { throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' ); } $this->schema = $schema; diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index e6d06329ad..171566b6c7 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -102,7 +102,7 @@ class SpecialSearch extends SpecialPage { /** * Entry point * - * @param string $par + * @param string|null $par */ public function execute( $par ) { $request = $this->getRequest(); @@ -115,7 +115,7 @@ class SpecialSearch extends SpecialPage { // parameter, but also as part of the primary url. This can have PII implications // in releasing page view data. As such issue a 301 redirect to the correct // URL. - if ( strlen( $par ) && !strlen( $term ) ) { + if ( $par !== null && $par !== '' && $term === '' ) { $query = $request->getValues(); unset( $query['title'] ); // Strip underscores from title parameter; most of the time we'll want -- 2.20.1