Skip to content

Commit f18e71e

Browse files
Merge pull request #19228 from MauricioFauth/preg_match-boolean-return
Fix boolean usage of the return value of preg_match()
2 parents 12e1bf6 + dc1bd60 commit f18e71e

55 files changed

Lines changed: 129 additions & 512 deletions

Some content is hidden

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

‎phpstan-baseline.neon‎

Lines changed: 0 additions & 385 deletions
Large diffs are not rendered by default.

‎psalm-baseline.xml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
<code><![CDATA[static function (int $level, string $message, string $file, int $line) use (&$prevErrorHandler) {
155155
if ($level === E_USER_DEPRECATED) {
156156
$templateLine = 0;
157-
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
157+
if (preg_match('/ at line (\d+)[ .]/', $message, $matches) === 1) {
158158
$templateLine = (int) $matches[1];
159159
}
160160

@@ -7548,7 +7548,6 @@
75487548
<code><![CDATA[$subgroupHeader->getName()]]></code>
75497549
</PossiblyNullOperand>
75507550
<RiskyTruthyFalsyComparison>
7551-
<code><![CDATA[! preg_match_all('/(str[A-Z][A-Za-z0-9]*)/', (string) $config->settings[$section][$opt], $matches)]]></code>
75527551
<code><![CDATA[empty(Config::getInstance()->settings[$section][$opt])]]></code>
75537552
</RiskyTruthyFalsyComparison>
75547553
<TypeDoesNotContainType>

‎src/Command/SetVersionCommand.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7272
private function getGeneratedClass(string $version): string
7373
{
7474
// Do not allow any major below 5
75-
$return = preg_match('/^([5-9]+)\.(\d{1,2})\.(\d{1,2})(-([a-z0-9]+))?$/', $version, $matches);
76-
if ($return === false || $return === 0) {
75+
if (preg_match('/^([5-9]+)\.(\d{1,2})\.(\d{1,2})(-([a-z0-9]+))?$/', $version, $matches) !== 1) {
7776
throw new RangeException('The version number is in the wrong format: ' . $version);
7877
}
7978

‎src/Command/TwigLintCommand.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114114
static function (int $level, string $message, string $file, int $line) use (&$prevErrorHandler) {
115115
if ($level === E_USER_DEPRECATED) {
116116
$templateLine = 0;
117-
if (preg_match('/ at line (\d+)[ .]/', $message, $matches)) {
117+
if (preg_match('/ at line (\d+)[ .]/', $message, $matches) === 1) {
118118
$templateLine = (int) $matches[1];
119119
}
120120

‎src/Config.php‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,45 +218,45 @@ public function checkClient(): void
218218
// 2. browser and version
219219
// (must check everything else before Mozilla)
220220

221-
$isMozilla = preg_match('@Mozilla/([0-9]\.[0-9]{1,2})@', $httpUserAgent, $mozillaVersion);
221+
$isMozilla = preg_match('@Mozilla/([0-9]\.[0-9]{1,2})@', $httpUserAgent, $mozillaVersion) === 1;
222222

223-
if (preg_match('@Opera(/| )([0-9]\.[0-9]{1,2})@', $httpUserAgent, $logVersion)) {
223+
if (preg_match('@Opera(/| )([0-9]\.[0-9]{1,2})@', $httpUserAgent, $logVersion) === 1) {
224224
$this->set('PMA_USR_BROWSER_VER', $logVersion[2]);
225225
$this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
226-
} elseif (preg_match('@(MS)?IE ([0-9]{1,2}\.[0-9]{1,2})@', $httpUserAgent, $logVersion)) {
226+
} elseif (preg_match('@(MS)?IE ([0-9]{1,2}\.[0-9]{1,2})@', $httpUserAgent, $logVersion) === 1) {
227227
$this->set('PMA_USR_BROWSER_VER', $logVersion[2]);
228228
$this->set('PMA_USR_BROWSER_AGENT', 'IE');
229-
} elseif (preg_match('@Trident/(7)\.0@', $httpUserAgent, $logVersion)) {
229+
} elseif (preg_match('@Trident/(7)\.0@', $httpUserAgent, $logVersion) === 1) {
230230
$this->set('PMA_USR_BROWSER_VER', (int) $logVersion[1] + 4);
231231
$this->set('PMA_USR_BROWSER_AGENT', 'IE');
232-
} elseif (preg_match('@OmniWeb/([0-9]{1,3})@', $httpUserAgent, $logVersion)) {
232+
} elseif (preg_match('@OmniWeb/([0-9]{1,3})@', $httpUserAgent, $logVersion) === 1) {
233233
$this->set('PMA_USR_BROWSER_VER', $logVersion[1]);
234234
$this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
235235
// Konqueror 2.2.2 says Konqueror/2.2.2
236236
// Konqueror 3.0.3 says Konqueror/3
237-
} elseif (preg_match('@(Konqueror/)(.*)(;)@', $httpUserAgent, $logVersion)) {
237+
} elseif (preg_match('@(Konqueror/)(.*)(;)@', $httpUserAgent, $logVersion) === 1) {
238238
$this->set('PMA_USR_BROWSER_VER', $logVersion[2]);
239239
$this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
240240
// must check Chrome before Safari
241-
} elseif ($isMozilla && preg_match('@Chrome/([0-9.]*)@', $httpUserAgent, $logVersion)) {
241+
} elseif ($isMozilla && preg_match('@Chrome/([0-9.]*)@', $httpUserAgent, $logVersion) === 1) {
242242
$this->set('PMA_USR_BROWSER_VER', $logVersion[1]);
243243
$this->set('PMA_USR_BROWSER_AGENT', 'CHROME');
244244
// newer Safari
245-
} elseif ($isMozilla && preg_match('@Version/(.*) Safari@', $httpUserAgent, $logVersion)) {
245+
} elseif ($isMozilla && preg_match('@Version/(.*) Safari@', $httpUserAgent, $logVersion) === 1) {
246246
$this->set('PMA_USR_BROWSER_VER', $logVersion[1]);
247247
$this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
248248
// older Safari
249-
} elseif ($isMozilla && preg_match('@Safari/([0-9]*)@', $httpUserAgent, $logVersion)) {
249+
} elseif ($isMozilla && preg_match('@Safari/([0-9]*)@', $httpUserAgent, $logVersion) === 1) {
250250
$this->set('PMA_USR_BROWSER_VER', $mozillaVersion[1] . '.' . $logVersion[1]);
251251
$this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
252252
// Firefox
253253
} elseif (
254254
! str_contains($httpUserAgent, 'compatible')
255-
&& preg_match('@Firefox/([\w.]+)@', $httpUserAgent, $logVersion)
255+
&& preg_match('@Firefox/([\w.]+)@', $httpUserAgent, $logVersion) === 1
256256
) {
257257
$this->set('PMA_USR_BROWSER_VER', $logVersion[1]);
258258
$this->set('PMA_USR_BROWSER_AGENT', 'FIREFOX');
259-
} elseif (preg_match('@rv:1\.9(.*)Gecko@', $httpUserAgent)) {
259+
} elseif (preg_match('@rv:1\.9(.*)Gecko@', $httpUserAgent) === 1) {
260260
$this->set('PMA_USR_BROWSER_VER', '1.9');
261261
$this->set('PMA_USR_BROWSER_AGENT', 'GECKO');
262262
} elseif ($isMozilla) {

‎src/Config/FormDisplay.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private function displayFieldInput(
408408
// TrustedProxies requires changes before displaying
409409
if ($systemPath === 'TrustedProxies') {
410410
foreach ($value as $ip => &$v) {
411-
if (preg_match('/^-\d+$/', $ip)) {
411+
if (preg_match('/^-\d+$/', $ip) === 1) {
412412
continue;
413413
}
414414

@@ -667,8 +667,7 @@ public function save(array $forms, bool $allowPartialSave = true): bool
667667
$i = 0;
668668
foreach ($values[$path] as $value) {
669669
$matches = [];
670-
$match = preg_match('/^(.+):(?:[ ]?)(\\w+)$/', $value, $matches);
671-
if ($match) {
670+
if (preg_match('/^(.+):(?:[ ]?)(\\w+)$/', $value, $matches) === 1) {
672671
// correct 'IP: HTTP header' pair
673672
$ip = trim($matches[1]);
674673
$proxies[$ip] = trim($matches[2]);

‎src/Config/Validator.php‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,7 @@ private static function validateTrustedProxies(string $path, array $values): arr
440440
$lines = [];
441441
foreach ($values[$path] as $ip => $v) {
442442
$v = Util::requestString($v);
443-
$lines[] = preg_match('/^-\d+$/', $ip)
444-
? $v
445-
: $ip . ': ' . $v;
443+
$lines[] = preg_match('/^-\d+$/', $ip) === 1 ? $v : $ip . ': ' . $v;
446444
}
447445
} else {
448446
// AJAX validation
@@ -453,7 +451,7 @@ private static function validateTrustedProxies(string $path, array $values): arr
453451
$line = trim($line);
454452
$matches = [];
455453
// we catch anything that may (or may not) be an IP
456-
if (! preg_match('/^(.+):(?:[ ]?)\\w+$/', $line, $matches)) {
454+
if (preg_match('/^(.+):(?:[ ]?)\\w+$/', $line, $matches) !== 1) {
457455
$result[$path][] = __('Incorrect value:') . ' '
458456
. htmlspecialchars($line);
459457
continue;
@@ -593,9 +591,7 @@ private static function validateByRegex(string $path, array $values, string $reg
593591
return '';
594592
}
595593

596-
$result = preg_match($regex, Util::requestString($values[$path]));
597-
598-
return [$path => $result ? '' : __('Incorrect value!')];
594+
return [$path => preg_match($regex, Util::requestString($values[$path])) === 1 ? '' : __('Incorrect value!')];
599595
}
600596

601597
/**

‎src/ConfigStorage/Relation.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,10 @@ public function buildForeignDropdown(array $foreign, string $data, string $mode)
750750
$key = (string) $key;
751751
$value = (string) $value;
752752

753-
if (mb_check_encoding($key, 'utf-8') && ! preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', $key)) {
753+
if (
754+
mb_check_encoding($key, 'utf-8')
755+
&& preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', $key) !== 1
756+
) {
754757
$selected = $key === $data;
755758
// show as text if it's valid utf-8
756759
$key = htmlspecialchars($key);
@@ -765,7 +768,7 @@ public function buildForeignDropdown(array $foreign, string $data, string $mode)
765768

766769
if (
767770
mb_check_encoding($value, 'utf-8')
768-
&& ! preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', $value)
771+
&& preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', $value) !== 1
769772
) {
770773
if (mb_strlen($value) <= $this->config->settings['LimitChars']) {
771774
// show as text if it's valid utf-8
@@ -1375,7 +1378,7 @@ public function getCreateTableSqlQueries(array $tableNameReplacements): array
13751378
$queries = explode(';', $createTablesFile);
13761379

13771380
foreach ($queries as $query) {
1378-
if (! preg_match('/CREATE TABLE IF NOT EXISTS `(.*)` \(/', $query, $table)) {
1381+
if (preg_match('/CREATE TABLE IF NOT EXISTS `(.*)` \(/', $query, $table) !== 1) {
13791382
continue;
13801383
}
13811384

‎src/Controllers/Database/StructureController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ private function hasTable(array $db, string $truename): bool
633633
'@^' .
634634
preg_quote(mb_substr($this->replication->extractDbOrTable($dbTable, 'table'), 0, -1), '@') . '@',
635635
$truename,
636-
)
636+
) === 1
637637
) {
638638
return true;
639639
}

‎src/Controllers/GisDataEditorController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private function extractGisType(mixed $gisType, string $type, string|null $value
132132
$gisType = mb_strtoupper($type);
133133
}
134134

135-
if ($value !== null && trim($value) !== '' && preg_match('/^\'?(\w+)\b/', $value, $matches)) {
135+
if ($value !== null && trim($value) !== '' && preg_match('/^\'?(\w+)\b/', $value, $matches) === 1) {
136136
$gisType = $matches[1];
137137
}
138138

0 commit comments

Comments
 (0)