Skip to content

Commit

Permalink
Merge pull request apache#2555 from KacerCZ/netbeans-5052-unused-priv…
Browse files Browse the repository at this point in the history
…ate-const

[NETBEANS-5052] Mark unused private constants
  • Loading branch information
junichi11 authored Nov 29, 2020
2 parents ad23179 + 24c4275 commit e9bd4c3
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ public ASTNodeColoring(ASTNode identifier, Set<ColoringAttributes> coloring) {
}

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;
Expand Down Expand Up @@ -241,6 +243,7 @@ public ASTNodeColoring(ASTNode identifier, Set<ColoringAttributes> coloring) {

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;
Expand Down Expand Up @@ -325,6 +328,16 @@ private void addColoringForUnusedPrivateFields() {
}
}

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()) {
Expand Down Expand Up @@ -377,6 +390,7 @@ public void visit(ClassDeclaration cldec) {
Block block = needToScan.remove(0);
block.accept(this);
}
addColoringForUnusedPrivateConstants();
addColoringForUnusedPrivateFields();
}
removeFromPath();
Expand Down Expand Up @@ -550,6 +564,7 @@ public void visit(ClassInstanceCreation node) {
Block block = needToScan.remove(0);
block.accept(this);
}
addColoringForUnusedPrivateConstants();
addColoringForUnusedPrivateFields();
}
removeFromPath();
Expand Down Expand Up @@ -727,11 +742,15 @@ public void visit(ConstantDeclaration node) {
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));
}
}
}
Expand Down Expand Up @@ -770,6 +789,10 @@ public void visit(StaticConstantAccess node) {
}
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

new class {
private const USED_PRIVATE_CONST = 1;
private const UNUSED_PRIVATE_CONST = 2;
private $usedField;
private $unusedField;
private static $usedStaticField;
private static $unusedStaticField;


public function publicMethod() {
self::USED_PRIVATE_CONST;
$this->usedField = 10;
self::$usedStaticField = 20;
$this->usedPrivateMethod();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?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<|;
private static $|>FIELD,STATIC,UNUSED:unusedStaticField<|;


public function |>METHOD:publicMethod<|() {
self::|>FIELD,STATIC:USED_PRIVATE_CONST<|;
$this->|>FIELD:usedField<| = 10;
self::$|>FIELD,STATIC:usedStaticField<| = 20;
$this->|>CUSTOM1:usedPrivateMethod<|();
Expand Down
5 changes: 5 additions & 0 deletions php/php.editor/test/unit/data/testfiles/semantic/class005.php
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +15,7 @@ public function __construct($name) { // method 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<|;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public void testAnalysisStatic() throws Exception {
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");
}
Expand Down

0 comments on commit e9bd4c3

Please sign in to comment.