Search in sources :

Example 6 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project idea-php-typo3-plugin by cedricziel.

the class ExtbaseControllerActionAction method actionPerformed.

/**
 * @param event Carries information on the invocation place
 */
@Override
public void actionPerformed(AnActionEvent event) {
    final Project project = getEventProject(event);
    if (project == null) {
        this.setStatus(event, false);
        return;
    }
    DataContext dataContext = event.getDataContext();
    final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    final PhpClass targetClass = editor == null || file == null ? null : PhpCodeEditUtil.findClassAtCaret(editor, file);
    if (targetClass == null) {
        JOptionPane.showMessageDialog(null, "Could not find containing class");
        return;
    }
    String actionName = Messages.showInputDialog(project, "New action name:", "New Extbase ActionController Action", TYPO3CMSIcons.TYPO3_ICON);
    if (StringUtils.isBlank(actionName)) {
        return;
    }
    actionName = Character.toLowerCase(actionName.charAt(0)) + actionName.substring(1);
    if (!actionName.endsWith("Action")) {
        actionName += "Action";
    }
    if (!PhpNameUtil.isValidMethodName(actionName)) {
        JOptionPane.showMessageDialog(null, "Invalid method name");
        return;
    }
    write(project, targetClass, actionName);
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 7 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project idea-php-typo3-plugin by cedricziel.

the class ExtbaseControllerActionAction method update.

public void update(AnActionEvent event) {
    Project project = getEventProject(event);
    if (project == null) {
        this.setStatus(event, false);
        return;
    }
    if (DumbService.isDumb(project)) {
        this.setStatus(event, false);
        return;
    }
    DataContext dataContext = event.getDataContext();
    final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    final PhpClass targetClass = editor == null || file == null ? null : PhpCodeEditUtil.findClassAtCaret(editor, file);
    if (targetClass == null) {
        setStatus(event, false);
        return;
    }
    if (ExtensionUtility.getExtensionDirectory(event) == null) {
        this.setStatus(event, false);
        return;
    }
    this.setStatus(event, PhpElementsUtil.hasSuperClass(targetClass, "\\TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController"));
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 8 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project idea-php-typo3-plugin by cedricziel.

the class TCAUtil method findAvailableRenderTypes.

private static Set<PsiElement> findAvailableRenderTypes(Project project) {
    PhpIndex phpIndex = PhpIndex.getInstance(project);
    PsiFile[] extLocalConfFiles = FilenameIndex.getFilesByName(project, EXT_LOCALCONF_FILENAME, GlobalSearchScope.allScope(project));
    Collection<PhpClass> nodeRegistries = phpIndex.getClassesByFQN(NODE_FACTORY_CLASS);
    Set<PsiElement> elements = new HashSet<>();
    for (PhpClass registry : nodeRegistries) {
        Collections.addAll(elements, PsiTreeUtil.collectElements(registry, element -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(3, PlatformPatterns.psiElement(PhpElementTypes.CLASS_FIELD).withName("nodeTypes"))).accepts(element)));
    }
    for (PsiFile file : extLocalConfFiles) {
        Collections.addAll(elements, PsiTreeUtil.collectElements(file, element -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_VALUE).withParent(PlatformPatterns.psiElement(PhpElementTypes.HASH_ARRAY_ELEMENT).withChild(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withText("'nodeName'")))).accepts(element)));
    }
    return elements;
}
Also used : java.util(java.util) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpElementsUtil.extractArrayIndexFromValue(com.cedricziel.idea.typo3.psi.PhpElementsUtil.extractArrayIndexFromValue) FilenameIndex(com.intellij.psi.search.FilenameIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PhpIndex(com.jetbrains.php.PhpIndex) PlatformPatterns(com.intellij.patterns.PlatformPatterns) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) PhpElementTypes(com.jetbrains.php.lang.parser.PhpElementTypes) PhpIndex(com.jetbrains.php.PhpIndex) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 9 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project idea-php-typo3-plugin by cedricziel.

the class RouteHelper method getTargetMethods.

@NotNull
private static PsiElement[] getTargetMethods(@NotNull Project project, @NotNull String routeName) {
    List<PsiElement> result = new ArrayList<>();
    List<RouteStub> values = FileBasedIndex.getInstance().getValues(RouteIndex.KEY, routeName, GlobalSearchScope.allScope(project));
    PhpIndex phpIndex = PhpIndex.getInstance(project);
    for (RouteStub routeStub : values) {
        String fqn = routeStub.getController();
        Collection<PhpClass> classesByFQN = phpIndex.getClassesByFQN(fqn);
        classesByFQN.forEach(c -> {
            if (c.findMethodByName(routeStub.getMethod()) != null) {
                result.add(c.findMethodByName(routeStub.getMethod()));
            }
        });
    }
    return result.toArray(new PsiElement[result.size()]);
}
Also used : PhpIndex(com.jetbrains.php.PhpIndex) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PhpClass

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

the class ThrowRawExceptionInspector method buildVisitor.

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

        @Override
        public void visitPhpThrowExpression(@NotNull PhpThrowExpression expression) {
            final PsiElement argument = expression.getArgument();
            if (argument instanceof NewExpression) {
                final NewExpression newExpression = (NewExpression) argument;
                final ClassReference classReference = newExpression.getClassReference();
                final String classFqn = classReference == null ? null : classReference.getFQN();
                if (classFqn != null) {
                    if (classFqn.equals("\\Exception")) {
                        holder.registerProblem(classReference, MessagesPresentationUtil.prefixWithEa(messageRawException), new TheLocalFix());
                    } else if (REPORT_MISSING_ARGUMENTS && newExpression.getParameters().length == 0) {
                        final PsiElement resolved = OpenapiResolveUtil.resolveReference(classReference);
                        if (resolved instanceof PhpClass && this.isTarget((PhpClass) resolved)) {
                            holder.registerProblem(newExpression, MessagesPresentationUtil.prefixWithEa(messageNoArguments));
                        }
                    }
                }
            }
        }

        private boolean isTarget(@NotNull PhpClass clazz) {
            final Method constructor = clazz.getConstructor();
            return constructor != null && constructor.getParameters().length == 3 && clazz.findOwnFieldByName("message", false) == null;
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) NewExpression(com.jetbrains.php.lang.psi.elements.NewExpression) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpThrowExpression(com.kalessil.phpStorm.phpInspectionsEA.openApi.elements.PhpThrowExpression) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) Method(com.jetbrains.php.lang.psi.elements.Method) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)35 PsiElement (com.intellij.psi.PsiElement)26 NotNull (org.jetbrains.annotations.NotNull)19 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)14 Method (com.jetbrains.php.lang.psi.elements.Method)11 PhpIndex (com.jetbrains.php.PhpIndex)8 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)7 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)7 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)6 OpenapiResolveUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil)6 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)5 Field (com.jetbrains.php.lang.psi.elements.Field)5 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)5 HashSet (java.util.HashSet)5 Project (com.intellij.openapi.project.Project)4 ClassReference (com.jetbrains.php.lang.psi.elements.ClassReference)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)3 Editor (com.intellij.openapi.editor.Editor)3