From aef1f5b391d8d42001a0e5c7d413e6a728b1fea0 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 9 Jan 2026 10:00:46 -0400 Subject: [PATCH 1/2] Add new sniff to simplify truthy checks --- classes/controllers/FrmAppController.php | 2 +- .../controllers/FrmDashboardController.php | 2 +- classes/controllers/FrmEntriesController.php | 10 +- classes/controllers/FrmFieldsController.php | 2 +- .../controllers/FrmFormActionsController.php | 6 +- classes/controllers/FrmFormsController.php | 4 +- classes/controllers/FrmStylesController.php | 2 +- classes/controllers/FrmXMLController.php | 2 +- classes/helpers/FrmAppHelper.php | 16 +- classes/helpers/FrmDashboardHelper.php | 2 +- classes/helpers/FrmFieldsHelper.php | 2 +- classes/helpers/FrmFormsHelper.php | 8 +- classes/helpers/FrmStylesHelper.php | 2 +- classes/helpers/FrmXMLHelper.php | 4 +- classes/models/FrmCreateFile.php | 2 +- classes/models/FrmDb.php | 4 +- classes/models/FrmEntry.php | 2 +- classes/models/FrmEntryValidate.php | 6 +- classes/models/FrmField.php | 6 +- classes/models/FrmFormApi.php | 4 +- classes/models/FrmInstallerSkin.php | 2 +- classes/models/FrmSolution.php | 2 +- classes/models/fields/FrmFieldDefault.php | 2 +- classes/models/fields/FrmFieldType.php | 2 +- .../RedundantEmptyOnParameterSniff.php | 273 ++++++++++++++++++ phpcs.xml | 1 + stripe/helpers/FrmTransLiteAppHelper.php | 4 +- stripe/models/FrmStrpLiteAuth.php | 2 +- tests/phpunit/emails/test_FrmEmail.php | 2 +- 29 files changed, 326 insertions(+), 52 deletions(-) create mode 100644 phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php diff --git a/classes/controllers/FrmAppController.php b/classes/controllers/FrmAppController.php index c5f1b02997..0029eee390 100644 --- a/classes/controllers/FrmAppController.php +++ b/classes/controllers/FrmAppController.php @@ -258,7 +258,7 @@ public static function load_wp_admin_style() { public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' ); - if ( empty( $show_nav ) || ! $form ) { + if ( ! $show_nav || ! $form ) { return; } diff --git a/classes/controllers/FrmDashboardController.php b/classes/controllers/FrmDashboardController.php index 767b3bc523..eb816d4086 100644 --- a/classes/controllers/FrmDashboardController.php +++ b/classes/controllers/FrmDashboardController.php @@ -197,7 +197,7 @@ public static function view_args_build_counter( $heading, $cta = array(), $value 'type' => 'default', ); - if ( ! empty( $cta ) ) { + if ( $cta ) { $counter_args['cta'] = $cta; } diff --git a/classes/controllers/FrmEntriesController.php b/classes/controllers/FrmEntriesController.php index 089761cf40..f5bb6a99d2 100644 --- a/classes/controllers/FrmEntriesController.php +++ b/classes/controllers/FrmEntriesController.php @@ -343,7 +343,7 @@ public static function check_hidden_cols( $check, $object_id, $meta_key, $meta_v return $check; } - if ( empty( $prev_value ) ) { + if ( ! $prev_value ) { $prev_value = get_metadata( 'user', $object_id, $meta_key, true ); } @@ -438,7 +438,7 @@ private static function hidden_column_key( $menu_name = '' ) { * @return string */ private static function base_column_key( $menu_name = '' ) { - if ( empty( $menu_name ) ) { + if ( ! $menu_name ) { $menu_name = FrmAppHelper::get_menu_name(); } @@ -592,7 +592,7 @@ private static function remove_excess_cols( $atts, &$result ) { break; } - if ( empty( $result ) || ! in_array( $col_key, $result, true ) ) { + if ( ! $result || ! in_array( $col_key, $result, true ) ) { $result[] = $col_key; --$i; } @@ -641,7 +641,7 @@ public static function display_list( $message = '', $errors = array() ) { die(); } - if ( empty( $message ) && isset( $_GET['import-message'] ) ) { + if ( ! $message && isset( $_GET['import-message'] ) ) { $message = __( 'Your import is complete', 'formidable' ); } @@ -794,7 +794,7 @@ public static function process_entry( $errors = '', $ajax = false ) { $frm_vars['created_entries'][ $form_id ] = array( 'errors' => $errors ); - if ( empty( $errors ) ) { + if ( ! $errors ) { $_POST['frm_skip_cookie'] = 1; $do_success = false; diff --git a/classes/controllers/FrmFieldsController.php b/classes/controllers/FrmFieldsController.php index 40c3bc6592..67e8fbb579 100644 --- a/classes/controllers/FrmFieldsController.php +++ b/classes/controllers/FrmFieldsController.php @@ -92,7 +92,7 @@ public static function create() { public static function include_new_field( $field_type, $form_id, $field_options = array() ) { $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id ); - if ( ! empty( $field_options ) ) { + if ( $field_options ) { $field_values['field_options'] = array_merge( $field_values['field_options'], $field_options ); } diff --git a/classes/controllers/FrmFormActionsController.php b/classes/controllers/FrmFormActionsController.php index e37f774b48..98e7f6d722 100644 --- a/classes/controllers/FrmFormActionsController.php +++ b/classes/controllers/FrmFormActionsController.php @@ -357,7 +357,7 @@ public static function get_form_actions( $action = 'all' ) { * @return void */ public static function list_actions( $form, $values ) { - if ( empty( $form ) ) { + if ( ! $form ) { return; } @@ -611,7 +611,7 @@ public static function update_settings( $form_id ) { * @return void */ public static function delete_missing_actions( $old_actions ) { - if ( ! empty( $old_actions ) ) { + if ( $old_actions ) { foreach ( $old_actions as $old_id ) { wp_delete_post( $old_id ); } @@ -696,7 +696,7 @@ public static function trigger_actions( $event, $form, $entry, $type = 'all', $a $entry = FrmEntry::getOne( $entry, true ); } - if ( empty( $entry ) || ( FrmEntriesHelper::DRAFT_ENTRY_STATUS === (int) $entry->is_draft && 'draft' !== $event ) ) { + if ( ! $entry || ( FrmEntriesHelper::DRAFT_ENTRY_STATUS === (int) $entry->is_draft && 'draft' !== $event ) ) { continue; } diff --git a/classes/controllers/FrmFormsController.php b/classes/controllers/FrmFormsController.php index f2f3cf839e..6f99bc68e1 100644 --- a/classes/controllers/FrmFormsController.php +++ b/classes/controllers/FrmFormsController.php @@ -295,7 +295,7 @@ private static function antispam_was_on( $form_id ) { * @return void */ public static function update( $values = array() ) { - if ( empty( $values ) ) { + if ( ! $values ) { $values = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing } @@ -2485,7 +2485,7 @@ public static function show_form( $id = '', $key = '', $title = false, $descript private static function maybe_get_form_to_show( $id ) { $form = false; - if ( ! empty( $id ) ) { + if ( $id ) { // Form id or key is set. $form = FrmForm::getOne( $id ); diff --git a/classes/controllers/FrmStylesController.php b/classes/controllers/FrmStylesController.php index 7312b34438..8058de306b 100644 --- a/classes/controllers/FrmStylesController.php +++ b/classes/controllers/FrmStylesController.php @@ -1353,7 +1353,7 @@ public static function do_accordion_sections( $screen, $context, $data_object ) wp_enqueue_script( 'accordion' ); - if ( empty( $screen ) ) { + if ( ! $screen ) { $screen = get_current_screen(); } elseif ( is_string( $screen ) ) { $screen = convert_to_screen( $screen ); diff --git a/classes/controllers/FrmXMLController.php b/classes/controllers/FrmXMLController.php index 0592f12014..dc337716dd 100644 --- a/classes/controllers/FrmXMLController.php +++ b/classes/controllers/FrmXMLController.php @@ -187,7 +187,7 @@ private static function override_url( $form, &$url ) { * @return string */ private static function get_selected_in_form( $form, $value = 'form' ) { - if ( ! empty( $form ) && ! empty( $form[ $value ] ) ) { + if ( $form && ! empty( $form[ $value ] ) ) { return $form[ $value ]; } diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index a70e4d7b43..91e954c763 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1004,7 +1004,7 @@ public static function decode_specialchars( &$value ) { */ private static function decode_amp( &$string ) { // Don't bother if there are no entities - saves a lot of processing - if ( empty( $string ) || ! str_contains( $string, '&' ) ) { + if ( ! $string || ! str_contains( $string, '&' ) ) { return; } @@ -1155,7 +1155,7 @@ private static function allowed_html( $allowed ) { if ( $allowed === 'all' ) { $allowed_html = $html; - } elseif ( ! empty( $allowed ) ) { + } elseif ( $allowed ) { foreach ( (array) $allowed as $a ) { $allowed_html[ $a ] = $html[ $a ] ?? array(); } @@ -2406,7 +2406,7 @@ public static function permission_check( $permission, $show_message = 'show' ) { * @return false|string The permission message or false if allowed */ public static function permission_nonce_error( $permission, $nonce_name = '', $nonce = '' ) { - if ( ! empty( $permission ) && ! current_user_can( $permission ) && ! current_user_can( 'administrator' ) ) { + if ( $permission && ! current_user_can( $permission ) && ! current_user_can( 'administrator' ) ) { $frm_settings = self::get_settings(); return $frm_settings->admin_permission; @@ -2414,7 +2414,7 @@ public static function permission_nonce_error( $permission, $nonce_name = '', $n $error = false; - if ( empty( $nonce_name ) ) { + if ( ! $nonce_name ) { return $error; } @@ -2849,7 +2849,7 @@ public static function setup_edit_vars( $record, $table, $fields = '', $default return false; } - if ( empty( $post_values ) ) { + if ( ! $post_values ) { $post_values = wp_unslash( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } @@ -2888,7 +2888,7 @@ public static function setup_edit_vars( $record, $table, $fields = '', $default * @return void */ private static function prepare_field_arrays( $fields, $record, array &$values, $args ) { - if ( ! empty( $fields ) ) { + if ( $fields ) { foreach ( (array) $fields as $field ) { if ( ! self::is_admin_page() ) { // Don't prep default values on the form settings page. @@ -3058,7 +3058,7 @@ private static function fill_form_defaults( $post_values, array &$values ) { * @return bool|int */ public static function custom_style_value( $post_values ) { - if ( ! empty( $post_values ) && isset( $post_values['options']['custom_style'] ) ) { + if ( $post_values && isset( $post_values['options']['custom_style'] ) ) { return absint( $post_values['options']['custom_style'] ); } @@ -3578,7 +3578,7 @@ public static function select_current_page( $page, $current_page, $action = arra $frm_action = 'reports'; } - if ( empty( $action ) || ( ! empty( $frm_action ) && in_array( $frm_action, $action, true ) ) ) { + if ( ! $action || ( ! empty( $frm_action ) && in_array( $frm_action, $action, true ) ) ) { echo ' class="current_page"'; } } diff --git a/classes/helpers/FrmDashboardHelper.php b/classes/helpers/FrmDashboardHelper.php index e6aa15b11d..a18ff1c3af 100644 --- a/classes/helpers/FrmDashboardHelper.php +++ b/classes/helpers/FrmDashboardHelper.php @@ -185,7 +185,7 @@ public function get_free_templates_banner() { * @return void */ public static function show_connect_links( $buttons = array(), $button_classes = '' ) { - if ( empty( $buttons ) ) { + if ( ! $buttons ) { $buttons = self::get_license_buttons(); } diff --git a/classes/helpers/FrmFieldsHelper.php b/classes/helpers/FrmFieldsHelper.php index eb30390e1d..2a278b9119 100644 --- a/classes/helpers/FrmFieldsHelper.php +++ b/classes/helpers/FrmFieldsHelper.php @@ -2746,7 +2746,7 @@ public static function render_ai_generate_options_button( $args, $should_hide_bu 'class' => self::get_ai_generate_options_button_class(), ); - if ( ! empty( $should_hide_bulk_edit ) ) { + if ( $should_hide_bulk_edit ) { $attributes['class'] .= ' frm-force-hidden'; } diff --git a/classes/helpers/FrmFormsHelper.php b/classes/helpers/FrmFormsHelper.php index 6607b4ca3f..1d6da52594 100644 --- a/classes/helpers/FrmFormsHelper.php +++ b/classes/helpers/FrmFormsHelper.php @@ -95,7 +95,7 @@ public static function forms_dropdown( $field_name, $field_value = '', $args = a * @return void */ public static function add_html_attr( $class, $param, &$add_html ) { - if ( ! empty( $class ) ) { + if ( $class ) { $add_html[ $param ] = sanitize_title( $param ) . '="' . esc_attr( trim( sanitize_text_field( $class ) ) ) . '"'; } } @@ -338,7 +338,7 @@ public static function get_success_message( $atts ) { public static function setup_new_vars( $values = array() ) { global $wpdb; - if ( ! empty( $values ) ) { + if ( $values ) { $post_values = $values; } else { $values = array(); @@ -1114,7 +1114,7 @@ private static function field_has_top_label( $field, $form ) { public static function get_form_style( $form ) { $style = 1; - if ( empty( $form ) || 'default' === $form ) { + if ( ! $form || 'default' === $form ) { return $style; } @@ -1356,7 +1356,7 @@ public static function delete_trash_link( $id, $status, $length = 'label' ) { public static function format_link_html( $link_details, $length = 'label' ) { $link = ''; - if ( ! empty( $link_details ) ) { + if ( $link_details ) { $link = 'id ); @@ -500,7 +500,7 @@ private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$ self::maybe_update_get_values_form_setting( $imported, $f ); self::migrate_placeholders( $f ); - if ( ! empty( $this_form ) ) { + if ( $this_form ) { // check for field to edit by field id if ( isset( $form_fields[ $f['id'] ] ) ) { FrmField::update( $f['id'], $f ); diff --git a/classes/models/FrmCreateFile.php b/classes/models/FrmCreateFile.php index 09305332fd..619eb375dc 100644 --- a/classes/models/FrmCreateFile.php +++ b/classes/models/FrmCreateFile.php @@ -157,7 +157,7 @@ public function get_file_contents() { private function get_contents( $file = '' ) { global $wp_filesystem; - if ( empty( $file ) ) { + if ( ! $file ) { $file = $this->new_file_path; } diff --git a/classes/models/FrmDb.php b/classes/models/FrmDb.php index 799ca121d6..33f7fd9dbe 100644 --- a/classes/models/FrmDb.php +++ b/classes/models/FrmDb.php @@ -423,11 +423,11 @@ private static function convert_options_to_array( &$args, $order_by = '', $limit $args = array( 'order_by' => $args ); } - if ( ! empty( $order_by ) ) { + if ( $order_by ) { $args['order_by'] = $order_by; } - if ( ! empty( $limit ) ) { + if ( $limit ) { $args['limit'] = $limit; } diff --git a/classes/models/FrmEntry.php b/classes/models/FrmEntry.php index 57d954ffb6..bfcd517f75 100644 --- a/classes/models/FrmEntry.php +++ b/classes/models/FrmEntry.php @@ -466,7 +466,7 @@ public static function get_new_entry_name( $values, $default = '' ) { public static function maybe_get_entry( &$entry ) { if ( $entry && is_numeric( $entry ) ) { $entry = self::getOne( $entry ); - } elseif ( empty( $entry ) || 'false' === $entry ) { + } elseif ( ! $entry || 'false' === $entry ) { $entry = false; } } diff --git a/classes/models/FrmEntryValidate.php b/classes/models/FrmEntryValidate.php index 4b1f1d3961..4b0991e6ac 100644 --- a/classes/models/FrmEntryValidate.php +++ b/classes/models/FrmEntryValidate.php @@ -112,7 +112,7 @@ private static function get_fields_to_validate( $values, $exclude ) { $where['fr.parent_form_id'] = array( null, 0 ); // Don't get excluded fields (like file upload fields in the ajax validation) - if ( ! empty( $exclude ) ) { + if ( $exclude ) { $where['fi.type not'] = $exclude; } @@ -523,7 +523,7 @@ private static function create_regular_expression_from_format( $pattern ) { $pattern = ''; foreach ( $parts as $part ) { - if ( empty( $pattern ) ) { + if ( ! $pattern ) { $pattern .= $part; } else { $pattern .= '(' . $part . ')?'; @@ -549,7 +549,7 @@ public static function spam_check( $exclude, $values, &$errors ) { return; } - if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { + if ( $exclude || empty( $values['item_meta'] ) || ! empty( $errors ) ) { // only check spam if there are no other errors return; } diff --git a/classes/models/FrmField.php b/classes/models/FrmField.php index 39d73819ce..9d08d04b70 100644 --- a/classes/models/FrmField.php +++ b/classes/models/FrmField.php @@ -921,7 +921,7 @@ public static function get_all_types_in_form( $form_id, $type, $limit = '', $inc break; } - if ( ! empty( $limit ) && $count >= $limit ) { + if ( $limit && $count >= $limit ) { break; } @@ -972,7 +972,7 @@ public static function get_all_for_form( $form_id, $limit = '', $inc_embed = 'ex ++$count; $fields[ $result->id ] = $result; - if ( ! empty( $limit ) && $count >= $limit ) { + if ( $limit && $count >= $limit ) { break; } } @@ -1093,7 +1093,7 @@ public static function getAll( $where = array(), $order_by = '', $limit = '', $b $form_table_name = $wpdb->prefix . 'frm_forms'; } - if ( ! empty( $order_by ) && ! str_contains( $order_by, 'ORDER BY' ) ) { + if ( $order_by && ! str_contains( $order_by, 'ORDER BY' ) ) { $order_by = ' ORDER BY ' . $order_by; } diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index 5ea9f08a66..264dffa252 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -253,7 +253,7 @@ protected function skip_categories() { * @return array */ public function get_addon_for_license( $license_plugin, $addons = array() ) { - if ( empty( $addons ) ) { + if ( ! $addons ) { $addons = $this->get_api_info(); } @@ -409,7 +409,7 @@ public function error_for_license() { * @return array */ public function get_error_from_response( $addons = array() ) { - if ( empty( $addons ) ) { + if ( ! $addons ) { $addons = $this->get_api_info(); } diff --git a/classes/models/FrmInstallerSkin.php b/classes/models/FrmInstallerSkin.php index 4f0340492d..8118bef26e 100644 --- a/classes/models/FrmInstallerSkin.php +++ b/classes/models/FrmInstallerSkin.php @@ -70,7 +70,7 @@ public function footer() {} * @param string|\WP_Error $errors The WP Error object of errors with the install process. */ public function error( $errors ) { - if ( ! empty( $errors ) ) { + if ( $errors ) { if ( ! is_string( $errors ) ) { $error = $errors->get_error_message(); $message = $errors->get_error_data(); diff --git a/classes/models/FrmSolution.php b/classes/models/FrmSolution.php index 170f451d45..7540dfea25 100644 --- a/classes/models/FrmSolution.php +++ b/classes/models/FrmSolution.php @@ -654,7 +654,7 @@ protected function show_view_options() { * @return void */ protected function show_import_options( $options, $importing, $xml = '' ) { - if ( empty( $options ) ) { + if ( ! $options ) { return; } diff --git a/classes/models/fields/FrmFieldDefault.php b/classes/models/fields/FrmFieldDefault.php index 50fceecd86..42cd78fcaa 100644 --- a/classes/models/fields/FrmFieldDefault.php +++ b/classes/models/fields/FrmFieldDefault.php @@ -21,7 +21,7 @@ class FrmFieldDefault extends FrmFieldType { * @return void */ protected function set_type( $type ) { - if ( empty( $type ) ) { + if ( ! $type ) { $type = 'text'; } parent::set_type( $type ); diff --git a/classes/models/fields/FrmFieldType.php b/classes/models/fields/FrmFieldType.php index 90bb3a5ead..f24e6412d3 100644 --- a/classes/models/fields/FrmFieldType.php +++ b/classes/models/fields/FrmFieldType.php @@ -958,7 +958,7 @@ public function prepare_field_value( $value, $atts ) { * @return array */ public function get_options( $values ) { - if ( empty( $values ) ) { + if ( ! $values ) { $values = (array) $this->field; } diff --git a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php new file mode 100644 index 0000000000..7d9b4c3f04 --- /dev/null +++ b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php @@ -0,0 +1,273 @@ +getTokens(); + + // Only process if empty() is directly inside an if/elseif condition. + if ( ! $this->isInIfCondition( $phpcsFile, $stackPtr ) ) { + return; + } + + // Find the opening parenthesis after empty. + $openParen = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); + + if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS ) { + return; + } + + // Find the variable inside empty(). + $variablePtr = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, null, true ); + + if ( false === $variablePtr || $tokens[ $variablePtr ]['code'] !== T_VARIABLE ) { + return; + } + + $variableName = $tokens[ $variablePtr ]['content']; + + // Check if there's anything else inside the empty() call (like array access). + $closeParen = $tokens[ $openParen ]['parenthesis_closer']; + $nextToken = $phpcsFile->findNext( T_WHITESPACE, $variablePtr + 1, $closeParen, true ); + + if ( false !== $nextToken ) { + // There's something after the variable (like array access $var['key']). + // Skip this case as it's more complex. + return; + } + + // Find the containing function. + $functionToken = $this->findContainingFunction( $phpcsFile, $stackPtr ); + + if ( false === $functionToken ) { + return; + } + + // Get the function parameters. + $parameters = $this->getFunctionParameters( $phpcsFile, $functionToken ); + + // Check if the variable is a function parameter. + if ( ! in_array( $variableName, $parameters, true ) ) { + return; + } + + // Check if there's a boolean NOT before empty. + $prevToken = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true ); + $isNegated = ( false !== $prevToken && $tokens[ $prevToken ]['code'] === T_BOOLEAN_NOT ); + + // Determine the suggested replacement. + if ( $isNegated ) { + $suggestion = $variableName; + $message = 'Redundant empty() on function parameter %s. Use "%s" instead of "! empty( %s )"'; + } else { + $suggestion = '! ' . $variableName; + $message = 'Redundant empty() on function parameter %s. Use "%s" instead of "empty( %s )"'; + } + + $fix = $phpcsFile->addFixableError( + $message, + $stackPtr, + 'Found', + array( $variableName, $suggestion, $variableName ) + ); + + if ( true === $fix ) { + $phpcsFile->fixer->beginChangeset(); + + if ( $isNegated ) { + // Remove the "!" before empty. + $phpcsFile->fixer->replaceToken( $prevToken, '' ); + + // Remove any whitespace between "!" and "empty". + for ( $i = $prevToken + 1; $i < $stackPtr; $i++ ) { + if ( $tokens[ $i ]['code'] === T_WHITESPACE ) { + $phpcsFile->fixer->replaceToken( $i, '' ); + } + } + } + + // Replace empty( $var ) with $var or ! $var. + $phpcsFile->fixer->replaceToken( $stackPtr, '' ); + + // Remove whitespace after empty. + for ( $i = $stackPtr + 1; $i < $openParen; $i++ ) { + if ( $tokens[ $i ]['code'] === T_WHITESPACE ) { + $phpcsFile->fixer->replaceToken( $i, '' ); + } + } + + // Replace opening paren. + $phpcsFile->fixer->replaceToken( $openParen, '' ); + + // Remove whitespace after opening paren. + $nextAfterOpen = $openParen + 1; + while ( $nextAfterOpen < $variablePtr && $tokens[ $nextAfterOpen ]['code'] === T_WHITESPACE ) { + $phpcsFile->fixer->replaceToken( $nextAfterOpen, '' ); + ++$nextAfterOpen; + } + + // Keep the variable, but add "! " prefix if not negated. + if ( ! $isNegated ) { + $phpcsFile->fixer->addContentBefore( $variablePtr, '! ' ); + } + + // Remove whitespace before closing paren. + $prevBeforeClose = $closeParen - 1; + while ( $prevBeforeClose > $variablePtr && $tokens[ $prevBeforeClose ]['code'] === T_WHITESPACE ) { + $phpcsFile->fixer->replaceToken( $prevBeforeClose, '' ); + --$prevBeforeClose; + } + + // Replace closing paren. + $phpcsFile->fixer->replaceToken( $closeParen, '' ); + + $phpcsFile->fixer->endChangeset(); + } + } + + /** + * Find the function or method that contains the given token. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token. + * + * @return int|false The position of the function token, or false if not found. + */ + private function findContainingFunction( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + for ( $i = $stackPtr - 1; $i >= 0; $i-- ) { + if ( $tokens[ $i ]['code'] === T_FUNCTION || $tokens[ $i ]['code'] === T_CLOSURE ) { + // Check if the stackPtr is within this function's scope. + if ( isset( $tokens[ $i ]['scope_opener'], $tokens[ $i ]['scope_closer'] ) ) { + if ( $stackPtr > $tokens[ $i ]['scope_opener'] && $stackPtr < $tokens[ $i ]['scope_closer'] ) { + return $i; + } + } + } + } + + return false; + } + + /** + * Get the parameter names for a function. + * + * @param File $phpcsFile The file being scanned. + * @param int $functionToken The position of the function token. + * + * @return array List of parameter variable names (e.g., ['$param1', '$param2']). + */ + private function getFunctionParameters( File $phpcsFile, $functionToken ) { + $tokens = $phpcsFile->getTokens(); + $parameters = array(); + + if ( ! isset( $tokens[ $functionToken ]['parenthesis_opener'], $tokens[ $functionToken ]['parenthesis_closer'] ) ) { + return $parameters; + } + + $openParen = $tokens[ $functionToken ]['parenthesis_opener']; + $closeParen = $tokens[ $functionToken ]['parenthesis_closer']; + + for ( $i = $openParen + 1; $i < $closeParen; $i++ ) { + if ( $tokens[ $i ]['code'] === T_VARIABLE ) { + $parameters[] = $tokens[ $i ]['content']; + } + } + + return $parameters; + } + + /** + * Check if the empty() call is directly inside an if/elseif condition. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the empty token. + * + * @return bool True if inside an if/elseif condition, false otherwise. + */ + private function isInIfCondition( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + // Find the opening parenthesis that contains this empty() call. + // We need to find the parenthesis that belongs to an if/elseif statement. + for ( $i = $stackPtr - 1; $i >= 0; $i-- ) { + $code = $tokens[ $i ]['code']; + + // Skip whitespace and the "!" operator. + if ( $code === T_WHITESPACE || $code === T_BOOLEAN_NOT ) { + continue; + } + + // If we hit an open parenthesis, check if it belongs to if/elseif. + if ( $code === T_OPEN_PARENTHESIS ) { + // Check what's before this parenthesis. + $beforeParen = $phpcsFile->findPrevious( T_WHITESPACE, $i - 1, null, true ); + + if ( false !== $beforeParen ) { + $beforeCode = $tokens[ $beforeParen ]['code']; + + if ( $beforeCode === T_IF || $beforeCode === T_ELSEIF ) { + return true; + } + } + + // This parenthesis doesn't belong to if/elseif. + return false; + } + + // If we hit something else (like another function call), stop. + if ( $code !== T_OPEN_PARENTHESIS ) { + return false; + } + } + + return false; + } +} diff --git a/phpcs.xml b/phpcs.xml index a728aa8208..ded8c3252c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -282,4 +282,5 @@ + diff --git a/stripe/helpers/FrmTransLiteAppHelper.php b/stripe/helpers/FrmTransLiteAppHelper.php index a828018d3e..c10fc6c254 100755 --- a/stripe/helpers/FrmTransLiteAppHelper.php +++ b/stripe/helpers/FrmTransLiteAppHelper.php @@ -364,7 +364,7 @@ public static function get_date_format() { * @return string */ public static function format_the_date( $date, $format = '' ) { - if ( empty( $format ) ) { + if ( ! $format ) { $format = self::get_date_format(); } return date_i18n( $format, strtotime( $date ) ); @@ -408,7 +408,7 @@ public static function get_user_link( $user_id ) { * @return void */ public static function show_in_table( $value, $label ) { - if ( ! empty( $value ) ) { ?> + if ( $value ) { ?> : diff --git a/stripe/models/FrmStrpLiteAuth.php b/stripe/models/FrmStrpLiteAuth.php index b778ab3d68..2611259674 100644 --- a/stripe/models/FrmStrpLiteAuth.php +++ b/stripe/models/FrmStrpLiteAuth.php @@ -696,7 +696,7 @@ private static function create_setup_intent( $payment_method_types ) { * @return void */ private static function add_amount_to_actions( $form_id, &$actions ) { - if ( empty( $actions ) ) { + if ( ! $actions ) { return; } diff --git a/tests/phpunit/emails/test_FrmEmail.php b/tests/phpunit/emails/test_FrmEmail.php index 61424c6d96..f6f6a2691e 100644 --- a/tests/phpunit/emails/test_FrmEmail.php +++ b/tests/phpunit/emails/test_FrmEmail.php @@ -708,7 +708,7 @@ public function test_plain_text_message() { } private function check_private_properties( $settings, $setting_name, $property = '' ) { - if ( empty( $property ) ) { + if ( ! $property ) { $property = $setting_name; } From b82f495273a552919ba6860fba5883dae90934c2 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 9 Jan 2026 10:06:17 -0400 Subject: [PATCH 2/2] Run php cs fixer on sniff --- .../Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php index 7d9b4c3f04..e86d1f153a 100644 --- a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RedundantEmptyOnParameterSniff.php @@ -147,6 +147,7 @@ public function process( File $phpcsFile, $stackPtr ) { // Remove whitespace after opening paren. $nextAfterOpen = $openParen + 1; + while ( $nextAfterOpen < $variablePtr && $tokens[ $nextAfterOpen ]['code'] === T_WHITESPACE ) { $phpcsFile->fixer->replaceToken( $nextAfterOpen, '' ); ++$nextAfterOpen; @@ -159,6 +160,7 @@ public function process( File $phpcsFile, $stackPtr ) { // Remove whitespace before closing paren. $prevBeforeClose = $closeParen - 1; + while ( $prevBeforeClose > $variablePtr && $tokens[ $prevBeforeClose ]['code'] === T_WHITESPACE ) { $phpcsFile->fixer->replaceToken( $prevBeforeClose, '' ); --$prevBeforeClose; @@ -177,7 +179,7 @@ public function process( File $phpcsFile, $stackPtr ) { * @param File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token. * - * @return int|false The position of the function token, or false if not found. + * @return false|int The position of the function token, or false if not found. */ private function findContainingFunction( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens();