Skip to content

Commit

Permalink
Fix #248 - Misleading rendering of $argc and $argv in function bodies
Browse files Browse the repository at this point in the history
Globals and superglobals are always highlighted

Signed-off-by: Dawid Pakuła <zulus@w3des.net>
  • Loading branch information
zulus committed Sep 6, 2023
1 parent b6fed48 commit 701578a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public static String[] getVariables(PHPVersion phpVersion, int type) {
return getInstance(phpVersion).getByType(type);
}

public static boolean isSuperGlobal(@Nullable String name, PHPVersion phpVersion) {
if (name == null) {
return false;
}
String[] variables = getInstance(phpVersion).variables.get(SUPER_GLOBAL);
for (String variable : variables) {
if (variable.equals(name)) {
return true;
}
}
return false;
}

public String[] getByType(int type) {
List<String> result = new LinkedList<>();
for (Entry<Integer, String[]> item : variables.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class SuperGlobalHighlighting extends AbstractSemanticHighlighting {
protected class SuperGlobalApply extends AbstractSemanticApply {
private int scope = 0;

@Override
public boolean visit(Variable var) {
Expand All @@ -28,6 +29,22 @@ public boolean visit(Variable var) {
return true;
}

@Override
public boolean visit(Block block) {
if (isScope(block.getParent().getType())) {
scope++;
}
return super.visit(block);
}

@Override
public void endVisit(Block block) {
if (isScope(block.getParent().getType())) {
scope--;
}
super.endVisit(block);
}

@Override
public boolean visit(SingleFieldDeclaration singleFieldDeclaration) {
// only handle property values (but not the property names)
Expand All @@ -48,10 +65,27 @@ private boolean isSuperGlobal(Variable var) {
if ((var.isDollared() || ASTNodes.isQuotedDollaredCurlied(var)) && var.getName() instanceof Identifier) {
String name = "$" //$NON-NLS-1$
+ ((Identifier) var.getName()).getName();
if (scope > 0) {
return PHPVariables.isSuperGlobal(name, var.getAST().apiLevel());
}
return PHPVariables.isVariable(name, var.getAST().apiLevel());
}
return false;
}

private boolean isScope(int type) {
switch (type) {
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
case ASTNode.CLASS_DECLARATION:
case ASTNode.FUNCTION_DECLARATION:
case ASTNode.LAMBDA_FUNCTION_DECLARATION:
case ASTNode.ARROW_FUNCTION_DECLARATION:
case ASTNode.INTERFACE_DECLARATION:
return true;
}

return false;
}
}

@Override
Expand Down

0 comments on commit 701578a

Please sign in to comment.