Skip to content

Commit

Permalink
[NETBEANS-946] PHP 7.3 support
Browse files Browse the repository at this point in the history
- list() Reference Assignment
  • Loading branch information
junichi11 committed Sep 4, 2018
1 parent 4d6df74 commit b2084d4
Show file tree
Hide file tree
Showing 7 changed files with 707 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
* list($a,$b) = array (1,2),
* list($a, list($b, $c)),
* list("id" => $id, "name" => $name) = $data[0]; // PHP7.1,
* list($a, &$b) = $array; // PHP 7.3
* [$a, $b, $c] = [1, 2, 3]; // PHP7.1,
* ["a" => $a, "b" => $b, "c" => $c] = ["a" => 1, "b" => 2, "c" => 3]; // PHP7.1
* [$a, &$b] = $array; // PHP 7.3
* </pre>
*/
public class ListVariable extends VariableBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
import org.netbeans.modules.php.editor.lexer.PHPTokenId;
import org.netbeans.modules.php.editor.parser.PHPParseResult;
import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
import org.netbeans.modules.php.editor.parser.astnodes.ArrayCreation;
import org.netbeans.modules.php.editor.parser.astnodes.ArrayElement;
import org.netbeans.modules.php.editor.parser.astnodes.ClassInstanceCreation;
import org.netbeans.modules.php.editor.parser.astnodes.Expression;
import org.netbeans.modules.php.editor.parser.astnodes.FunctionInvocation;
import org.netbeans.modules.php.editor.parser.astnodes.ListVariable;
import org.netbeans.modules.php.editor.parser.astnodes.Reference;
import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
import org.openide.filesystems.FileObject;
import org.openide.util.NbBundle;
Expand Down Expand Up @@ -83,6 +87,7 @@ private static final class CheckVisitor extends DefaultVisitor {
private final List<VerificationError> errors = new ArrayList<>();
private final List<ASTNode> nodes = new ArrayList<>();
private final FileObject fileObject;
private boolean isInListVariable = false;

public CheckVisitor(FileObject fileObject) {
this.fileObject = fileObject;
Expand Down Expand Up @@ -111,6 +116,29 @@ public void visit(FunctionInvocation node) {
super.visit(node);
}

@Override
public void visit(ListVariable node) {
if (CancelSupport.getDefault().isCancelled()) {
return;
}
checkListReferenceAssignment(node.getElements());
isInListVariable = true;
super.visit(node);
isInListVariable = false;
}

@Override
public void visit(ArrayCreation node) {
// nested new list syntax has ArrayCreation
// e.g. [$a, [$b, $c]] = $array;
if (CancelSupport.getDefault().isCancelled()) {
return;
}
if (isInListVariable) {
checkListReferenceAssignment(node.getElements());
}
}

private void checkFunctionCallTrailingCommas() {
if (!nodes.isEmpty()) {
BaseDocument document = GsfUtilities.getDocument(fileObject, true);
Expand Down Expand Up @@ -171,6 +199,16 @@ private Token<? extends PHPTokenId> findPreviousToken(TokenSequence<PHPTokenId>
return LexUtilities.findPrevious(ts, Arrays.asList(PHPTokenId.WHITESPACE));
}

private void checkListReferenceAssignment(List<ArrayElement> elements) {
// e.g. list($a, &$b) = $array;
elements.forEach(element -> {
Expression value = element.getValue();
if (value instanceof Reference) {
createError(value);
}
});
}

private void createError(ASTNode node) {
createError(node.getStartOffset(), node.getEndOffset());
}
Expand Down
Loading

0 comments on commit b2084d4

Please sign in to comment.