Skip to content

Commit

Permalink
[#1831] EmptyClassInspector: resolved false positives (enums)
Browse files Browse the repository at this point in the history
  • Loading branch information
ea-inspections-team committed Sep 18, 2024
1 parent a665c27 commit 83ae086
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/

public class EmptyClassInspector extends BasePhpInspection {
private static final String message = "Class does not contain any properties or methods.";
private static final String messageClass = "Class does not contain any properties or methods.";
private static final String messageEnum = "Enum does not contain any values or methods.";

@NotNull
@Override
Expand All @@ -42,8 +43,11 @@ public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, bool
return new BasePhpElementVisitor() {
@Override
public void visitPhpClass(@NotNull PhpClass clazz) {
if (!clazz.isInterface() && !clazz.isDeprecated() && !clazz.isAnonymous()) {
final boolean isEmpty = clazz.getOwnFields().length == 0 && clazz.getOwnMethods().length == 0 && clazz.getTraits().length == 0;
if (! clazz.isInterface() && ! clazz.isDeprecated() && ! clazz.isAnonymous()) {
final boolean isEmpty = clazz.getOwnFields().length == 0 &&
clazz.getOwnMethods().length == 0 &&
clazz.getTraits().length == 0 &&
clazz.getEnumCases().isEmpty();
if (isEmpty) {
final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
if (parent != null) {
Expand All @@ -57,7 +61,7 @@ public void visitPhpClass(@NotNull PhpClass clazz) {
if (nameNode != null) {
holder.registerProblem(
nameNode,
MessagesPresentationUtil.prefixWithEa(message)
MessagesPresentationUtil.prefixWithEa(clazz.isEnum() ? messageEnum : messageClass )
);
}
}
Expand Down
1 change: 1 addition & 0 deletions testData/fixtures/classes/empty-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ClassWithAProperty { public $property; }
class ClassWithAMethod { public function __construct() {} }

class <weak_warning descr="[EA] Class does not contain any properties or methods.">AnEmptyClass</weak_warning> {}
enum <weak_warning descr="[EA] Enum does not contain any values or methods.">AnEmptyEnum</weak_warning> {}

/* false-positives: empty interface */
interface EmptyInterface {}
Expand Down

0 comments on commit 83ae086

Please sign in to comment.