Skip to content

Commit

Permalink
Merge pull request #9361 from samsonasik/refactor-more-strict
Browse files Browse the repository at this point in the history
refactor: use more strict result check on preg_match_all() result
  • Loading branch information
samsonasik authored Jan 2, 2025
2 parents 8e0bb04 + 82340aa commit 3c851f1
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static function promptByMultipleKeys(string $text, array $options): array
// return the prompt again if $input contain(s) non-numeric character, except a comma.
// And if max from $options less than max from input,
// it means user tried to access null value in $options
if ($pattern === 0 || $maxOptions < $maxInput) {
if ($pattern < 1 || $maxOptions < $maxInput) {
static::error('Please select correctly.');
CLI::newLine();

Expand Down
6 changes: 3 additions & 3 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
} else {
// Split multiple conditions
// @TODO This does not parse `BETWEEN a AND b` correctly.
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE) >= 1) {
$conditions = [];
$joints = $joints[0];
array_unshift($joints, ['', 0]);
Expand Down Expand Up @@ -3470,7 +3470,7 @@ protected function getOperator(string $str, bool $list = false)
'/' . implode('|', $this->pregOperators) . '/i',
$str,
$match
) ? ($list ? $match[0] : $match[0][0]) : false;
) >= 1 ? ($list ? $match[0] : $match[0][0]) : false;
}

/**
Expand Down Expand Up @@ -3501,7 +3501,7 @@ private function getOperatorFromWhereKey(string $whereKey)
'/' . implode('|', $pregOperators) . '/i',
$whereKey,
$match
) ? $match[0] : false;
) >= 1 ? $match[0] : false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ protected function matchNamedBinds(string $sql, array $binds): string
*/
protected function matchSimpleBinds(string $sql, array $binds, int $bindCount, int $ml): string
{
if ($c = preg_match_all("/'[^']*'/", $sql, $matches)) {
if ($c = preg_match_all("/'[^']*'/", $sql, $matches) >= 1) {
$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);

// Bind values' count must match the count of markers in the query
Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLSRV/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function join(string $table, $cond, string $type = '', ?bool $escape = nu
$cond = ' ON ' . $cond;
} else {
// Split multiple conditions
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) {
if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE) >= 1) {
$conditions = [];
$joints = $joints[0];
array_unshift($joints, ['', 0]);
Expand Down
2 changes: 1 addition & 1 deletion system/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ public function wordWrap($str, $charlim = null)

$unwrap = [];

if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches)) {
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches) >= 1) {
for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
$unwrap[] = $matches[1][$i];
$str = str_replace($matches[0][$i], '{{unwrapped' . $i . '}}', $str);
Expand Down
6 changes: 3 additions & 3 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function ascii_to_entities(string $str): string
*/
function entities_to_ascii(string $str, bool $all = true): string
{
if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
if (preg_match_all('/\&#(\d+)\;/', $str, $matches) >= 1) {
for ($i = 0, $s = count($matches[0]); $i < $s; $i++) {
$digits = (int) $matches[1][$i];
$out = '';
Expand Down Expand Up @@ -195,7 +195,7 @@ function word_censor(string $str, array $censored, string $replacement = ''): st
"\\1{$replacement}\\3",
$str
);
} elseif (preg_match_all("/{$delim}(" . $badword . "){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE)) {
} elseif (preg_match_all("/{$delim}(" . $badword . "){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) >= 1) {
$matches = $matches[1];

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

if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches)) {
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches) >= 1) {
for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
$unwrap[] = $matches[1][$i];
$str = str_replace($matches[0][$i], '{{unwrapped' . $i . '}}', $str);
Expand Down
4 changes: 2 additions & 2 deletions system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ function auto_link(string $str, string $type = 'both', bool $popup = false): str
$str,
$matches,
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
)
) >= 1
) {
// Set our target HTML if using popup links.
$target = ($popup) ? ' target="_blank"' : '';
Expand All @@ -387,7 +387,7 @@ function auto_link(string $str, string $type = 'both', bool $popup = false): str
$str,
$matches,
PREG_OFFSET_CAPTURE
)
) >= 1
) {
foreach (array_reverse($matches[0]) as $match) {
if (filter_var($match[0], FILTER_VALIDATE_EMAIL) !== false) {
Expand Down
2 changes: 1 addition & 1 deletion system/Session/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function open($path, $name): bool
$this->savePath,
$matches,
PREG_SET_ORDER
) === 0
) < 1
) {
$this->memcached = null;
$this->logger->error('Session: Invalid Memcached save path format: ' . $this->savePath);
Expand Down
2 changes: 1 addition & 1 deletion system/Typography/Typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function autoTypography(string $str, bool $reduceLinebreaks = false): str

// HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
$htmlComments = [];
if (str_contains($str, '<!--') && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches)) {
if (str_contains($str, '<!--') && preg_match_all('#(<!\-\-.*?\-\->)#s', $str, $matches) >= 1) {
for ($i = 0, $total = count($matches[0]); $i < $total; $i++) {
$htmlComments[] = $matches[0][$i];
$str = str_replace($matches[0][$i], '{@HC' . $i . '}', $str);
Expand Down
4 changes: 2 additions & 2 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ protected function extractNoparse(string $template): string
* $matches[][0] is the raw match
* $matches[][1] is the contents
*/
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER)) {
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER) >= 1) {
foreach ($matches as $match) {
// Create a hash of the contents to insert in its place.
$hash = md5($match[1]);
Expand Down Expand Up @@ -691,7 +691,7 @@ protected function parsePlugins(string $template)
* $matches[1] = all parameters string in opening tag
* $matches[2] = content between the tags to send to the plugin.
*/
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER) === 0) {
if (preg_match_all($pattern, $template, $matches, PREG_SET_ORDER) < 1) {
continue;
}

Expand Down

0 comments on commit 3c851f1

Please sign in to comment.