diff --git a/classes/helpers/FrmEntriesListHelper.php b/classes/helpers/FrmEntriesListHelper.php index dace38fe73..6dd7ebc268 100644 --- a/classes/helpers/FrmEntriesListHelper.php +++ b/classes/helpers/FrmEntriesListHelper.php @@ -278,18 +278,20 @@ protected function display_tablenav( $which ) { protected function extra_tablenav( $which ) { $form_id = FrmAppHelper::simple_get( 'form', 'absint' ); - if ( $which === 'top' && ! $form_id ) { - echo '
'; + if ( $which !== 'top' || $form_id ) { + return; + } - // Override the referrer to prevent it from being used for the screen options. - echo ''; + echo '
'; - echo ''; + // Override the referrer to prevent it from being used for the screen options. + echo ''; - FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) ); - submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) ); - echo '
'; - } + echo ''; + + FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) ); + submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) ); + echo '
'; } /** diff --git a/classes/helpers/FrmXMLHelper.php b/classes/helpers/FrmXMLHelper.php index b3b6922cc2..330d183064 100644 --- a/classes/helpers/FrmXMLHelper.php +++ b/classes/helpers/FrmXMLHelper.php @@ -961,16 +961,18 @@ private static function update_custom_style_setting_on_import( &$form ) { private static function update_custom_style_setting_after_import( $form_id ) { $form = FrmForm::getOne( $form_id ); - if ( $form && isset( $form->options['old_style'] ) ) { - $form = (array) $form; - $saved_style = $form['options']['custom_style']; - $form['options']['custom_style'] = $form['options']['old_style']; - self::update_custom_style_setting_on_import( $form ); - $has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong + if ( ! $form || ! isset( $form->options['old_style'] ) ) { + return; + } - if ( $has_changed ) { - FrmForm::update( $form['id'], $form ); - } + $form = (array) $form; + $saved_style = $form['options']['custom_style']; + $form['options']['custom_style'] = $form['options']['old_style']; + self::update_custom_style_setting_on_import( $form ); + $has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong + + if ( $has_changed ) { + FrmForm::update( $form['id'], $form ); } } diff --git a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php index 37ec8f41e3..6fa5f4e851 100644 --- a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/FlipIfToEarlyReturnSniff.php @@ -24,6 +24,15 @@ * } * } * + * Also catches: + * function example($value) { + * $var = get_value(); + * if ($var && $condition) { + * // 5+ lines of code + * return $result; + * } + * } + * * Good: * function example($value) { * if (! $value) { @@ -89,10 +98,16 @@ public function process( File $phpcsFile, $stackPtr ) { // Check if the first statement is an if. if ( $tokens[ $firstStatement ]['code'] !== T_IF ) { - return; - } + // If the first statement isn't an if, check if it's a simple assignment + // followed by an if statement (pattern: $var = value; if ($var && condition) {...}) + $ifToken = $this->findIfAfterSimpleSetup( $phpcsFile, $firstStatement, $scopeCloser ); - $ifToken = $firstStatement; + if ( false === $ifToken ) { + return; + } + } else { + $ifToken = $firstStatement; + } // Check if this if has an else or elseif - skip those. if ( isset( $tokens[ $ifToken ]['scope_closer'] ) ) { @@ -110,7 +125,7 @@ public function process( File $phpcsFile, $stackPtr ) { $ifScopeCloser = $tokens[ $ifToken ]['scope_closer']; - // Check if the if statement is the only thing in the function body. + // Check if the if statement is the main logic in the function body. // The next non-whitespace/non-comment token after the if's closing brace should be the function's closing brace. $afterIf = $phpcsFile->findNext( array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_DOC_COMMENT_OPEN_TAG, T_DOC_COMMENT_CLOSE_TAG, T_DOC_COMMENT_STAR, T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG, T_DOC_COMMENT_WHITESPACE ), @@ -538,4 +553,81 @@ private function getConditionString( File $phpcsFile, $ifToken ) { return $condition; } + + /** + * Find an if statement that comes after simple setup code. + * + * @param File $phpcsFile The file being scanned. + * @param int $firstToken The first token in the function. + * @param int $scopeCloser The function's scope closer. + * + * @return false|int Position of the if token, or false if not found. + */ + private function findIfAfterSimpleSetup( File $phpcsFile, $firstToken, $scopeCloser ) { + $tokens = $phpcsFile->getTokens(); + + // Allow simple assignments: $var = value; + $current = $firstToken; + $setupStatements = 0; + $maxSetupStatements = 3; // Allow up to 3 simple setup statements + + while ( $current < $scopeCloser && $setupStatements < $maxSetupStatements ) { + // Skip whitespace + if ( $tokens[ $current ]['code'] === T_WHITESPACE ) { + $current++; + continue; + } + + // Check if this is a simple assignment + if ( $this->isSimpleAssignment( $phpcsFile, $current ) ) { + $setupStatements++; + // Move to the end of this statement (semicolon) + $semicolon = $phpcsFile->findNext( T_SEMICOLON, $current + 1, $scopeCloser ); + + if ( false === $semicolon ) { + return false; + } + $current = $semicolon + 1; + continue; + } + + // If we find an if after the setup, return it + if ( $tokens[ $current ]['code'] === T_IF ) { + return $current; + } + + // Anything else means this pattern doesn't match + return false; + } + + return false; + } + + /** + * Check if a token represents a simple assignment statement. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the token to check. + * + * @return bool True if this is a simple assignment. + */ + private function isSimpleAssignment( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + // Look for pattern: $variable = value; + if ( $tokens[ $stackPtr ]['code'] !== T_VARIABLE ) { + return false; + } + + // Next non-whitespace should be = + $next = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); + + if ( false === $next || $tokens[ $next ]['code'] !== T_EQUAL ) { + return false; + } + + // Find the semicolon to confirm this is a complete assignment + $semicolon = $phpcsFile->findNext( T_SEMICOLON, $next + 1, null ); + return false !== $semicolon; + } }