private static boolean isStatementBlock(AstNode lcurly) { return lcurly.getParent().getParent().is(PHPGrammar.STATEMENT) && lcurly.getParent().getParent().getParent().isNot( PHPGrammar.DECLARE_STATEMENT, PHPGrammar.FOREACH_STATEMENT, PHPGrammar.FOR_STATEMENT, PHPGrammar.DO_WHILE_STATEMENT, PHPGrammar.WHILE_STATEMENT, PHPGrammar.ELSE_CLAUSE, PHPGrammar.ELSEIF_CLAUSE, PHPGrammar.IF_STATEMENT); }
private static boolean isNotNested(AstNode returnNode) { return returnNode.getParent().is(Kind.BLOCK) && returnNode.getParent().getParent().is( Kind.SET_METHOD, Kind.GET_METHOD, Kind.METHOD, Kind.GENERATOR_METHOD, Kind.GENERATOR_FUNCTION_EXPRESSION, Kind.FUNCTION_EXPRESSION, Kind.FUNCTION_DECLARATION, Kind.GENERATOR_DECLARATION); }
@Override public AstSelect parent() { List<AstNode> result = new ArrayList<>(); for (AstNode node : list) { node = node.getParent(); if (node != null) { result.add(node); } } return AstSelectFactory.create(result); }
private AstNode getFirstAncestor(AstNode node, AstNodeType t1, AstNodeType t2) { AstNode result = node; while (result != null && !result.is(t1, t2)) { result = result.getParent(); } return result; }
private boolean isExcluded(AstNode rcurly) { return rcurly.getParent().is( CSharpGrammar.ARRAY_INITIALIZER, CSharpGrammar.COLLECTION_INITIALIZER, CSharpGrammar.OBJECT_INITIALIZER); }
private boolean isWrapperFunction(AstNode childFunction) { if(astNode != null && astNode.is(PythonGrammar.FUNCDEF)) { AstNode childStatement = childFunction.getParent().getParent(); return astNode.getFirstChild(PythonGrammar.SUITE) .getChildren(PythonGrammar.STATEMENT) .stream() .filter(statement -> statement != childStatement) .allMatch(NestingLevel::isSimpleReturn); } return false; }
private boolean isConstructor(AstNode methodNode, String methodName) { AstNode grandParent = methodNode.getParent().getParent(); boolean isConstructorBeforePHP5_3_3 = false; if (grandParent.is(PHPGrammar.CLASS_DECLARATION)) { isConstructorBeforePHP5_3_3 = methodName.equals(grandParent.getFirstChild(PHPGrammar.IDENTIFIER).getTokenOriginalValue()); } return isConstructorBeforePHP5_3_3 || "__construct".equals(methodName); }
@Override public Object getParentNode(Object contextNode) { if (isElement(contextNode)) { AstNode astNode = (AstNode) contextNode; return astNode.getParent(); } else if (isAttribute(contextNode)) { Attribute attribute = (Attribute) contextNode; return attribute.getAstNode(); } else { throw new UnsupportedOperationException("Unsupported context object type for parent node \"" + contextNode.getClass().getSimpleName() + "\": " + contextNode); } }
private static boolean isNotVariableVariables(AstNode compoundNode) { AstNode compoundParentPreviousNode = compoundNode.getParent().getPreviousAstNode(); return compoundParentPreviousNode != null && compoundParentPreviousNode.isNot(PHPGrammar.SIMPLE_INDIRECT_REFERENCE); } }
private void retrieveAllPrivateFields(AstNode classDef) { for (AstNode varDeclaration : Clazz.getFields(classDef)) { if (Modifiers.getModifiers(varDeclaration.getParent().getPreviousAstNode()).contains(FlexKeyword.PRIVATE)) { for (AstNode identifier : Variable.getDeclaredIdentifiers(varDeclaration)) { privateFields.put(identifier.getTokenValue(), new PrivateField(identifier, 0)); } } } }
@Override public void visitNode(AstNode astNode) { Token lastToken = astNode.getLastToken(); if (lastToken == null || !FlexPunctuator.SEMICOLON.getValue().equals(lastToken.getValue())) { addIssue("Add a semicolon at the end of this statement", astNode.getParent().getLastToken()); } }
@Override public void leaveNode(AstNode astNode) { if (astNode.is(PHPGrammar.BLOCK) && isFunctionBlock(astNode)) { if (getReturnStatementCounter() > max) { getContext().createLineViolation(this, "Reduce the number of returns of this function {0}, down to the maximum allowed {1}.", astNode.getParent(), getReturnStatementCounter(), max); } returnStatementCounter.pop(); } }
@Override public void visitNode(AstNode node) { AstNode statementNode = node.getParent(); AstNode nextStatement = statementNode.getNextSibling(); if (isUnReachableCode(nextStatement)) { getContext().createLineViolation(this, "Remove the code after this \"{0}\".", nextStatement, node.getFirstChild().getTokenOriginalValue()); } }
@Override public void visitNode(AstNode node) { if (node.hasParent(PlSqlGrammar.VARIABLE_DECLARATION)) { AstNode datatype = node.getParent().getFirstChild(PlSqlGrammar.DATATYPE); AstNode expression = node.getLastChild(); if (expression.is(PlSqlGrammar.METHOD_CALL) && !datatype.getTokenValue().equals(expression.getTokenValue())) { addLineIssue(getLocalizedMessage(CHECK_KEY), node.getTokenLine()); } } }
@Override public void visitNode(AstNode astNode) { AstNode defaultClause = astNode.getFirstChild(PHPGrammar.DEFAULT_CLAUSE); if (defaultClause == null) { getContext().createLineViolation(this, "Add a \"case default\" clause to this \"switch\" statement.", astNode.getParent()); } else if (defaultClause.getNextAstNode().isNot(PHPPunctuator.RCURLYBRACE)) { getContext().createLineViolation(this, "Move this \"case default\" clause to the end of this \"switch\" statement.", defaultClause); } } }
@Override public void visitNode(AstNode node) { MethodMatcher count = MethodMatcher.create().name("count").addParameter(); if (!node.getParent().is(DmlGrammar.SELECT_COLUMN) || !count.matches(node)) { return; } checkArguments(node, count.getArgumentsValues(node)); }