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 @@ -240,10 +240,17 @@ private function negateCondition( $condition ) {
return substr( $condition, 2 );
}

if ( strpos( $condition, '!' ) === 0 ) {
if ( strpos( $condition, '!' ) === 0 && strpos( $condition, '!=' ) !== 0 ) {
return substr( $condition, 1 );
}

// Try to flip comparison operators.
$flipped = $this->flipComparisonOperator( $condition );

if ( $flipped !== false ) {
return $flipped;
}

// Otherwise, add negation.
// If condition is simple (no spaces or operators at top level), just add !
// If complex, wrap in parentheses.
Expand All @@ -254,6 +261,43 @@ private function negateCondition( $condition ) {
return '! ( ' . $condition . ' )';
}

/**
* Try to flip a comparison operator in a condition.
*
* @param string $condition The condition to flip.
*
* @return false|string The flipped condition, or false if not a simple comparison.
*/
private function flipComparisonOperator( $condition ) {
// Map of operators to their opposites.
$operatorMap = array(
'!==' => '===',
'===' => '!==',
'!=' => '==',
'==' => '!=',
'>=' => '<',
'<=' => '>',
'>' => '<=',
'<' => '>=',
);

// 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;
}

return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
}
}

return false;
}

/**
* Check if a condition is simple (single variable/function call).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,17 @@ private function negateCondition( $condition ) {
return substr( $condition, 2 );
}

if ( strpos( $condition, '!' ) === 0 ) {
if ( strpos( $condition, '!' ) === 0 && strpos( $condition, '!=' ) !== 0 ) {
return substr( $condition, 1 );
}

// Try to flip comparison operators.
$flipped = $this->flipComparisonOperator( $condition );

if ( $flipped !== false ) {
return $flipped;
}

// Otherwise, add negation.
// If condition is simple (no spaces or operators at top level), just add !
// If complex, wrap in parentheses.
Expand All @@ -262,6 +269,43 @@ private function negateCondition( $condition ) {
return '! ( ' . $condition . ' )';
}

/**
* Try to flip a comparison operator in a condition.
*
* @param string $condition The condition to flip.
*
* @return false|string The flipped condition, or false if not a simple comparison.
*/
private function flipComparisonOperator( $condition ) {
// Map of operators to their opposites.
$operatorMap = array(
'!==' => '===',
'===' => '!==',
'!=' => '==',
'==' => '!=',
'>=' => '<',
'<=' => '>',
'>' => '<=',
'<' => '>=',
);

// 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;
}

return substr( $condition, 0, $pos ) . $opposite . substr( $condition, $pos + strlen( $op ) );
}
}

return false;
}

/**
* Check if a condition is simple (single variable/function call).
*
Expand Down