Skip to content

Refactor FXIOS-7301 - Remove 2 closure_body_length violations from MainMenuMiddleware.swift (Firefox) and InternalTelemetrySettingsView.swift (Focus) #26242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ionixjunior
Copy link
Contributor

@ionixjunior ionixjunior commented Apr 22, 2025

📜 Tickets

Jira ticket
Github issue

💡 Description

This PR removes 2 closure_body_length violations from the MainMenuMiddleware.swift (Firefox) and InternalTelemetrySettingsView.swift (Focus). Also, it decreases the threshold to 42.

The most controversial change in this PR is in the MainMenuMiddleware.swift file. In this file, I changed the mainMenuProvider property, splitting it into separated functions. I created three new functions, each one based on the ActionType: MainMenuActionType, GeneralBrowserActionType, and MainMenuDetailsActionType. To the last one, I needed to add the @unknown keyword, because all option already available into switch case statement.

I've changed the strategy, so I just discard the paragraph above, and I made a comment bellow explaining this.

Please, feel free to give me suggestions about how can we improve this.

This PR is part of a series of PRs described here #16442 (comment) and here #16442 (comment).

📝 Checklist

You have to check all boxes before merging

  • Filled in the above information (tickets numbers and description of your work)
  • Updated the PR name to follow our PR naming guidelines
  • Wrote unit tests and/or ensured the tests suite is passing
  • When working on UI, I checked and implemented accessibility (minimum Dynamic Text and VoiceOver)
  • If needed, I updated documentation / comments for complex code and public methods
  • If needed, added a backport comment (example @Mergifyio backport release/v120)

@ionixjunior ionixjunior requested a review from a team as a code owner April 22, 2025 22:23
@ionixjunior ionixjunior requested a review from lmarceau April 22, 2025 22:23
Copy link
Collaborator

@FilippoZazzeroni FilippoZazzeroni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit refactor for MainMenuMiddleware the rest looks good to me

switch action.actionType {
case MainMenuActionType.tapNavigateToDestination:
self.handleTapNavigateToDestinationAction(action: action, isHomepage: isHomepage)
if self.handleMainMenuActionType(action: action, isHomepage: isHomepage) { return }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we change this to instead of checking if the method handles the action, to check for the action type, like we did in other cases.

if let action = action as? GeneralBrowserAction {
 handleGeneralBrowserAction()
}

With this we can avoid also returning a boolean to check if the action was handled. What do you think @ionixjunior let me know if it makes sense what i said, thanks 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I totally agree. Thank you for point this! I've made some small changes. I added to the methods the action type parameter to avoid another type casting inside each function. Let me know what do you think about it.

The branch was rebased and updated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ionixjunior i like it, but i'd it has a slight problem that ActionType is not linked with Action so i'd do with other files like the TabManagerMiddleware where we check the Action directly not the action type. Also i'd prefer to do

if let acti = ...

else if let {
}

if else statements instead of only if since we don't want multiple action to accidentally be resolved from closure.

@lmarceau lmarceau removed their request for review April 24, 2025 13:29
@FilippoZazzeroni FilippoZazzeroni self-requested a review April 25, 2025 13:23
@ionixjunior ionixjunior force-pushed the issue_16442_part_XLIX branch from a70afe7 to 25c60a7 Compare April 28, 2025 22:35

default: break
@unknown default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need @unknow at this point ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I can remove it, but also I need to remove the default statement, because the switch statement defines all the enum possibilities, and I cannot have some code that will never be executed. The default case statement will never be reached, so the compiler will generate an error if it is kept in the code.

What's the best practice here? Keep the default statement with the @unknown keyword or just remove the entire default statement?

switch action.actionType {
case MainMenuActionType.tapNavigateToDestination:
self.handleTapNavigateToDestinationAction(action: action, isHomepage: isHomepage)
if self.handleMainMenuActionType(action: action, isHomepage: isHomepage) { return }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ionixjunior i like it, but i'd it has a slight problem that ActionType is not linked with Action so i'd do with other files like the TabManagerMiddleware where we check the Action directly not the action type. Also i'd prefer to do

if let acti = ...

else if let {
}

if else statements instead of only if since we don't want multiple action to accidentally be resolved from closure.

@ionixjunior
Copy link
Contributor Author

@FilippoZazzeroni I understand your point about the slight problem that ActionType is not linked with Action, and I liked the example of TabManagerMiddleware, but I believe we need to think more about in the MainMenuMiddleware class.

To find the correct type and call the new small functions, it will be necessary to look at the ActionType, because on mainMenuProvider property, the action variable is from type MainMenuAction. See the guard let at the line 54. So, if we check only the type of action, we can't understand what small function to call. Is this makes sense?

Maybe my approach was not so good, trying to split this function based on the action type. I'll think how can we deal with this, but let me know what do you think.

About the else if statement, it makes sense to me.

@FilippoZazzeroni
Copy link
Collaborator

Sorry for answering late @ionixjunior i was out of office yesterday. Anyway yes i agree, if we know already that we are going to have one action and different action type, then we can check for the actionType directly.

@ionixjunior
Copy link
Contributor Author

No problem at all @FilippoZazzeroni. This week I'll work on it again. Sorry for answering late too.

@FilippoZazzeroni
Copy link
Collaborator

no problem @ionixjunior there is no rush

@ionixjunior ionixjunior force-pushed the issue_16442_part_XLIX branch from 25c60a7 to 28d2b1c Compare May 12, 2025 22:48
@ionixjunior
Copy link
Contributor Author

@FilippoZazzeroni I had an idea. I just extract the entire closure block to a new function and it solved the problem. I was afraid about another rule, the function body length, but I looked at the documentation, and I verifyed the default value for it is 50 https://realm.github.io/SwiftLint/function_body_length.html. So, the new function I've created has 41 lines and everthing will be okay with this another rule too.

Let me know what do you think about it. The branch was rebased and updated.

Copy link
Collaborator

@FilippoZazzeroni FilippoZazzeroni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @ionixjunior 🎉 🎉 just a reminder that if we are going to apply the same procedure for function_body_length than we will need to refactor also this method.

@mobiletest-ci-bot
Copy link

Messages
📖 Project coverage: 36.4%
📖 Edited 3 files
📖 Created 0 files

Client.app: Coverage: 35.06

File Coverage
MainMenuMiddleware.swift 32.1% ⚠️

Generated by 🚫 Danger Swift against 28d2b1c

@ionixjunior
Copy link
Contributor Author

@FilippoZazzeroni yes, absolutely.

@FilippoZazzeroni FilippoZazzeroni merged commit 430d352 into mozilla-mobile:main May 14, 2025
12 checks passed
@ionixjunior ionixjunior deleted the issue_16442_part_XLIX branch May 15, 2025 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants