com.jetbrains.php.lang.psi.PhpFile Java Examples

The following examples show how to use com.jetbrains.php.lang.psi.PhpFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SymfonyContainerServiceBuilder.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public void update(AnActionEvent event) {
    Project project = event.getData(PlatformDataKeys.PROJECT);

    if (project == null || !Symfony2ProjectComponent.isEnabled(project)) {
        this.setStatus(event, false);
        return;
    }

    Pair<PsiFile, PhpClass> pair = findPhpClass(event);
    if(pair == null) {
        return;
    }

    PsiFile psiFile = pair.getFirst();
    if(!(psiFile instanceof YAMLFile) && !(psiFile instanceof XmlFile) && !(psiFile instanceof PhpFile)) {
        this.setStatus(event, false);
    }
}
 
Example #2
Source File: ContainerBuilderStubIndex.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, ContainerBuilderCall, FileContent> getIndexer() {

    return inputData -> {

        Map<String, ContainerBuilderCall> map = new THashMap<>();

        PsiFile psiFile = inputData.getPsiFile();
        if(!(psiFile instanceof PhpFile) ||
            !Symfony2ProjectComponent.isEnabledForIndex(psiFile.getProject()) ||
            !isValidForIndex(inputData, psiFile)
            ){

            return map;
        }

        StreamEx.of(PhpPsiUtil.findAllClasses((PhpFile) psiFile))
            .flatMap(clazz -> StreamEx.of(clazz.getOwnMethods()))
            .forEach(method -> processMethod(method, map));
        return map;
    };
}
 
Example #3
Source File: EventMethodCallInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
        return super.buildVisitor(holder, isOnTheFly);
    }

    return new PsiElementVisitor() {
        @Override
        public void visitFile(PsiFile psiFile) {
            if(psiFile instanceof XmlFile) {
                visitXmlFile(psiFile, holder, new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
            } else if(psiFile instanceof YAMLFile) {
                visitYamlFile(psiFile, holder, new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
            } else if(psiFile instanceof PhpFile) {
                visitPhpFile((PhpFile) psiFile, holder);
            }
        }
    };
}
 
Example #4
Source File: DoctrineUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Index metadata file with its class and repository.
 * As of often class stay in static only context
 */
@Nullable
public static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull PsiFile psiFile) {

    Collection<Pair<String, String>> pairs = null;

    if(psiFile instanceof XmlFile) {
        pairs = getClassRepositoryPair((XmlFile) psiFile);
    } else if(psiFile instanceof YAMLFile) {
        pairs = getClassRepositoryPair((YAMLFile) psiFile);
    } else if(psiFile instanceof PhpFile) {
        pairs = getClassRepositoryPair((PsiElement) psiFile);
    }

    return pairs;
}
 
Example #5
Source File: ServiceDeprecatedClassesInspection.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void visitFile(PsiFile psiFile) {

    ProblemRegistrar problemRegistrar = null;

    if(psiFile instanceof YAMLFile) {
        psiFile.acceptChildren(new YmlClassElementWalkingVisitor(holder, problemRegistrar = new ProblemRegistrar()));
    } else if(psiFile instanceof XmlFile) {
        psiFile.acceptChildren(new XmlClassElementWalkingVisitor(holder, problemRegistrar = new ProblemRegistrar()));
    } else if(psiFile instanceof PhpFile) {
        psiFile.acceptChildren(new PhpClassWalkingVisitor(holder, problemRegistrar = new ProblemRegistrar()));
    }

    if(problemRegistrar != null) {
        problemRegistrar.reset();
    }

    super.visitFile(psiFile);
}
 
Example #6
Source File: ConfigEntityTypeAnnotationIndex.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, String, FileContent> getIndexer() {
    return inputData -> {
        final Map<String, String> map = new THashMap<>();

        PsiFile psiFile = inputData.getPsiFile();
        if(!(psiFile instanceof PhpFile)) {
            return map;
        }

        if(!IndexUtil.isValidForIndex(inputData, psiFile)) {
            return map;
        }

        psiFile.accept(new MyPsiRecursiveElementWalkingVisitor(map));

        return map;
    };
}
 
Example #7
Source File: ConfigKeyStubIndex.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Void, FileContent> getIndexer() {
    return fileContent -> {
        final Map<String, Void> map = new THashMap<>();

        PsiFile psiFile = fileContent.getPsiFile();
        if(!(psiFile instanceof PhpFile)) {
            return map;
        }

        ConfigFileUtil.ConfigFileMatchResult result = ConfigFileUtil.matchConfigFile(fileContent.getProject(), fileContent.getFile());

        // config/app.php
        // config/testing/app.php
        if(result.matches()) {
            psiFile.acceptChildren(new ArrayReturnPsiRecursiveVisitor(result.getKeyPrefix(), (key, psiKey, isRootElement) -> {
                if (!isRootElement) {
                    map.put(key, null);
                }
            }));
        }

        return map;
    };
}
 
Example #8
Source File: BladeCustomDirectivesStubIndex.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Void, FileContent> getIndexer() {
    return fileContent -> {
        final Map<String, Void> map = new THashMap<>();
        PsiFile psiFile = fileContent.getPsiFile();

        if(!(psiFile instanceof PhpFile)) {
            return map;
        }

        psiFile.acceptChildren(new BladeCustomDirectivesVisitor(hit -> map.put(hit.second, null)));

        return map;
    };
}
 
Example #9
Source File: TranslationKeyStubIndex.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Void, FileContent> getIndexer() {
    return fileContent -> {

        final Map<String, Void> map = new THashMap<>();

        PsiFile psiFile = fileContent.getPsiFile();
        if(!(psiFile instanceof PhpFile)) {
            return map;
        }

        String namespace = TranslationUtil.getNamespaceFromFilePath(fileContent.getFile().getPath());
        if(namespace == null) {
            return map;
        }

        psiFile.acceptChildren(new ArrayReturnPsiRecursiveVisitor(
            namespace, (key, psiKey, isRootElement) -> map.put(key, null))
        );

        return map;
    };
}
 
Example #10
Source File: ServiceGenerateAction.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
private boolean isValidForPhpClass(Editor editor, PsiFile file) {

        if(!(file instanceof PhpFile)) {
            return false;
        }

        int offset = editor.getCaretModel().getOffset();
        if(offset <= 0) {
            return false;
        }

        PsiElement psiElement = file.findElementAt(offset);
        if(psiElement == null) {
            return false;
        }

        return PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement);
    }
 
Example #11
Source File: BundleClassGeneratorUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PhpClass getBundleClassInDirectory(@NotNull PsiDirectory bundleDirContext) {

    for (PsiFile psiFile : bundleDirContext.getFiles()) {

        if(!(psiFile instanceof PhpFile)) {
            continue;
        }

        PhpClass aClass = PhpPsiUtil.findClass((PhpFile) psiFile, phpClass ->
            PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface")
        );

        if(aClass != null) {
            return aClass;
        }

    }

    return null;
}
 
Example #12
Source File: DoctrinePropertyOrmAnnotationGenerateAction.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {

    if(!(file instanceof PhpFile) || !DoctrineUtil.isDoctrineOrmInVendor(project)) {
        return false;
    }

    int offset = editor.getCaretModel().getOffset();
    if(offset <= 0) {
        return false;
    }

    PsiElement psiElement = file.findElementAt(offset);
    if(psiElement == null) {
        return false;
    }

    if(!PlatformPatterns.psiElement().inside(PhpClass.class).accepts(psiElement)) {
        return false;
    }

    return true;
}
 
Example #13
Source File: PhpAutoPopupSpaceTypedHandler.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {

    if (!(file instanceof PhpFile)) {
        return TypedHandlerDelegate.Result.CONTINUE;
    }

    if ((c != ' ')) {
        return TypedHandlerDelegate.Result.CONTINUE;
    }

    PsiElement psiElement = file.findElementAt(editor.getCaretModel().getOffset() - 2);
    if (psiElement == null || !(PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL).accepts(psiElement) || PlatformPatterns.psiElement(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE).accepts(psiElement))) {
        return TypedHandlerDelegate.Result.CONTINUE;
    }

    scheduleAutoPopup(project, editor, null);

    return TypedHandlerDelegate.Result.CONTINUE;
}
 
Example #14
Source File: CoreServiceMapStubIndex.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Set<String>, FileContent> getIndexer() {

    return inputData -> {

        final Map<String, Set<String>> map = new THashMap<>();

        PsiFile psiFile = inputData.getPsiFile();

        if (psiFile instanceof PhpFile) {
            Map<String, ArrayList<TYPO3ServiceDefinition>> serviceMap = new THashMap<>();
            psiFile.accept(new CoreServiceDefinitionParserVisitor(serviceMap));

            serviceMap.forEach((serviceId, definitionList) -> {
                Set<String> implementations = new HashSet<>();
                definitionList.forEach(typo3ServiceDefinition -> implementations.add(typo3ServiceDefinition.getClassName()));
                map.put(serviceId, implementations);
            });
        }

        return map;
    };
}
 
Example #15
Source File: TemplateAnnotationIndex.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, TemplateAnnotationUsage, FileContent> getIndexer() {
    return inputData -> {
        final Map<String, TemplateAnnotationUsage> map = new HashMap<>();

        PsiFile psiFile = inputData.getPsiFile();
        if (!(psiFile instanceof PhpFile)) {
            return map;
        }

        if (!AnnotationUtil.isValidForIndex(inputData)) {
            return map;
        }

        psiFile.accept(new MyPsiRecursiveElementWalkingVisitor(map));

        return map;
    };
}
 
Example #16
Source File: SwitchContext.java    From phpstorm-plugin with MIT License 6 votes vote down vote up
@Nullable
protected PhpClass getSwitchClass(final AnActionEvent e) {
    Object psiFile = e.getData(PlatformDataKeys.PSI_FILE);

    if (null == psiFile) {
        return null;
    }

    if (!(psiFile instanceof PhpFile)) {
        return null;
    }
    PhpFile phpFile = ((PhpFile) psiFile);
    PhpClass testedClass = getSwitchClass(e.getProject(), phpFile);

    if (null == testedClass) {
        return null;
    }

    return testedClass;
}
 
Example #17
Source File: SwitchContext.java    From phpstorm-plugin with MIT License 6 votes vote down vote up
@Nullable
protected PhpClass getSwitchClass(Project project, PhpFile phpFile) {
    PhpClass currentClass = Utils.getFirstTestClassFromFile(phpFile);
    if (currentClass != null) {
        // The file contains a test class, switch to the tested class
        return Utils.locateTestedClass(project, currentClass);
    }

    currentClass = Utils.getFirstClassFromFile(phpFile);
    if (currentClass != null) {
        // The file contains a class, switch to the test class if it exists
        return Utils.locateTestClass(project, currentClass);
    }

    return null;
}
 
Example #18
Source File: Run.java    From phpstorm-plugin with MIT License 6 votes vote down vote up
@Nullable
protected PhpClass getCurrentTestClass(AnActionEvent e) {
    PhpFile phpFile = getPhpFile(e);

    if (null == phpFile) {
        return null;
    }

    PhpClass currentClass = Utils.getFirstTestClassFromFile(phpFile);

    if (currentClass != null) {
        // The file contains a test class, use it
        return currentClass;
    }

    // There is no tests in this file, maybe this is a php class
    currentClass = Utils.getFirstClassFromFile(phpFile);
    if (null == currentClass) {
        return null;
    }

    // This is a PHP class, find its test
    return Utils.locateTestClass(e.getProject(), currentClass);
}
 
Example #19
Source File: Run.java    From phpstorm-plugin with MIT License 6 votes vote down vote up
@Nullable
protected Method getCurrentTestMethod(AnActionEvent e) {
    PhpFile file = getPhpFile(e);
    Editor editor = getEditor(e);

    if (file == null || editor == null) {
        return null;
    }

    Method method = PsiTreeUtil.findElementOfClassAtOffset(file, editor.getCaretModel().getOffset(), Method.class, false);

    if (method != null && method.getName().startsWith("test")) {
        return method;
    }

    return null;
}
 
Example #20
Source File: AnnotationUsageIndex.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public DataIndexer<String, Set<String>, FileContent> getIndexer() {
    return inputData -> {
        final Map<String, Set<String>> map = new THashMap<>();

        PsiFile psiFile = inputData.getPsiFile();
        if(!(psiFile instanceof PhpFile)) {
            return map;
        }

        if(!AnnotationUtil.isValidForIndex(inputData)) {
            return map;
        }

        psiFile.accept(new PhpDocTagAnnotationRecursiveElementWalkingVisitor(pair -> {
            map.put(pair.getFirst(), new HashSet<>());
            return true;
        }));

        return map;
    };
}
 
Example #21
Source File: PhpBundleCompilerPassGenerateAction.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    if(!(file instanceof PhpFile) || !Symfony2ProjectComponent.isEnabled(project)) {
        return false;
    }

    return PhpBundleFileFactory.getPhpClassForCreateCompilerScope(editor, file) != null;
}
 
Example #22
Source File: PhpAutoPopupSpaceTypedHandler.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
@Override
    public Result checkAutoPopup(char charTyped, Project project, Editor editor, PsiFile file) {

        if (!(file instanceof PhpFile)) {
            return TypedHandlerDelegate.Result.CONTINUE;
        }

//        scheduleAutoPopup(project, editor, null);
//
//        if ((charTyped != ' ')) {
//            return TypedHandlerDelegate.Result.STOP;
//        }

        return TypedHandlerDelegate.Result.CONTINUE;
    }
 
Example #23
Source File: PhpParameterStringCompletionConfidence.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
@NotNull
    @Override
    public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {

        if (!(psiFile instanceof PhpFile)) {
            return ThreeState.UNSURE;
        }

        PsiElement context = contextElement.getContext();
        if (!(context instanceof StringLiteralExpression)) {
            return ThreeState.UNSURE;
        }

//        // $test == "";
//        if(context.getParent() instanceof BinaryExpression) {
//            return ThreeState.NO;
//        }

        // $object->method("");
        PsiElement stringContext = context.getContext();
        if (stringContext instanceof ParameterList) {
            return ThreeState.NO;
        }

//        // $object->method(... array('foo'); array('bar' => 'foo') ...);
//        ArrayCreationExpression arrayCreationExpression = PhpElementsUtil.getCompletableArrayCreationElement(context);
//        if(arrayCreationExpression != null && arrayCreationExpression.getContext() instanceof ParameterList) {
//            return ThreeState.NO;
//        }

//        // $array['value']
//        if(PlatformPatterns.psiElement().withSuperParent(2, ArrayIndex.class).accepts(contextElement)) {
//            return ThreeState.NO;
//        }

        return ThreeState.UNSURE;
    }
 
Example #24
Source File: AnnotationIconProvider.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public Icon getIcon(@NotNull PsiElement element, @Iconable.IconFlags int flags) {
    if (element instanceof PhpFile && PhpPsiUtil.findClasses((PhpFile)element, AnnotationUtil::isAnnotationClass).size() == 1) {
        return PlatformIcons.ANNOTATION_TYPE_ICON;
    }

    return null;
}
 
Example #25
Source File: PhpAutoPopupTypedHandler.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
@NotNull
@Override
public Result checkAutoPopup(char charTyped, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {

    if (!(file instanceof PhpFile)) {
        return Result.CONTINUE;
    }

    if (charTyped != '%') {
        return Result.CONTINUE;
    }

    int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);

    ParameterList parameterList = PhpPsiUtil.getParentByCondition(psiElement, true, ParameterList.INSTANCEOF, Statement.INSTANCEOF);
    if (parameterList != null) {
        FunctionReference functionCall = ObjectUtils.tryCast(parameterList.getParent(), FunctionReference.class);
        String fqn = PhpElementsUtil.resolveFqn(functionCall);

        if (/*charTyped == '%' &&*/ PhpElementsUtil.isFormatFunction(fqn)) {
            if (StringUtil.getPrecedingCharNum(editor.getDocument().getCharsSequence(), offset, '%') % 2 == 0) {
                PhpCompletionUtil.showCompletion(editor);
            }
        }
    }

    return Result.CONTINUE;
}
 
Example #26
Source File: PhpInjectFileReferenceIndex.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
@NotNull
public DataIndexer<String, PhpInjectFileReference, FileContent> getIndexer() {
    return inputData -> {
        Map<String, PhpInjectFileReference> map = new THashMap<>();
        PsiFile file = inputData.getPsiFile();
        if (file instanceof PhpFile) {
            PhpControlFlowUtil.processFile((PhpFile)file, new PhpInjectFileReferenceCollector(map));
        }
        return map;
    };
}
 
Example #27
Source File: PhpBundleCompilerPassIntention.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) {
    if(!Symfony2ProjectComponent.isEnabled(project)) {
        return false;
    }

    if(!(psiElement.getContainingFile() instanceof PhpFile)) {
        return false;
    }

    return PhpBundleFileFactory.getPhpClassForCreateCompilerScope(PsiTreeUtil.getParentOfType(psiElement, PhpClass.class)) != null;
}
 
Example #28
Source File: PhpBundleCompilerPassIntention.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement psiElement) throws IncorrectOperationException {

    if(!(psiElement.getContainingFile() instanceof PhpFile)) {
        return;
    }

    PhpClass phpClass = PhpBundleFileFactory.getPhpClassForCreateCompilerScope(PsiTreeUtil.getParentOfType(psiElement, PhpClass.class));
    if(phpClass == null) {
        return;
    }

    PhpBundleFileFactory.invokeCreateCompilerPass(phpClass, editor);
}
 
Example #29
Source File: PhpBundleFileFactory.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
public static PhpClass getPhpClassForCreateCompilerScope(@NotNull Editor editor, @Nullable PsiFile file) {

    if(file == null || !(file instanceof PhpFile)) {
        return null;
    }

    return getPhpClassForCreateCompilerScope(PhpCodeEditUtil.findClassAtCaret(editor, file));
}
 
Example #30
Source File: ServiceLineMarkerProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void entityClassMarker(PsiElement psiElement, Collection<? super RelatedItemLineMarkerInfo> result) {

        PsiElement phpClassContext = psiElement.getContext();
        if(!(phpClassContext instanceof PhpClass)) {
            return;
        }

        Collection<PsiFile> psiFiles = new ArrayList<>();
        // @TODO: use DoctrineMetadataUtil, for single resolve; we have collecting overhead here
        for(DoctrineModel doctrineModel: EntityHelper.getModelClasses(psiElement.getProject())) {
            PhpClass phpClass = doctrineModel.getPhpClass();
            if(!PhpElementsUtil.isEqualClassName(phpClass, (PhpClass) phpClassContext)) {
                continue;
            }

            PsiFile psiFile = EntityHelper.getModelConfigFile(phpClass);

            // prevent self navigation for line marker
            if(psiFile == null || psiFile instanceof PhpFile) {
                continue;
            }

            psiFiles.add(psiFile);
        }

        if(psiFiles.size() == 0) {
            return;
        }

        NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.DOCTRINE_LINE_MARKER).
            setTargets(psiFiles).
            setTooltipText("Navigate to model");

        result.add(builder.createLineMarkerInfo(psiElement));
    }