Skip to content
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

SONARPHP-683 Rule: Cognitive Complexity of functions should not be too high #173

Merged
merged 5 commits into from
Mar 6, 2017

Conversation

vilchik-elena
Copy link
Contributor

No description provided.

public void visitCatchBlock(CatchBlockTree tree) {
complexity.addComplexityWithNesting(tree.catchToken());

complexity.incNesting();
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a perfect case for FP, so I propose to remove all this duplication with a little wrapper in CognitiveComplexity :

public void nestAndDo(Runnable action) { level++; action.run(); level--;}

Which will let you replace all these things with calls like complexity.nestAndDo(() -> super.visitCatchBlock(tree));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@inverno done in b1b9ee8

}

@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest to first flatten the BinaryExpressionTree in a list of operators that reproduces the original syntax : [AND,OR,AND] and only after we have that list we calculate the complexity on it by scanning from left to right.
The algorithm to flatten could be something like :
list = new_empty_list
i = 0
flatten(i, list, tree)

function flatten(i, list, tree)
list.add(i,tree)
flatten(i, list, tree.left)
flatten(++i, list, tree.right)
end_function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@inverno done in 81affa1

@SonarTech
Copy link

SonarQube analysis reported 1 issue

  • MAJOR 1 major

Watch the comments in this conversation to review them.

statements.forEach(statementTree -> statementTree.accept(this)));
}

private void visitWithNesting(Runnable action) {

Choose a reason for hiding this comment

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

MAJOR Remove this unused private "visitWithNesting" method. rule

@inverno inverno merged commit d0ea039 into master Mar 6, 2017
@pynicolas pynicolas deleted the SONARPHP-683 branch October 26, 2017 11:30
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