Skip to content

Commit

Permalink
SONARPHP-769 Check if method has no modifier (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-werner-sonarsource authored May 26, 2020
1 parent bff2d63 commit 7817709
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.sonar.check.Rule;
import org.sonar.php.tree.TreeUtils;
import org.sonar.php.tree.impl.PHPTree;
import org.sonar.plugins.php.api.tree.Tree.Kind;
import org.sonar.plugins.php.api.tree.declaration.ClassDeclarationTree;
import org.sonar.plugins.php.api.tree.declaration.FunctionDeclarationTree;
Expand All @@ -47,7 +48,7 @@ public class EmptyMethodCheck extends PHPVisitorCheck {

@Override
public void visitMethodDeclaration(MethodDeclarationTree tree) {
if (tree.body().is(Kind.BLOCK) && !(hasValuableBody((BlockTree) tree.body()) || isClassAbstract(tree) || hasCommentAbove(tree.modifiers().get(0)))) {
if (tree.body().is(Kind.BLOCK) && !(hasValuableBody((BlockTree) tree.body()) || isClassAbstract(tree) || hasCommentAbove(((PHPTree) tree).getFirstToken()))) {
commitIssue(tree, "method");
}

Expand All @@ -56,7 +57,7 @@ public void visitMethodDeclaration(MethodDeclarationTree tree) {

@Override
public void visitFunctionDeclaration(FunctionDeclarationTree tree) {
if (!(hasValuableBody(tree.body()) || hasCommentAbove(tree.functionToken()))) {
if (!(hasValuableBody(tree.body()) || hasCommentAbove(((PHPTree) tree).getFirstToken()))) {
commitIssue(tree, "function");
}

Expand Down
3 changes: 3 additions & 0 deletions php-checks/src/test/resources/checks/EmptyMethodCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function method10() {

// Comment
public function method11() {} // Compliant

// Comment
function method11() {} // Compliant
}

abstract class AbstractClass {
Expand Down

0 comments on commit 7817709

Please sign in to comment.