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
2 changes: 1 addition & 1 deletion classes/models/FrmEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ private static function insert_entry_into_database( $new_values ) {

$query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );

return ! $query_results ? false : $wpdb->insert_id;
return $query_results ? $wpdb->insert_id : false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public function process( File $phpcsFile, $stackPtr ) {
return;
}

// Check for negated function call pattern: ! function() ? false : value.
$negationInfo = $this->findNegatedFunctionCall( $phpcsFile, $statementStart, $stackPtr );
// Check for negated condition pattern: ! function() ? false : value or ! $var ? false : value.
$negationInfo = $this->findNegatedCondition( $phpcsFile, $statementStart, $stackPtr );

if ( false !== $negationInfo ) {
$this->processNegatedFunctionCall( $phpcsFile, $stackPtr, $colonPtr, $ternaryEnd, $negationInfo, $trueBranch, $falseBranch );
$this->processNegatedCondition( $phpcsFile, $stackPtr, $colonPtr, $ternaryEnd, $negationInfo, $trueBranch, $falseBranch );
return;
}

Expand Down Expand Up @@ -134,15 +134,15 @@ public function process( File $phpcsFile, $stackPtr ) {
}

/**
* Find a negated function call pattern: ! function().
* Find a negated condition pattern: ! function() or ! $variable.
*
* @param File $phpcsFile The file being scanned.
* @param int $start Start position to search.
* @param int $end End position to search.
*
* @return array|false Array with 'negation' and 'function_end' keys, or false.
* @return array|false Array with 'negation' and 'condition_end' keys, or false.
*/
private function findNegatedFunctionCall( File $phpcsFile, $start, $end ) {
private function findNegatedCondition( File $phpcsFile, $start, $end ) {
$tokens = $phpcsFile->getTokens();

// Look for ! operator.
Expand All @@ -151,19 +151,36 @@ private function findNegatedFunctionCall( File $phpcsFile, $start, $end ) {
continue;
}

// Found !, now check if followed by a function call.
// Found !, now check what follows.
$nextToken = $phpcsFile->findNext( T_WHITESPACE, $i + 1, $end, true );

if ( false === $nextToken ) {
continue;
}

// Check for function call (T_STRING followed by open paren).
// Skip T_EMPTY - ! empty() is a common pattern that shouldn't be flipped.
if ( $tokens[ $nextToken ]['code'] === T_EMPTY ) {
continue;
}

// Check for variable: ! $var.
if ( $tokens[ $nextToken ]['code'] === T_VARIABLE ) {
// Check there's nothing significant between variable and ?.
$afterVar = $phpcsFile->findNext( T_WHITESPACE, $nextToken + 1, $end, true );

if ( false !== $afterVar ) {
// There's something else - not a simple negated variable.
continue;
}

return array(
'negation' => $i,
'condition' => $nextToken,
'condition_end' => $nextToken,
);
}

// Check for function call (T_STRING followed by open paren).
if ( $tokens[ $nextToken ]['code'] !== T_STRING ) {
continue;
}
Expand Down Expand Up @@ -194,29 +211,29 @@ private function findNegatedFunctionCall( File $phpcsFile, $start, $end ) {
}

return array(
'negation' => $i,
'function' => $nextToken,
'function_end' => $closeParen,
'negation' => $i,
'condition' => $nextToken,
'condition_end' => $closeParen,
);
}

return false;
}

/**
* Process a negated function call ternary: ! function() ? false : value.
* Process a negated condition ternary: ! function() ? false : value or ! $var ? false : value.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the ? token.
* @param int $colonPtr The position of the : token.
* @param int $ternaryEnd The end of the ternary.
* @param array $negationInfo Info about the negation.
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the ? token.
* @param int $colonPtr The position of the : token.
* @param int $ternaryEnd The end of the ternary.
* @param array $negationInfo Info about the negation.
* @param string $trueBranch The true branch content.
* @param string $falseBranch The false branch content.
*
* @return void
*/
private function processNegatedFunctionCall( File $phpcsFile, $stackPtr, $colonPtr, $ternaryEnd, $negationInfo, $trueBranch, $falseBranch ) {
private function processNegatedCondition( File $phpcsFile, $stackPtr, $colonPtr, $ternaryEnd, $negationInfo, $trueBranch, $falseBranch ) {
$tokens = $phpcsFile->getTokens();

// Only flip if the true branch is false/null.
Expand Down Expand Up @@ -247,9 +264,9 @@ private function processNegatedFunctionCall( File $phpcsFile, $stackPtr, $colonP
}

$fix = $phpcsFile->addFixableError(
'Ternary condition uses negated function call. Flip to use positive condition instead.',
'Ternary condition uses negation. Flip to use positive condition instead.',
$negationInfo['negation'],
'NegatedFunctionTernary'
'NegatedTernary'
);

if ( true === $fix ) {
Expand Down
Loading