Search in sources :

Example 51 with PhpType

use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project yii2support by nvlad.

the class ViewMissedPhpDocInspection method buildVisitor.

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

        @Override
        public void visitPhpFile(PhpFile PhpFile) {
            Project project = PhpFile.getProject();
            ViewResolve resolve = ViewUtil.resolveView(PhpFile.getVirtualFile(), project);
            if (resolve == null) {
                return;
            }
            Map<String, String> params = getVariables(PhpFile);
            Map<String, String> declaredVariables = new HashMap<>();
            Collection<PhpDocVariable> variableCollection = PsiTreeUtil.findChildrenOfType(PhpFile, PhpDocVariable.class);
            for (PhpDocVariable variable : variableCollection) {
                declaredVariables.put(variable.getName(), variable.getType().toString());
            }
            Map<String, String> missedVariables = new HashMap<>();
            for (String variableName : params.keySet()) {
                if (!declaredVariables.containsKey(variableName)) {
                    missedVariables.put(variableName, params.get(variableName));
                }
            }
            if (missedVariables.isEmpty()) {
                return;
            }
            String problemDescription = "Missed View variable declaration.";
            ViewMissedPhpDocLocalQuickFix quickFix = new ViewMissedPhpDocLocalQuickFix(PhpFile, missedVariables);
            problemsHolder.registerProblem(PhpFile, problemDescription, quickFix);
        }

        private Map<String, String> getVariables(PhpFile phpFile) {
            Collection<PsiReference> references = ReferencesSearch.search(phpFile).findAll();
            Map<String, String> result = new HashMap<>();
            Map<String, PhpType> viewArgumentCollection = new LinkedHashMap<>();
            for (PsiReference reference : references) {
                MethodReference methodReference = PsiTreeUtil.getParentOfType(reference.getElement(), MethodReference.class);
                if (methodReference == null) {
                    continue;
                }
                Map<String, PhpType> params = RenderUtil.getViewArguments(methodReference);
                for (Map.Entry<String, PhpType> entry : params.entrySet()) {
                    if (viewArgumentCollection.containsKey(entry.getKey())) {
                        PhpType.PhpTypeBuilder typeBuilder = new PhpType.PhpTypeBuilder();
                        typeBuilder.add(viewArgumentCollection.get(entry.getKey()));
                        typeBuilder.add(entry.getValue());
                        viewArgumentCollection.replace(entry.getKey(), typeBuilder.build());
                    } else {
                        viewArgumentCollection.put(entry.getKey(), entry.getValue());
                    }
                }
            }
            for (String key : viewArgumentCollection.keySet()) {
                result.put(key, viewArgumentCollection.get(key).toString());
            }
            return result;
        }
    };
}
Also used : PhpFile(com.jetbrains.php.lang.psi.PhpFile) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) PsiReference(com.intellij.psi.PsiReference) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) LinkedHashMap(java.util.LinkedHashMap) Project(com.intellij.openapi.project.Project) PhpDocVariable(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocVariable) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with PhpType

use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project yii2support by nvlad.

the class MissedViewLocalQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final String projectUrl = YiiApplicationUtils.getYiiRootUrl(project);
    final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
    VirtualFile virtualFile = virtualFileManager.findFileByUrl(projectUrl + myPath);
    if (virtualFile != null) {
        System.out.println("File " + projectUrl + myPath + " already exist.");
        return;
    }
    VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
    if (yiiRoot == null) {
        return;
    }
    PsiDirectory directory = PsiManager.getInstance(project).findDirectory(yiiRoot);
    if (directory == null) {
        return;
    }
    List<String> pathElements = StringUtil.split(StringUtil.trimLeading(myPath, '/'), "/");
    if (pathElements.size() == 0) {
        return;
    }
    final String fileName = pathElements.get(pathElements.size() - 1);
    pathElements.remove(pathElements.size() - 1);
    for (String pathElement : pathElements) {
        if (directory.findSubdirectory(pathElement) == null) {
            directory.createSubdirectory(pathElement);
        }
        directory = directory.findSubdirectory(pathElement);
        if (directory == null) {
            return;
        }
    }
    final PsiFile viewPsiFile = directory.createFile(fileName);
    FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(Files.getFileExtension(myPath));
    String templateName = getFileTemplateName(fileType);
    if (templateName == null) {
        System.out.println("Template for \"" + myPath + "\" not detected.");
        return;
    }
    templateName = templateName.toLowerCase();
    FileEditorManager.getInstance(project).openFile(viewPsiFile.getVirtualFile(), true);
    final FileTemplate[] templates = FileTemplateManager.getDefaultInstance().getTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY);
    FileTemplate template = null;
    for (FileTemplate fileTemplate : templates) {
        if (fileTemplate.isTemplateOfType(fileType) && fileTemplate.getName().toLowerCase().equals(templateName)) {
            template = fileTemplate;
            break;
        }
    }
    if (template != null && viewPsiFile.getViewProvider().getDocument() != null) {
        final Properties properties = FileTemplateManager.getDefaultInstance().getDefaultProperties();
        if (myParameters != null) {
            Set<String> parameters = new LinkedHashSet<>(myParameters.size());
            for (Map.Entry<String, PhpType> parameter : myParameters.entrySet()) {
                parameters.add(parameter.getKey() + ' ' + parameter.getValue());
            }
            properties.setProperty("YII2_VIEW_PARAMETERS", StringUtil.join(parameters, ";"));
        }
        template.setLiveTemplateEnabled(true);
        template.setReformatCode(true);
        try {
            viewPsiFile.getViewProvider().getDocument().insertString(0, template.getText(properties));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) PhpFileType(com.jetbrains.php.lang.PhpFileType) TwigFileType(com.jetbrains.twig.TwigFileType) FileType(com.intellij.openapi.fileTypes.FileType) SmartyFileType(com.jetbrains.smarty.SmartyFileType) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 53 with PhpType

use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.

the class MethodArgumentDroppedMatcherInspection method buildVisitor.

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

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.METHOD_REFERENCE).accepts(element)) {
                return;
            }
            MethodReference methodReference = (MethodReference) element;
            ParameterList parameterList = methodReference.getParameterList();
            PhpExpression classReference = methodReference.getClassReference();
            if (classReference != null) {
                PhpType inferredType = classReference.getInferredType();
                String compiledClassMethodKey = inferredType.toString() + "->" + methodReference.getName();
                if (ExtensionScannerUtil.classMethodHasDroppedArguments(element.getProject(), compiledClassMethodKey)) {
                    Integer maximumNumberOfArguments = ExtensionScannerUtil.getMaximumNumberOfArguments(element.getProject(), compiledClassMethodKey);
                    if (parameterList != null && maximumNumberOfArguments != -1 && parameterList.getParameters().length != maximumNumberOfArguments) {
                        problemsHolder.registerProblem(element, "Number of arguments changed with TYPO3 9, consider refactoring");
                    }
                }
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpExpression(com.jetbrains.php.lang.psi.elements.PhpExpression) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with PhpType

use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.

the class GeneralUtilityServiceTypeProvider method getType.

@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb()) {
        return null;
    }
    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstanceService")) {
        return null;
    }
    MethodReference methodReference = (MethodReference) psiElement;
    if (methodReference.getParameters().length == 0) {
        return null;
    }
    PsiElement firstParam = methodReference.getParameters()[0];
    if (firstParam instanceof StringLiteralExpression) {
        StringLiteralExpression ref = (StringLiteralExpression) firstParam;
        String serviceId = ref.getContents();
        return new PhpType().add("#" + this.getKey() + serviceId);
    }
    return null;
}
Also used : StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with PhpType

use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.

the class GeneralUtilityTypeProvider method getType.

@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb()) {
        return null;
    }
    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstance")) {
        return null;
    }
    MethodReference methodReference = (MethodReference) psiElement;
    if (methodReference.getParameters().length == 0) {
        return null;
    }
    PsiElement firstParam = methodReference.getParameters()[0];
    if (firstParam instanceof PhpReference) {
        PhpReference ref = (PhpReference) firstParam;
        if (ref.getText().toLowerCase().contains("::class")) {
            return new PhpType().add("#" + this.getKey() + ref.getSignature());
        }
    }
    return null;
}
Also used : PhpReference(com.jetbrains.php.lang.psi.elements.PhpReference) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)56 PsiElement (com.intellij.psi.PsiElement)46 NotNull (org.jetbrains.annotations.NotNull)41 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)33 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)20 Project (com.intellij.openapi.project.Project)20 HashSet (java.util.HashSet)20 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)16 Nullable (org.jetbrains.annotations.Nullable)16 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)15 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)14 PhpIndex (com.jetbrains.php.PhpIndex)14 com.kalessil.phpStorm.phpInspectionsEA.utils (com.kalessil.phpStorm.phpInspectionsEA.utils)13 IElementType (com.intellij.psi.tree.IElementType)12 Set (java.util.Set)12 PhpTokenTypes (com.jetbrains.php.lang.lexer.PhpTokenTypes)10 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)9 PhpTypedElement (com.jetbrains.php.lang.psi.elements.PhpTypedElement)9 InterfacesExtractUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.hierarhy.InterfacesExtractUtil)8 Collectors (java.util.stream.Collectors)8