From 83ae086b0044de31a8eefaad04ed5a0bd0ea3d57 Mon Sep 17 00:00:00 2001 From: EA Inspections Team Date: Wed, 18 Sep 2024 20:44:45 +0200 Subject: [PATCH] [#1831] EmptyClassInspector: resolved false positives (enums) --- .../classes/EmptyClassInspector.java | 12 ++++++++---- testData/fixtures/classes/empty-class.php | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/semanticalAnalysis/classes/EmptyClassInspector.java b/src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/semanticalAnalysis/classes/EmptyClassInspector.java index 33d197b343..74cf698af6 100644 --- a/src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/semanticalAnalysis/classes/EmptyClassInspector.java +++ b/src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/semanticalAnalysis/classes/EmptyClassInspector.java @@ -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 @@ -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) { @@ -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 ) ); } } diff --git a/testData/fixtures/classes/empty-class.php b/testData/fixtures/classes/empty-class.php index e885b5b557..b5b5bf99cd 100644 --- a/testData/fixtures/classes/empty-class.php +++ b/testData/fixtures/classes/empty-class.php @@ -4,6 +4,7 @@ class ClassWithAProperty { public $property; } class ClassWithAMethod { public function __construct() {} } class AnEmptyClass {} +enum AnEmptyEnum {} /* false-positives: empty interface */ interface EmptyInterface {}