Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,30 +269,80 @@ private function negateCondition( $condition ) {
* @return false|string The flipped condition, or false if not a simple comparison.
*/
private function flipComparisonOperator( $condition ) {
// Map of operators to their opposites.
// Map of operators to their opposites (check longer ones first).
$operatorMap = array(
'!==' => '===',
'===' => '!==',
'!=' => '==',
'==' => '!=',
'>=' => '<',
'<=' => '>',
'>' => '<=',
'<' => '>=',
);

// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
}

// Check for each operator (check longer ones first).
foreach ( $operatorMap as $op => $opposite ) {
$pos = strpos( $condition, $op );

if ( $pos !== false ) {
// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
}
}

// Handle single < and > separately to avoid matching -> or =>.
$pos = $this->findComparisonOperator( $condition, '>' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '<=' . substr( $condition, $pos + 1 );
}

$pos = $this->findComparisonOperator( $condition, '<' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '>=' . substr( $condition, $pos + 1 );
}

return false;
}

/**
* Find a single comparison operator (< or >) that is not part of -> or =>.
*
* @param string $condition The condition string.
* @param string $operator The operator to find (< or >).
*
* @return false|int The position of the operator, or false if not found.
*/
private function findComparisonOperator( $condition, $operator ) {
$pos = 0;

while ( ( $pos = strpos( $condition, $operator, $pos ) ) !== false ) {
// Check character before to avoid matching -> or =>.
if ( $pos > 0 ) {
$charBefore = $condition[ $pos - 1 ];

// Skip if this is part of ->, =>, <=, >=, or a multi-char comparison.
if ( $charBefore === '-' || $charBefore === '=' || $charBefore === '<' || $charBefore === '>' || $charBefore === '!' ) {
++$pos;
continue;
}
}

return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
// Check character after to avoid matching <=, >=, =>.
if ( $pos < strlen( $condition ) - 1 ) {
$charAfter = $condition[ $pos + 1 ];

if ( $charAfter === '=' || $charAfter === '>' ) {
++$pos;
continue;
}
}

return $pos;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,30 +322,80 @@ private function hasTopLevelOperator( $condition ) {
* @return false|string The flipped condition, or false if not a simple comparison.
*/
private function flipComparisonOperator( $condition ) {
// Map of operators to their opposites.
// Map of operators to their opposites (check longer ones first).
$operatorMap = array(
'!==' => '===',
'===' => '!==',
'!=' => '==',
'==' => '!=',
'>=' => '<',
'<=' => '>',
'>' => '<=',
'<' => '>=',
);

// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
}

// Check for each operator (check longer ones first).
foreach ( $operatorMap as $op => $opposite ) {
$pos = strpos( $condition, $op );

if ( $pos !== false ) {
// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
}
}

// Handle single < and > separately to avoid matching -> or =>.
$pos = $this->findComparisonOperator( $condition, '>' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '<=' . substr( $condition, $pos + 1 );
}

$pos = $this->findComparisonOperator( $condition, '<' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '>=' . substr( $condition, $pos + 1 );
}

return false;
}

/**
* Find a single comparison operator (< or >) that is not part of -> or =>.
*
* @param string $condition The condition string.
* @param string $operator The operator to find (< or >).
*
* @return false|int The position of the operator, or false if not found.
*/
private function findComparisonOperator( $condition, $operator ) {
$pos = 0;

while ( ( $pos = strpos( $condition, $operator, $pos ) ) !== false ) {
// Check character before to avoid matching -> or =>.
if ( $pos > 0 ) {
$charBefore = $condition[ $pos - 1 ];

// Skip if this is part of ->, =>, <=, >=, or a multi-char comparison.
if ( $charBefore === '-' || $charBefore === '=' || $charBefore === '<' || $charBefore === '>' || $charBefore === '!' ) {
++$pos;
continue;
}
}

return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
// Check character after to avoid matching <=, >=, =>.
if ( $pos < strlen( $condition ) - 1 ) {
$charAfter = $condition[ $pos + 1 ];

if ( $charAfter === '=' || $charAfter === '>' ) {
++$pos;
continue;
}
}

return $pos;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,36 +557,80 @@ private function hasTopLevelOperator( $condition ) {
* @return false|string The flipped condition, or false if not a simple comparison.
*/
private function flipComparisonOperator( $condition ) {
// Skip if condition contains object operators (->), array access, or method calls.
// These can be confused with comparison operators.
if ( strpos( $condition, '->' ) !== false || strpos( $condition, '::' ) !== false ) {
return false;
}

// Map of operators to their opposites.
// Map of operators to their opposites (check longer ones first).
$operatorMap = array(
'!==' => '===',
'===' => '!==',
'!=' => '==',
'==' => '!=',
'>=' => '<',
'<=' => '>',
'>' => '<=',
'<' => '>=',
);

// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
}

// Check for each operator (check longer ones first).
foreach ( $operatorMap as $op => $opposite ) {
$pos = strpos( $condition, $op );

if ( $pos !== false ) {
// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
}
}

// Handle single < and > separately to avoid matching -> or =>.
$pos = $this->findComparisonOperator( $condition, '>' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '<=' . substr( $condition, $pos + 1 );
}

$pos = $this->findComparisonOperator( $condition, '<' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '>=' . substr( $condition, $pos + 1 );
}

return false;
}

/**
* Find a single comparison operator (< or >) that is not part of -> or =>.
*
* @param string $condition The condition string.
* @param string $operator The operator to find (< or >).
*
* @return false|int The position of the operator, or false if not found.
*/
private function findComparisonOperator( $condition, $operator ) {
$pos = 0;

while ( ( $pos = strpos( $condition, $operator, $pos ) ) !== false ) {
// Check character before to avoid matching -> or =>.
if ( $pos > 0 ) {
$charBefore = $condition[ $pos - 1 ];

// Skip if this is part of ->, =>, <=, >=, or a multi-char comparison.
if ( $charBefore === '-' || $charBefore === '=' || $charBefore === '<' || $charBefore === '>' || $charBefore === '!' ) {
++$pos;
continue;
}
}

return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
// Check character after to avoid matching <=, >=, =>.
if ( $pos < strlen( $condition ) - 1 ) {
$charAfter = $condition[ $pos + 1 ];

if ( $charAfter === '=' || $charAfter === '>' ) {
++$pos;
continue;
}
}

return $pos;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,35 +366,80 @@ private function hasTopLevelOperator( $condition ) {
* @return false|string The flipped condition, or false if not a simple comparison.
*/
private function flipComparisonOperator( $condition ) {
// Map of operators to their opposites.
// Map of operators to their opposites (check longer ones first).
$operatorMap = array(
'!==' => '===',
'===' => '!==',
'!=' => '==',
'==' => '!=',
'>=' => '<',
'<=' => '>',
'>' => '<=',
'<' => '>=',
);

// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
}

// Check for each operator (check longer ones first).
foreach ( $operatorMap as $op => $opposite ) {
$pos = strpos( $condition, $op );

if ( $pos !== false ) {
// Make sure this is a simple comparison (no && or ||).
if ( strpos( $condition, '&&' ) !== false || strpos( $condition, '||' ) !== false ) {
return false;
}
if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
}
}

// Handle single < and > separately to avoid matching -> or =>.
$pos = $this->findComparisonOperator( $condition, '>' );

// Skip if this is part of -> (object operator).
if ( $op === '>' && $pos > 0 && $condition[ $pos - 1 ] === '-' ) {
if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '<=' . substr( $condition, $pos + 1 );
}

$pos = $this->findComparisonOperator( $condition, '<' );

if ( false !== $pos ) {
return substr( $condition, 0, $pos ) . '>=' . substr( $condition, $pos + 1 );
}

return false;
}

/**
* Find a single comparison operator (< or >) that is not part of -> or =>.
*
* @param string $condition The condition string.
* @param string $operator The operator to find (< or >).
*
* @return false|int The position of the operator, or false if not found.
*/
private function findComparisonOperator( $condition, $operator ) {
$pos = 0;

while ( ( $pos = strpos( $condition, $operator, $pos ) ) !== false ) {
// Check character before to avoid matching -> or =>.
if ( $pos > 0 ) {
$charBefore = $condition[ $pos - 1 ];

// Skip if this is part of ->, =>, <=, >=, or a multi-char comparison.
if ( $charBefore === '-' || $charBefore === '=' || $charBefore === '<' || $charBefore === '>' || $charBefore === '!' ) {
++$pos;
continue;
}
}

return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
// Check character after to avoid matching <=, >=, =>.
if ( $pos < strlen( $condition ) - 1 ) {
$charAfter = $condition[ $pos + 1 ];

if ( $charAfter === '=' || $charAfter === '>' ) {
++$pos;
continue;
}
}

return $pos;
}

return false;
Expand Down
Loading
Loading