-
Notifications
You must be signed in to change notification settings - Fork 108
SONARPHP-1721 S1155 An issue should not be raised even if empty() is used. #1600
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
Conversation
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Show resolved
Hide resolved
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well 🙂.
I think the code can be improved in parts, however, I did not manage to do a proper review yet. Feel free to take a look at what I wrote, and I will do a proper one next week.
php-checks/src/test/resources/checks/CountInsteadOfEmptyCheck.php
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The review took a while. Never worked on sonar-php before and, hence, I had to understand a couple of things first. 😉
The implementation works! 👍
The tests appear to be sufficient maybe even complete 👍, although I still had no closer look 😅.
However, I think we must improve the implementation such that it leverages the Visitor pattern. Currently, we are going back and forth in the implementation, which makes it a little hard to understand and introduces some overhead and uncertainty.
In addition to the detailed comments below, I suggest to take inspiration from this alternative implementation - again 😅:
private boolean isEmptyUsedInCondition(FunctionCallTree countCallTree, @Nullable ExpressionTree countArgument) {
if (!(countArgument instanceof VariableIdentifierTreeImpl countArgumentVariable)) {
return false;
}
Tree condition = countCallTree.getParent();
while (condition != null) {
if (...) {
break;
}
condition = condition.getParent();
}
EmptyMethodCallCheck emptyCheck = new EmptyMethodCallCheck(context(), countArgumentVariable.symbol());
condition.accept(emptyCheck);
return emptyCheck.containsEmpty();
}
...
private static class EmptyMethodCallCheck extends PHPVisitorCheck {
private static final FunctionCall EMPTY_FUNCTION_CALL = new FunctionCall("empty");
private boolean emptyFound = false;
CheckContext parentContext;
Symbol countArgumentSymbol;
EmptyMethodCallCheck(CheckContext parentContext, Symbol countArgumentSymbol) {
this.parentContext = parentContext;
this.countArgumentSymbol = countArgumentSymbol;
}
@Override
public void visitFunctionCall(FunctionCallTree tree) {
if (!emptyFound && isEmptyFunction(tree) && !tree.callArguments().isEmpty()) {
ExpressionTree firstArgument = tree.callArguments().get(0).value();
if (firstArgument.is(Tree.Kind.VARIABLE_IDENTIFIER)
&& firstArgument instanceof VariableIdentifierTreeImpl firstArgumentVariable
&& firstArgumentVariable.symbol() == countArgumentSymbol) {
emptyFound = true;
}
}
super.visitFunctionCall(tree);
}
private boolean isEmptyFunction(FunctionCallTree tree) {
return EMPTY_FUNCTION_CALL.test(TreeValues.of(tree, parentContext.symbolTable()));
}
public boolean containsEmpty() {
return emptyFound;
}
}
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are getting there 😉.
Please double-check previous comments as well 🙂.
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take another look on all unresolved conversations in the PR.
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let us do a sync 😉
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 👍
Two comments left which are super easy to fix 😊.
Still not approving as the parent skipping (see comment below) might be critical 😅.
Will immediately approve once fixed 😉.
If you want to increase coverage (there are two uncovered lines), I think you can only do so with some mock-tests. Feel free to add them, but I think they are not needed as both lines will never be reached in a real scenario. Hence, coverage is fine as it is. 👍
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
php-checks/src/main/java/org/sonar/php/checks/CountInsteadOfEmptyCheck.java
Outdated
Show resolved
Hide resolved
|
felix-pauck-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🎉




No description provided.