|
33 | 33 | import java.util.Stack;
|
34 | 34 | import java.util.regex.Pattern;
|
35 | 35 | import org.netbeans.api.annotations.common.CheckForNull;
|
| 36 | +import org.netbeans.api.annotations.common.NullAllowed; |
36 | 37 | import org.netbeans.api.lexer.Token;
|
37 | 38 | import org.netbeans.api.lexer.TokenSequence;
|
38 | 39 | import org.netbeans.api.lexer.TokenUtilities;
|
39 | 40 | import org.netbeans.modules.php.api.util.StringUtils;
|
40 | 41 | import org.netbeans.modules.php.editor.CodeUtils;
|
| 42 | +import org.netbeans.modules.php.editor.PredefinedSymbols.Attributes; |
| 43 | +import static org.netbeans.modules.php.editor.PredefinedSymbols.Attributes.DEPRECATED; |
41 | 44 | import org.netbeans.modules.php.editor.api.AliasedName;
|
42 | 45 | import org.netbeans.modules.php.editor.api.PhpModifiers;
|
43 | 46 | import org.netbeans.modules.php.editor.api.QualifiedName;
|
|
69 | 72 | import org.netbeans.modules.php.editor.parser.astnodes.AnonymousObjectVariable;
|
70 | 73 | import org.netbeans.modules.php.editor.parser.astnodes.ArrayCreation;
|
71 | 74 | import org.netbeans.modules.php.editor.parser.astnodes.Assignment;
|
| 75 | +import org.netbeans.modules.php.editor.parser.astnodes.Attribute; |
| 76 | +import org.netbeans.modules.php.editor.parser.astnodes.AttributeDeclaration; |
| 77 | +import org.netbeans.modules.php.editor.parser.astnodes.Attributed; |
72 | 78 | import org.netbeans.modules.php.editor.parser.astnodes.ClassInstanceCreation;
|
73 | 79 | import org.netbeans.modules.php.editor.parser.astnodes.ClassInstanceCreationVariable;
|
74 | 80 | import org.netbeans.modules.php.editor.parser.astnodes.ClassName;
|
@@ -358,6 +364,61 @@ public static boolean isDeprecatedFromPHPDoc(Program root, ASTNode node) {
|
358 | 364 | return getDeprecatedDescriptionFromPHPDoc(root, node) != null;
|
359 | 365 | }
|
360 | 366 |
|
| 367 | + public static boolean isDeprecatedFromAttribute(FileScope fileScope, Program root, ASTNode node) { |
| 368 | + if (node instanceof Attributed) { |
| 369 | + List<Attribute> attributes = ((Attributed) node).getAttributes(); |
| 370 | + for (Attribute attribute : attributes) { |
| 371 | + for (AttributeDeclaration attributeDeclaration : attribute.getAttributeDeclarations()) { |
| 372 | + String attributeName = CodeUtils.extractQualifiedName(attributeDeclaration.getAttributeName()); |
| 373 | + if (isPredefinedAttributeName(DEPRECATED, attributeName, fileScope, attributeDeclaration.getStartOffset())) { |
| 374 | + return true; |
| 375 | + } |
| 376 | + } |
| 377 | + } |
| 378 | + } |
| 379 | + return false; |
| 380 | + } |
| 381 | + |
| 382 | + public static boolean isDeprecated(FileScope fileScope, Program root, ASTNode node) { |
| 383 | + if (isDeprecatedFromAttribute(fileScope, root, node)) { |
| 384 | + return true; |
| 385 | + } |
| 386 | + return isDeprecatedFromPHPDoc(root, node); |
| 387 | + } |
| 388 | + |
| 389 | + public static boolean isPredefinedAttributeName(Attributes attribute, String attributeName, FileScope fileScope, int offset) { |
| 390 | + if (attribute.getFqName().equals(attributeName)) { |
| 391 | + return true; |
| 392 | + } |
| 393 | + if (attribute.getName().equals(attributeName)) { |
| 394 | + List<? extends NamespaceScope> declaredNamespaces = new ArrayList<NamespaceScope>(fileScope.getDeclaredNamespaces()); |
| 395 | + Collections.sort(declaredNamespaces, (n1, n2) -> -Integer.compare(n1.getOffset(), n2.getOffset())); |
| 396 | + NamespaceScope namespaceScope = null; |
| 397 | + for (NamespaceScope declaredNamespace : declaredNamespaces) { |
| 398 | + int namespaceOffset = declaredNamespace.getOffset(); |
| 399 | + if (namespaceOffset < offset) { |
| 400 | + namespaceScope = declaredNamespace; |
| 401 | + break; |
| 402 | + } |
| 403 | + } |
| 404 | + // check FQ name because there may be `use \AttributeName;` |
| 405 | + if (isPredefinedAttributeName(attribute, attributeName, namespaceScope, offset)) { |
| 406 | + return true; |
| 407 | + } |
| 408 | + } |
| 409 | + return false; |
| 410 | + } |
| 411 | + |
| 412 | + private static boolean isPredefinedAttributeName(Attributes attribute, String attributeName, @NullAllowed NamespaceScope namespaceScope, int offset) { |
| 413 | + if (namespaceScope != null) { |
| 414 | + QualifiedName fullyQualifiedName = VariousUtils.getFullyQualifiedName(QualifiedName.create(attributeName), offset, namespaceScope); |
| 415 | + if (attribute.getFqName().equals(fullyQualifiedName.toString())) { |
| 416 | + return true; |
| 417 | + } |
| 418 | + } |
| 419 | + return false; |
| 420 | + } |
| 421 | + |
361 | 422 | public static Map<String, Pair<String, List<Pair<QualifiedName, Boolean>>>> getParamTypesFromPHPDoc(Program root, ASTNode node) {
|
362 | 423 | Map<String, Pair<String, List<Pair<QualifiedName, Boolean>>>> retval = new HashMap<>();
|
363 | 424 | Comment comment = Utils.getCommentForNode(root, node);
|
|
0 commit comments