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/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,7 @@ private static function fill_form_defaults( $post_values, array &$values ) {

foreach ( array( 'before', 'after', 'submit' ) as $h ) {
if ( ! isset( $values[ $h . '_html' ] ) ) {
$values[ $h . '_html' ] = ( $post_values['options'][ $h . '_html' ] ?? FrmFormsHelper::get_default_html( $h ) );
$values[ $h . '_html' ] = $post_values['options'][ $h . '_html' ] ?? FrmFormsHelper::get_default_html( $h );
}
unset( $h );
}
Expand Down
2 changes: 1 addition & 1 deletion classes/helpers/FrmFormsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ public static function format_link_html( $link_details, $length = 'label' ) {
$link .= ' onclick="return confirm(\'' . esc_attr( $link_details['confirm'] ) . '\')"';
}

$label = ( $link_details[ $length ] ?? $link_details['label'] );
$label = $link_details[ $length ] ?? $link_details['label'];

if ( $length === 'icon' && isset( $link_details[ $length ] ) ) {
$label = '<span class="' . $label . '" title="' . esc_attr( $link_details['label'] ) . '" aria-hidden="true"></span>';
Expand Down
2 changes: 1 addition & 1 deletion classes/helpers/FrmStylesCardHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public static function get_style_param_for_card( $style ) {
if ( empty( $style->post_content['fieldset_bg_color'] ) ) {
$background_color = '#fff';
} else {
$background_color = ( str_starts_with( $style->post_content['fieldset_bg_color'], 'rgb' ) ? $style->post_content['fieldset_bg_color'] : '#' . $style->post_content['fieldset_bg_color'] );
$background_color = str_starts_with( $style->post_content['fieldset_bg_color'], 'rgb' ) ? $style->post_content['fieldset_bg_color'] : '#' . $style->post_content['fieldset_bg_color'];
}
$styles[] = '--preview-background-color: ' . $background_color;

Expand Down
2 changes: 1 addition & 1 deletion classes/models/FrmEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ private function prepare_email_setting( $value, $user_id_args ) {
* @return array|string $emails
*/
private function explode_emails( $emails ) {
$emails = ( ! empty( $emails ) ? preg_split( '/(,|;)/', $emails ) : '' );
$emails = ! empty( $emails ) ? preg_split( '/(,|;)/', $emails ) : '';

return is_array( $emails ) ? array_map( 'trim', $emails ) : trim( $emails );
}
Expand Down
2 changes: 1 addition & 1 deletion classes/models/FrmFormAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ protected function get_group( $action_options ) {
*/
public function get_field_name( $field_name, $post_field = 'post_content' ) {
$name = $this->option_name . '[' . $this->number . ']';
$name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' );
$name .= empty( $post_field ) ? '' : '[' . $post_field . ']';

return $name . ( '[' . $field_name . ']' );
}
Expand Down
4 changes: 2 additions & 2 deletions classes/models/fields/FrmFieldUserID.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function prepare_field_html( $args ) {
*/
protected function get_field_value( $args ) {
$user_ID = get_current_user_id();
$user_ID = ( $user_ID ? $user_ID : '' );
$user_ID = $user_ID ? $user_ID : '';
$posted_value = ( FrmAppHelper::is_admin() && $_POST && isset( $_POST['item_meta'][ $this->field['id'] ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$action = ( $args['action'] ?? $args['form_action'] ?? '' );
$action = $args['action'] ?? $args['form_action'] ?? '';
$updating = $action === 'update';
return is_numeric( $this->field['value'] ) || $posted_value || $updating ? $this->field['value'] : $user_ID;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php
/**
* Formidable_Sniffs_CodeAnalysis_RedundantParenthesesSniff
*
* Detects redundant parentheses around simple expressions in assignments.
* For example: $var = ( $a ?? $b ); can be simplified to $var = $a ?? $b;
*
* @package Formidable\Sniffs
*/

namespace Formidable\Sniffs\CodeAnalysis;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

/**
* Detects redundant parentheses around simple expressions in assignments.
*
* Bad:
* $var = ( $post_values['key'] ?? $default );
*
* Good:
* $var = $post_values['key'] ?? $default;
*/
class RedundantParenthesesSniff implements Sniff {

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
return array( T_OPEN_PARENTHESIS );
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

// Must have a closing parenthesis.
if ( ! isset( $tokens[ $stackPtr ]['parenthesis_closer'] ) ) {
return;
}

$closeParen = $tokens[ $stackPtr ]['parenthesis_closer'];

// Check what's before the opening parenthesis.
$prevToken = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true );

if ( false === $prevToken ) {
return;
}

// We only care about parentheses after an assignment operator or array arrow.
$assignmentTokens = array(
T_EQUAL,
T_DOUBLE_ARROW,
T_COALESCE_EQUAL,
T_PLUS_EQUAL,
T_MINUS_EQUAL,
T_CONCAT_EQUAL,
);

if ( ! in_array( $tokens[ $prevToken ]['code'], $assignmentTokens, true ) ) {
return;
}

// Check what's after the closing parenthesis.
$afterClose = $phpcsFile->findNext( T_WHITESPACE, $closeParen + 1, null, true );

if ( false === $afterClose ) {
return;
}

// The expression should end with a semicolon or comma (for array elements).
if ( $tokens[ $afterClose ]['code'] !== T_SEMICOLON && $tokens[ $afterClose ]['code'] !== T_COMMA && $tokens[ $afterClose ]['code'] !== T_CLOSE_PARENTHESIS ) {
return;
}

// Check if the content inside is a simple expression (no complex operators that would need grouping).
if ( ! $this->isSimpleExpression( $phpcsFile, $stackPtr, $closeParen ) ) {
return;
}

$fix = $phpcsFile->addFixableError(
'Redundant parentheses around simple expression',
$stackPtr,
'Found'
);

if ( true === $fix ) {
$phpcsFile->fixer->beginChangeset();

// Remove opening parenthesis.
$phpcsFile->fixer->replaceToken( $stackPtr, '' );

// Remove whitespace after opening parenthesis.
$next = $stackPtr + 1;

while ( $next < $closeParen && $tokens[ $next ]['code'] === T_WHITESPACE ) {
$phpcsFile->fixer->replaceToken( $next, '' );
++$next;
}

// Remove whitespace before closing parenthesis.
$prev = $closeParen - 1;

while ( $prev > $stackPtr && $tokens[ $prev ]['code'] === T_WHITESPACE ) {
$phpcsFile->fixer->replaceToken( $prev, '' );
--$prev;
}

// Remove closing parenthesis.
$phpcsFile->fixer->replaceToken( $closeParen, '' );

$phpcsFile->fixer->endChangeset();
}
}

/**
* Check if the expression inside parentheses is simple enough that parentheses are redundant.
*
* A simple expression is one that:
* - Contains only a null coalescing operator (??)
* - Contains only a ternary operator
* - Is a single value/variable/function call
*
* @param File $phpcsFile The file being scanned.
* @param int $openParen The position of the opening parenthesis.
* @param int $closeParen The position of the closing parenthesis.
*
* @return bool True if the expression is simple, false otherwise.
*/
private function isSimpleExpression( File $phpcsFile, $openParen, $closeParen ) {
$tokens = $phpcsFile->getTokens();

// Count operators that would make this a complex expression.
$hasCoalesce = false;
$hasTernary = false;
$hasLogicalOp = false;
$hasComparisonOp = false;
$hasArithmeticOp = false;
$nestedParenDepth = 0;

for ( $i = $openParen + 1; $i < $closeParen; $i++ ) {
$code = $tokens[ $i ]['code'];

// Track nested parentheses - we only care about the top level.
if ( $code === T_OPEN_PARENTHESIS ) {
++$nestedParenDepth;
continue;
}

if ( $code === T_CLOSE_PARENTHESIS ) {
--$nestedParenDepth;
continue;
}

// Skip tokens inside nested parentheses.
if ( $nestedParenDepth > 0 ) {
continue;
}

// Check for various operators.
if ( $code === T_COALESCE ) {
$hasCoalesce = true;
} elseif ( $code === T_INLINE_THEN || $code === T_INLINE_ELSE ) {
$hasTernary = true;
} elseif ( in_array( $code, array( T_BOOLEAN_AND, T_BOOLEAN_OR, T_LOGICAL_AND, T_LOGICAL_OR, T_LOGICAL_XOR ), true ) ) {
$hasLogicalOp = true;
} elseif ( in_array( $code, array( T_IS_EQUAL, T_IS_NOT_EQUAL, T_IS_IDENTICAL, T_IS_NOT_IDENTICAL, T_LESS_THAN, T_GREATER_THAN, T_IS_SMALLER_OR_EQUAL, T_IS_GREATER_OR_EQUAL, T_SPACESHIP ), true ) ) {
$hasComparisonOp = true;
} elseif ( in_array( $code, array( T_PLUS, T_MINUS, T_MULTIPLY, T_DIVIDE, T_MODULUS ), true ) ) {
$hasArithmeticOp = true;
}
}

// If there are logical or comparison operators combined with other operators, it's complex.
if ( $hasLogicalOp || $hasComparisonOp ) {
return false;
}

// If there are arithmetic operators, it might need parentheses for precedence.
if ( $hasArithmeticOp ) {
return false;
}

// A simple null coalesce or ternary without other operators is fine.
if ( $hasCoalesce || $hasTernary ) {
return true;
}

// If there are no operators at all, it's a simple value - parentheses are redundant.
return true;
}
}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,5 @@
<rule ref="Formidable.WhiteSpace.BlankLineAfterClosingBrace" />
<rule ref="Formidable.WhiteSpace.BlankLineBeforeReturnAfterBrace" />
<rule ref="Formidable.CodeAnalysis.RedundantEmptyOnParameter" />
<rule ref="Formidable.CodeAnalysis.RedundantParentheses" />
</ruleset>