Search in sources :

Example 31 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project yii2support by nvlad.

the class UndetectableTableInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
    return new PhpElementVisitor() {

        @Override
        public void visitPhpClass(PhpClass clazz) {
            PhpIndex index = PhpIndex.getInstance(problemsHolder.getProject());
            if (DatabaseUtils.HasConnections(problemsHolder.getProject()) && ClassUtils.isClassInheritsOrEqual(clazz, ClassUtils.getClass(index, "\\yii\\db\\ActiveRecord"))) {
                String table = DatabaseUtils.getTableByActiveRecordClass(clazz);
                if (table == null) {
                    problemsHolder.registerProblem(clazz.getFirstChild(), "Can not detect database table for class " + clazz.getFQN(), ProblemHighlightType.WEAK_WARNING);
                } else if (!DatabaseUtils.isTableExists(table, problemsHolder.getProject())) {
                    problemsHolder.registerProblem(clazz.getFirstChild(), "Table '" + table + "' not found in database connections", ProblemHighlightType.WEAK_WARNING);
                }
            }
            super.visitPhpClass(clazz);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project yii2support by nvlad.

the class UrlUtils method getRoutes.

public static HashMap<String, Method> getRoutes(Project project) {
    Collection<PhpClass> controllers = getControllers(project);
    HashMap<String, Method> routes = new HashMap<>();
    for (PhpClass controller : controllers) {
        if (!excludeControllers.contains(controller.getFQN()))
            routes.putAll(controllerToRoutes(controller));
    }
    return routes;
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method)

Example 33 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project yii2support by nvlad.

the class UrlUtils method getClassesByParent.

private static Collection<PhpClass> getClassesByParent(String parentFqn, Project project) {
    Collection<PhpClass> subclasses = new ArrayList<>();
    Collection<PhpClass> directSubclasses = PhpIndex.getInstance(project).getDirectSubclasses(parentFqn);
    for (PhpClass directSubclass : directSubclasses) {
        subclasses.addAll(getClassesByParent(directSubclass.getFQN(), project));
    }
    subclasses.addAll(directSubclasses);
    return subclasses;
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass)

Example 34 with PhpClass

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

the class GeneralUtilityServiceTypeProvider method getBySignature.

@Override
public Collection<? extends PhpNamedElement> getBySignature(String expression, Set<String> visited, int depth, Project project) {
    Collection<PhpNamedElement> phpNamedElementCollections = new ArrayList<>();
    PhpIndex phpIndex = PhpIndex.getInstance(project);
    CoreServiceParser serviceParser = new CoreServiceParser();
    serviceParser.collect(project);
    List<TYPO3ServiceDefinition> resolvedServices = serviceParser.resolve(project, expression);
    if (resolvedServices == null || resolvedServices.isEmpty()) {
        return phpNamedElementCollections;
    }
    resolvedServices.forEach(serviceDefinition -> {
        Collection<PhpClass> classesByFQN = phpIndex.getClassesByFQN(serviceDefinition.getClassName());
        phpNamedElementCollections.addAll(classesByFQN);
    });
    return phpNamedElementCollections;
}
Also used : TYPO3ServiceDefinition(com.cedricziel.idea.typo3.domain.TYPO3ServiceDefinition) PhpNamedElement(com.jetbrains.php.lang.psi.elements.PhpNamedElement) PhpIndex(com.jetbrains.php.PhpIndex) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) ArrayList(java.util.ArrayList) CoreServiceParser(com.cedricziel.idea.typo3.container.CoreServiceParser)

Example 35 with PhpClass

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

the class CreateInjectorQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
    PhpDocComment docCommentElement = (PhpDocComment) element.getElement().getParent();
    PsiElement owner = docCommentElement.getOwner();
    if (!(owner instanceof Field)) {
        return;
    }
    Field field = (Field) owner;
    final String fieldName = field.getName();
    final PhpClass containingClass = PsiTreeUtil.getParentOfType(element.getElement(), PhpClass.class);
    if (containingClass == null) {
        return;
    }
    PhpType type = field.getType();
    if (type == PhpType.SCALAR) {
        // Scalar type - can't inject those, heh.
        return;
    }
    final String methodName = "inject" + StringUtils.capitalize(fieldName);
    Method injectorFunction = PhpPsiElementFactory.createMethod(project, "public function " + methodName + " (" + type + " $" + fieldName + ") {" + "  $this->" + fieldName + " = $" + fieldName + ";" + "}");
    final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, element.getContainingFile().getVirtualFile()), true);
    if (editor == null) {
        return;
    }
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    final int insertPos = CodeUtil.getMethodInsertPosition(containingClass, methodName);
    if (insertPos == -1) {
        return;
    }
    new WriteCommandAction(project) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            StringBuffer textBuf = new StringBuffer();
            textBuf.append("\n");
            textBuf.append(injectorFunction.getText());
            editor.getDocument().insertString(insertPos, textBuf);
            final int endPos = insertPos + textBuf.length();
            CodeStyleManager.getInstance(project).reformatText(containingClass.getContainingFile(), insertPos, endPos);
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            Method insertedMethod = containingClass.findMethodByName(methodName);
            if (insertedMethod != null) {
                editor.getCaretModel().moveToOffset(insertedMethod.getTextRange().getStartOffset());
                editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
                element.getElement().delete();
            }
        }

        @Override
        public String getGroupID() {
            return "Create Injection Method";
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Result(com.intellij.openapi.application.Result) Field(com.jetbrains.php.lang.psi.elements.Field) PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

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