Search in sources :

Example 6 with GroupStatement

use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.

the class UnusedGotoLabelInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpGotoLabel(@NotNull PhpGotoLabel label) {
            final Function function = ExpressionSemanticUtil.getScope(label);
            final GroupStatement body = null == function ? null : ExpressionSemanticUtil.getGroupStatement(function);
            if (body != null && ExpressionSemanticUtil.countExpressionsInGroup(body) > 0) {
                /* process goto statements and drop used from existing */
                final Collection<PhpGoto> references = PsiTreeUtil.findChildrenOfType(body, PhpGoto.class);
                if (!references.isEmpty()) {
                    final String labelName = label.getName();
                    for (final PhpGoto gotoExpression : references) {
                        final String labelUsed = gotoExpression.getName();
                        if (null != labelUsed && labelUsed.equals(labelName)) {
                            references.clear();
                            return;
                        }
                    }
                    references.clear();
                }
                /* TODO: marks as unused instead, see https://youtrack.jetbrains.com/issue/WI-34508 */
                holder.registerProblem(label, MessagesPresentationUtil.prefixWithEa(message), ProblemHighlightType.LIKE_DEPRECATED, new TheLocalFix());
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) NotNull(org.jetbrains.annotations.NotNull) PhpGotoLabel(com.jetbrains.php.lang.psi.elements.PhpGotoLabel) PhpGoto(com.jetbrains.php.lang.psi.elements.PhpGoto) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GroupStatement

use of com.jetbrains.php.lang.psi.elements.GroupStatement in project phpinspectionsea by kalessil.

the class ShortListSyntaxCanBeUsedInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        public void visitPhpMultiassignmentExpression(MultiassignmentExpression multiassignmentExpression) {
            /* ensure php version is at least PHP 7.1 */
            final PhpLanguageLevel phpVersion = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
            if (phpVersion.compareTo(PhpLanguageLevel.PHP710) < 0) {
                return;
            }
            /* verify if it's dedicated statement and it's the list(...) construction */
            final PsiElement parent = multiassignmentExpression.getParent();
            if (!OpenapiTypesUtil.isStatementImpl(parent)) {
                return;
            }
            final PsiElement listKeyword = multiassignmentExpression.getFirstChild();
            if (null != listKeyword && PhpTokenTypes.kwLIST == listKeyword.getNode().getElementType()) {
                holder.registerProblem(listKeyword, messageAssign, ProblemHighlightType.WEAK_WARNING, new TheLocalFix());
            }
        }

        public void visitPhpForeach(ForeachStatement foreach) {
            /* ensure php version is at least PHP 7.1 */
            final PhpLanguageLevel phpVersion = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
            if (phpVersion.compareTo(PhpLanguageLevel.PHP710) < 0) {
                return;
            }
            final List<Variable> variables = foreach.getVariables();
            if (variables.size() > 0) {
                PsiElement childNode = foreach.getFirstChild();
                while (null != childNode) {
                    if (childNode.getClass() == LeafPsiElement.class && PhpTokenTypes.kwLIST == childNode.getNode().getElementType()) {
                        holder.registerProblem(childNode, messageForeach, ProblemHighlightType.WEAK_WARNING, new TheLocalFix());
                        break;
                    }
                    childNode = childNode.getNextSibling();
                    if (childNode instanceof GroupStatement) {
                        break;
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) Variable(com.jetbrains.php.lang.psi.elements.Variable) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) MultiassignmentExpression(com.jetbrains.php.lang.psi.elements.MultiassignmentExpression) PhpLanguageLevel(com.jetbrains.php.config.PhpLanguageLevel) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) ForeachStatement(com.jetbrains.php.lang.psi.elements.ForeachStatement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GroupStatement (com.jetbrains.php.lang.psi.elements.GroupStatement)7 PsiElement (com.intellij.psi.PsiElement)4 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)2 Function (com.jetbrains.php.lang.psi.elements.Function)2 Variable (com.jetbrains.php.lang.psi.elements.Variable)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 PhpLanguageLevel (com.jetbrains.php.config.PhpLanguageLevel)1 Catch (com.jetbrains.php.lang.psi.elements.Catch)1 Else (com.jetbrains.php.lang.psi.elements.Else)1 ElseIf (com.jetbrains.php.lang.psi.elements.ElseIf)1 ForeachStatement (com.jetbrains.php.lang.psi.elements.ForeachStatement)1 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)1 If (com.jetbrains.php.lang.psi.elements.If)1 MultiassignmentExpression (com.jetbrains.php.lang.psi.elements.MultiassignmentExpression)1 PhpGoto (com.jetbrains.php.lang.psi.elements.PhpGoto)1 PhpGotoLabel (com.jetbrains.php.lang.psi.elements.PhpGotoLabel)1 Try (com.jetbrains.php.lang.psi.elements.Try)1