This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 24c4275 [NETBEANS-5052] Mark unused private constants new e9bd4c3 Merge pull request #2555 from KacerCZ/netbeans-5052-unused-private-const 24c4275 is described below commit 24c42759a8b79625c3aafb009578c4e1537238a7 Author: Tomas Prochazka <ka...@razdva.cz> AuthorDate: Sun Nov 22 17:06:08 2020 +0100 [NETBEANS-5052] Mark unused private constants https://issues.apache.org/jira/browse/NETBEANS-5052 Marks unused private constants in PHP Editor in same way as unused private fields and methods. --- .../modules/php/editor/csl/SemanticAnalysis.java | 31 +++++++++++++++++++--- .../data/testfiles/semantic/anonymousClass01.php | 3 +++ .../semantic/anonymousClass01.php.semantic | 3 +++ .../test/unit/data/testfiles/semantic/class005.php | 5 ++++ .../data/testfiles/semantic/class005.php.semantic | 5 ++++ .../data/testfiles/semantic/unusedPrivateConst.php | 13 +++++++++ .../semantic/unusedPrivateConst.php.semantic | 13 +++++++++ .../php/editor/csl/SemanticAnalyzerTest.java | 4 +++ 8 files changed, 73 insertions(+), 4 deletions(-) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/csl/SemanticAnalysis.java b/php/php.editor/src/org/netbeans/modules/php/editor/csl/SemanticAnalysis.java index 8dfbf64..0af6a37 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/csl/SemanticAnalysis.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/csl/SemanticAnalysis.java @@ -213,6 +213,8 @@ public class SemanticAnalysis extends SemanticAnalyzer { } Map<OffsetRange, Set<ColoringAttributes>> highlights; + // for unsed private constant: name, identifier + private final Map<UnusedIdentifier, ASTNodeColoring> privateUnusedConstants; // for unused private fields: name, varible // if isused, then it's deleted from the list and marked as the field private final Map<UnusedIdentifier, ASTNodeColoring> privateFieldsUnused; @@ -241,6 +243,7 @@ public class SemanticAnalysis extends SemanticAnalyzer { public SemanticHighlightVisitor(Map<OffsetRange, Set<ColoringAttributes>> highlights, Snapshot snapshot, Model model) { this.highlights = highlights; + privateUnusedConstants = new HashMap<>(); privateFieldsUnused = new HashMap<>(); privateUnusedMethods = new HashMap<>(); this.snapshot = snapshot; @@ -325,6 +328,16 @@ public class SemanticAnalysis extends SemanticAnalyzer { } } + private void addColoringForUnusedPrivateConstants() { + for (ASTNodeColoring item : privateUnusedConstants.values()) { + if (item.coloring.contains(ColoringAttributes.DEPRECATED)) { + addColoringForNode(item.identifier, DEPRECATED_UNUSED_STATIC_FIELD_SET); + } else { + addColoringForNode(item.identifier, UNUSED_STATIC_FIELD_SET); + } + } + } + @Override public void scan(ASTNode node) { if (!isCancelled()) { @@ -377,6 +390,7 @@ public class SemanticAnalysis extends SemanticAnalyzer { Block block = needToScan.remove(0); block.accept(this); } + addColoringForUnusedPrivateConstants(); addColoringForUnusedPrivateFields(); } removeFromPath(); @@ -550,6 +564,7 @@ public class SemanticAnalysis extends SemanticAnalyzer { Block block = needToScan.remove(0); block.accept(this); } + addColoringForUnusedPrivateConstants(); addColoringForUnusedPrivateFields(); } removeFromPath(); @@ -727,11 +742,15 @@ public class SemanticAnalysis extends SemanticAnalyzer { parentNode = path.get(1); } if (parentNode instanceof ClassDeclaration || parentNode instanceof InterfaceDeclaration - || parentNode instanceof TraitDeclaration) { + || parentNode instanceof ClassInstanceCreation) { + boolean isPrivate = Modifier.isPrivate(node.getModifier()); List<Identifier> names = node.getNames(); - if (!names.isEmpty()) { - for (Identifier identifier : names) { - addColoringForNode(identifier, createConstantDeclarationColoring(identifier)); + for (Identifier identifier : names) { + Set<ColoringAttributes> coloring = createConstantDeclarationColoring(identifier); + if (!isPrivate) { + addColoringForNode(identifier, coloring); + } else { + privateUnusedConstants.put(new UnusedIdentifier(identifier.getName(), typeInfo), new ASTNodeColoring(identifier, coloring)); } } } @@ -770,6 +789,10 @@ public class SemanticAnalysis extends SemanticAnalyzer { } Identifier constant = node.getConstantName(); if (constant != null) { + ASTNodeColoring item = privateUnusedConstants.remove(new UnusedIdentifier(constant.getName(), typeInfo)); + if (item != null) { + addColoringForNode(item.identifier, item.coloring); + } addColoringForNode(constant, ColoringAttributes.STATIC_FIELD_SET); } super.visit(node); diff --git a/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php b/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php index dd77281..b71435c 100644 --- a/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php +++ b/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php @@ -1,6 +1,8 @@ <?php new class { + private const USED_PRIVATE_CONST = 1; + private const UNUSED_PRIVATE_CONST = 2; private $usedField; private $unusedField; private static $usedStaticField; @@ -8,6 +10,7 @@ new class { public function publicMethod() { + self::USED_PRIVATE_CONST; $this->usedField = 10; self::$usedStaticField = 20; $this->usedPrivateMethod(); diff --git a/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php.semantic b/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php.semantic index 0fdedbc..4a5722d 100644 --- a/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php.semantic +++ b/php/php.editor/test/unit/data/testfiles/semantic/anonymousClass01.php.semantic @@ -1,6 +1,8 @@ <?php new class { + private const |>FIELD,STATIC:USED_PRIVATE_CONST<| = 1; + private const |>FIELD,STATIC,UNUSED:UNUSED_PRIVATE_CONST<| = 2; private $|>FIELD:usedField<|; private $|>FIELD,UNUSED:unusedField<|; private static $|>FIELD,STATIC:usedStaticField<|; @@ -8,6 +10,7 @@ new class { public function |>METHOD:publicMethod<|() { + self::|>FIELD,STATIC:USED_PRIVATE_CONST<|; $this->|>FIELD:usedField<| = 10; self::$|>FIELD,STATIC:usedStaticField<| = 20; $this->|>CUSTOM1:usedPrivateMethod<|(); diff --git a/php/php.editor/test/unit/data/testfiles/semantic/class005.php b/php/php.editor/test/unit/data/testfiles/semantic/class005.php index 4d4bd5f..5a278a6 100644 --- a/php/php.editor/test/unit/data/testfiles/semantic/class005.php +++ b/php/php.editor/test/unit/data/testfiles/semantic/class005.php @@ -1,5 +1,9 @@ <?php class person { // class name + public const MIN_AGE = 1; // public constant + protected const MAX_AGE = 150; // protected constant + private const SETTING1 = 'abc'; // used private constant + private const SETTING2 = 5; // unused private constant private $name; // class field declaration public $me = "mydefaultname"; // class field declaration private $you; // unused private class field @@ -11,6 +15,7 @@ class person { // class name echo $this->$name."\n"; // $name is on class field echo $this->name."\n"; // usage of class field person::$count = person::$count + 1; + echo self::SETTING1."\n"; } private function yourName() { // unused method diff --git a/php/php.editor/test/unit/data/testfiles/semantic/class005.php.semantic b/php/php.editor/test/unit/data/testfiles/semantic/class005.php.semantic index 034d455..932eee4 100644 --- a/php/php.editor/test/unit/data/testfiles/semantic/class005.php.semantic +++ b/php/php.editor/test/unit/data/testfiles/semantic/class005.php.semantic @@ -1,5 +1,9 @@ <?php class |>CLASS:person<| { // class name + public const |>FIELD,STATIC:MIN_AGE<| = 1; // public constant + protected const |>FIELD,STATIC:MAX_AGE<| = 150; // protected constant + private const |>FIELD,STATIC:SETTING1<| = 'abc'; // used private constant + private const |>FIELD,STATIC,UNUSED:SETTING2<| = 5; // unused private constant private $|>FIELD:name<|; // class field declaration public $|>FIELD:me<| = "mydefaultname"; // class field declaration private $|>FIELD,UNUSED:you<|; // unused private class field @@ -11,6 +15,7 @@ class |>CLASS:person<| { // class name echo $this->$name."\n"; // $name is on class field echo $this->|>FIELD:name<|."\n"; // usage of class field person::$|>FIELD,STATIC:count<| = person::$|>FIELD,STATIC:count<| + 1; + echo self::|>FIELD,STATIC:SETTING1<|."\n"; } private function |>METHOD,UNUSED:yourName<|() { // unused method diff --git a/php/php.editor/test/unit/data/testfiles/semantic/unusedPrivateConst.php b/php/php.editor/test/unit/data/testfiles/semantic/unusedPrivateConst.php new file mode 100644 index 0000000..c87701c --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/semantic/unusedPrivateConst.php @@ -0,0 +1,13 @@ +<?php + +class Foo { + + public const PUBLIC_CONST = 1; + protected const PROTECTED_CONST = 2; + private const USED_PRIVATE = 3; + private const UNUSED_PRIVATE = 4; + + function bar() { + return self::USED_PRIVATE; + } +} diff --git a/php/php.editor/test/unit/data/testfiles/semantic/unusedPrivateConst.php.semantic b/php/php.editor/test/unit/data/testfiles/semantic/unusedPrivateConst.php.semantic new file mode 100644 index 0000000..966d56e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/semantic/unusedPrivateConst.php.semantic @@ -0,0 +1,13 @@ +<?php + +class |>CLASS:Foo<| { + + public const |>FIELD,STATIC:PUBLIC_CONST<| = 1; + protected const |>FIELD,STATIC:PROTECTED_CONST<| = 2; + private const |>FIELD,STATIC:USED_PRIVATE<| = 3; + private const |>FIELD,STATIC,UNUSED:UNUSED_PRIVATE<| = 4; + + function |>METHOD:bar<|() { + return self::|>FIELD,STATIC:USED_PRIVATE<|; + } +} diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/SemanticAnalyzerTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/SemanticAnalyzerTest.java index ba14216..f943e6d 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/SemanticAnalyzerTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/SemanticAnalyzerTest.java @@ -47,6 +47,10 @@ public class SemanticAnalyzerTest extends SemanticAnalysisTestBase { checkSemantic("testfiles/semantic/class002.php"); } + public void testAnalysisUnusedPrivateConstant() throws Exception { + checkSemantic("testfiles/semantic/unusedPrivateConst.php"); + } + public void testAnalysisUnusedPrivateField() throws Exception { checkSemantic("testfiles/semantic/class003.php"); } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists