org.sonar.php.checks.utils.CheckUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.php.checks.utils.CheckUtils.java

Source

/*
 * SonarQube PHP Plugin
 * Copyright (C) 2010-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.php.checks.utils;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.sonar.php.tree.impl.PHPTree;
import org.sonar.plugins.php.api.tree.Tree;
import org.sonar.plugins.php.api.tree.Tree.Kind;
import org.sonar.plugins.php.api.tree.declaration.FunctionDeclarationTree;
import org.sonar.plugins.php.api.tree.declaration.FunctionTree;
import org.sonar.plugins.php.api.tree.declaration.MethodDeclarationTree;
import org.sonar.plugins.php.api.tree.expression.FunctionCallTree;
import org.sonar.plugins.php.api.tree.lexical.SyntaxToken;
import org.sonar.plugins.php.api.tree.lexical.SyntaxTrivia;

import java.util.List;

public class CheckUtils {

    private CheckUtils() {
    }

    private static final Kind[] FUNCTION_KINDS_ARRAY = { Kind.METHOD_DECLARATION, Kind.FUNCTION_DECLARATION,
            Kind.FUNCTION_EXPRESSION };

    public static final ImmutableList<Kind> FUNCTION_KINDS = ImmutableList.copyOf(FUNCTION_KINDS_ARRAY);

    public static boolean isFunction(Tree tree) {
        return tree.is(FUNCTION_KINDS_ARRAY);
    }

    /**
     * Returns function or method's name, or "expression" if the given node is a function expression.
     *
     * @param functionDec FUNCTION_DECLARATION, METHOD_DECLARATION or FUNCTION_EXPRESSION
     * @return name of function or "expression" if function expression
     */
    public static String getFunctionName(FunctionTree functionDec) {
        if (functionDec.is(Kind.FUNCTION_DECLARATION)) {
            return "\"" + ((FunctionDeclarationTree) functionDec).name().text() + "\"";
        } else if (functionDec.is(Kind.METHOD_DECLARATION)) {
            return "\"" + ((MethodDeclarationTree) functionDec).name().text() + "\"";
        }
        return "expression";
    }

    /**
     * Return whether the method is overriding a parent method or not.
     *
     * @param declaration METHOD_DECLARATION
     * @return true if method has tag "@inheritdoc" in it's doc comment.
     */
    public static boolean isOverriding(MethodDeclarationTree declaration) {
        for (SyntaxTrivia comment : ((PHPTree) declaration).getFirstToken().trivias()) {
            if (StringUtils.containsIgnoreCase(comment.text(), "@inheritdoc")) {
                return true;
            }
        }
        return false;
    }

    public static final ImmutableMap<String, String> PREDEFINED_VARIABLES = ImmutableMap.<String, String>builder()
            .put("$HTTP_SERVER_VARS", "$_SERVER").put("$HTTP_GET_VARS", "$_GET").put("$HTTP_POST_VARS", "$_POST")
            .put("$HTTP_POST_FILES", "$_FILES").put("$HTTP_SESSION_VARS", "$_SESSION")
            .put("$HTTP_ENV_VARS", "$_ENV").put("$HTTP_COOKIE_VARS", "$_COOKIE").build();

    public static boolean isExitExpression(FunctionCallTree functionCallTree) {
        String callee = functionCallTree.callee().toString();
        return "die".equalsIgnoreCase(callee) || "exit".equalsIgnoreCase(callee);
    }

    public static boolean hasModifier(List<SyntaxToken> modifiers, String toFind) {
        for (SyntaxToken modifier : modifiers) {
            if (modifier.text().equalsIgnoreCase(toFind)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isClosingTag(SyntaxToken token) {
        if (token.is(Kind.INLINE_HTML_TOKEN)) {
            String text = token.text().trim();
            return "?>".equals(text) || "%>".equals(text);
        }
        return false;
    }

}