Skip to content

Commit 3c851f1

Browse files
authored
Merge pull request #9361 from samsonasik/refactor-more-strict
refactor: use more strict result check on preg_match_all() result
2 parents 8e0bb04 + 82340aa commit 3c851f1

File tree

10 files changed

+16
-16
lines changed

10 files changed

+16
-16
lines changed

system/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public static function promptByMultipleKeys(string $text, array $options): array
348348
// return the prompt again if $input contain(s) non-numeric character, except a comma.
349349
// And if max from $options less than max from input,
350350
// it means user tried to access null value in $options
351-
if ($pattern === 0 || $maxOptions < $maxInput) {
351+
if ($pattern < 1 || $maxOptions < $maxInput) {
352352
static::error('Please select correctly.');
353353
CLI::newLine();
354354

system/Database/BaseBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
663663
} else {
664664
// Split multiple conditions
665665
// @TODO This does not parse `BETWEEN a AND b` correctly.
666-
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
666+
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE) >= 1) {
667667
$conditions = [];
668668
$joints = $joints[0];
669669
array_unshift($joints, ['', 0]);
@@ -3470,7 +3470,7 @@ protected function getOperator(string $str, bool $list = false)
34703470
'/' . implode('|', $this->pregOperators) . '/i',
34713471
$str,
34723472
$match
3473-
) ? ($list ? $match[0] : $match[0][0]) : false;
3473+
) >= 1 ? ($list ? $match[0] : $match[0][0]) : false;
34743474
}
34753475

34763476
/**
@@ -3501,7 +3501,7 @@ private function getOperatorFromWhereKey(string $whereKey)
35013501
'/' . implode('|', $pregOperators) . '/i',
35023502
$whereKey,
35033503
$match
3504-
) ? $match[0] : false;
3504+
) >= 1 ? $match[0] : false;
35053505
}
35063506

35073507
/**

system/Database/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ protected function matchNamedBinds(string $sql, array $binds): string
342342
*/
343343
protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, int $ml): string
344344
{
345-
if ($c = preg_match_all("/'[^']*'/", $sql, $matches)) {
345+
if ($c = preg_match_all("/'[^']*'/", $sql, $matches) >= 1) {
346346
$c = preg_match_all('/' . preg_quote($this->bindMarker, '/') . '/i', str_replace($matches[0], str_replace($this->bindMarker, str_repeat(' ', $ml), $matches[0]), $sql, $c), $matches, PREG_OFFSET_CAPTURE);
347347

348348
// Bind values' count must match the count of markers in the query

system/Database/SQLSRV/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
122122
$cond = ' ON ' . $cond;
123123
} else {
124124
// Split multiple conditions
125-
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
125+
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE) >= 1) {
126126
$conditions = [];
127127
$joints = $joints[0];
128128
array_unshift($joints, ['', 0]);

system/Email/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ public function wordWrap($str, $charlim = null)
10311031

10321032
$unwrap = [];
10331033

1034-
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches)) {
1034+
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches) >= 1) {
10351035
for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
10361036
$unwrap[] = $matches[1][$i];
10371037
$str = str_replace($matches[0][$i], '{{unwrapped' . $i . '}}', $str);

system/Helpers/text_helper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function ascii_to_entities(string $str): string
131131
*/
132132
function entities_to_ascii(string $str, bool $all = true): string
133133
{
134-
if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
134+
if (preg_match_all('/\&#(\d+)\;/', $str, $matches) >= 1) {
135135
for ($i = 0, $s = count($matches[0]); $i < $s; $i++) {
136136
$digits = (int) $matches[1][$i];
137137
$out = '';
@@ -195,7 +195,7 @@ function word_censor(string $str, array $censored, string $replacement = ''): st
195195
"\\1{$replacement}\\3",
196196
$str
197197
);
198-
} elseif (preg_match_all("/{$delim}(" . $badword . "){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE)) {
198+
} elseif (preg_match_all("/{$delim}(" . $badword . "){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) >= 1) {
199199
$matches = $matches[1];
200200

201201
for ($i = count($matches) - 1; $i >= 0; $i--) {
@@ -351,7 +351,7 @@ function word_wrap(string $str, int $charlim = 76): string
351351
// strip the entire chunk and replace it with a marker.
352352
$unwrap = [];
353353

354-
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches)) {
354+
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches) >= 1) {
355355
for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
356356
$unwrap[] = $matches[1][$i];
357357
$str = str_replace($matches[0][$i], '{{unwrapped' . $i . '}}', $str);

system/Helpers/url_helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ function auto_link(string $str, string $type = 'both', bool $popup = false): str
360360
$str,
361361
$matches,
362362
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
363-
)
363+
) >= 1
364364
) {
365365
// Set our target HTML if using popup links.
366366
$target = ($popup) ? ' target="_blank"' : '';
@@ -387,7 +387,7 @@ function auto_link(string $str, string $type = 'both', bool $popup = false): str
387387
$str,
388388
$matches,
389389
PREG_OFFSET_CAPTURE
390-
)
390+
) >= 1
391391
) {
392392
foreach (array_reverse($matches[0]) as $match) {
393393
if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== false) {

system/Session/Handlers/MemcachedHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function open($path, $name): bool
9898
$this->savePath,
9999
$matches,
100100
PREG_SET_ORDER
101-
) === 0
101+
) < 1
102102
) {
103103
$this->memcached = null;
104104
$this->logger->error('Session: Invalid Memcached save path format: ' . $this->savePath);

system/Typography/Typography.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function autoTypography(string $str, bool $reduceLinebreaks = false): str
9696

9797
// HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
9898
$htmlComments = [];
99-
if (str_contains($str, '<!--') && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches)) {
99+
if (str_contains($str, '<!--') && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches) >= 1) {
100100
for ($i = 0, $total = count($matches[0]); $i < $total; $i++) {
101101
$htmlComments[] = $matches[0][$i];
102102
$str = str_replace($matches[0][$i], '{@HC' . $i . '}', $str);

system/View/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ protected function extractNoparse(string $template): string
419419
* $matches[][0] is the raw match
420420
* $matches[][1] is the contents
421421
*/
422-
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER)) {
422+
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER) >= 1) {
423423
foreach ($matches as $match) {
424424
// Create a hash of the contents to insert in its place.
425425
$hash = md5($match[1]);
@@ -691,7 +691,7 @@ protected function parsePlugins(string $template)
691691
* $matches[1] = all parameters string in opening tag
692692
* $matches[2] = content between the tags to send to the plugin.
693693
*/
694-
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER) === 0) {
694+
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER) < 1) {
695695
continue;
696696
}
697697

0 commit comments

Comments
 (0)