com.jetbrains.php.lang.psi.resolve.types.PhpType Java Examples

The following examples show how to use com.jetbrains.php.lang.psi.resolve.types.PhpType. 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: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static String getTypeDisplayName(Project project, Set<String> types) {

        Collection<PhpClass> classFromPhpTypeSet = getClassFromPhpTypeSet(project, types);
        if (classFromPhpTypeSet.size() > 0) {
            return classFromPhpTypeSet.iterator().next().getPresentableFQN();
        }

        PhpType phpType = new PhpType();
        for (String type : types) {
            phpType.add(type);
        }
        PhpType phpTypeFormatted = PhpIndex.getInstance(project).completeType(project, phpType, new HashSet<>());

        if (phpTypeFormatted.getTypes().size() > 0) {
            return StringUtils.join(phpTypeFormatted.getTypes(), "|");
        }

        if (types.size() > 0) {
            return types.iterator().next();
        }

        return "";
    }
 
Example #2
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static String getTypeDisplayName(Project project, Set<String> types) {

        Collection<PhpClass> classFromPhpTypeSet = PhpElementsUtil.getClassFromPhpTypeSet(project, types);
        if(classFromPhpTypeSet.size() > 0) {
            return classFromPhpTypeSet.iterator().next().getPresentableFQN();
        }

        PhpType phpType = new PhpType();
        for (String type : types) {
            phpType.add(type);
        }
        PhpType phpTypeFormatted = PhpIndex.getInstance(project).completeType(project, phpType, new HashSet<>());

        if(phpTypeFormatted.getTypes().size() > 0) {
            return StringUtils.join(phpTypeFormatted.getTypes(), "|");
        }

        if(types.size() > 0) {
            return types.iterator().next();
        }

        return "";

    }
 
Example #3
Source File: TemplateAnnotationTypeProvider.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (psiElement instanceof FunctionReference) {
        String subject = ((FunctionReference) psiElement).getSignature();
        String parameters = StringUtils.join(PhpTypeProviderUtil.getReferenceSignatures((FunctionReference) psiElement), PARAMETER_SEPARATOR);

        // done also by PhpStorm; is this suitable? reduce parameters maybe to limit to one on longer values?
        if (subject.length() <= 200 && parameters.length() <= 300) {
            return new PhpType().add("#" + this.getKey() + subject + '\u0198' + parameters);
        } else if(subject.length() <= 200) {
            // fallback on long parameter; to support at least some other features
            return new PhpType().add("#" + this.getKey() + subject + '\u0198');
        }
    }

    return null;
}
 
Example #4
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static Collection<PhpClass> getClassFromPhpTypeSet(Project project, Set<String> types) {

        PhpType phpType = new PhpType();
        for (String type : types) {
            phpType.add(type);
        }

        List<PhpClass> phpClasses = new ArrayList<>();

        for(String typeName: PhpIndex.getInstance(project).completeType(project, phpType, new HashSet<>()).getTypes()) {
            if(typeName.startsWith("\\")) {
                PhpClass phpClass = PhpElementsUtil.getClassInterface(project, typeName);
                if(phpClass != null) {
                    phpClasses.add(phpClass);
                }
            }
        }

        return phpClasses;
    }
 
Example #5
Source File: ServiceArgumentParameterHintsProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
private String createTypeHintFromParameter(@NotNull Project project, Parameter parameter) {
    String className = parameter.getDeclaredType().toString();
    if(PhpType.isNotExtendablePrimitiveType(className)) {
        return parameter.getName();
    }

    int i = className.lastIndexOf("\\");
    if(i > 0) {
        return className.substring(i + 1);
    }

    PhpClass expectedClass = PhpElementsUtil.getClassInterface(project, className);
    if(expectedClass != null) {
        return expectedClass.getName();
    }

    return parameter.getName();
}
 
Example #6
Source File: PhpElementsUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static Collection<PhpClass> getClassFromPhpTypeSetArrayClean(Project project, Set<String> types) {

        PhpType phpType = new PhpType();
        for (String type : types) {
            phpType.add(type);
        }

        ArrayList<PhpClass> phpClasses = new ArrayList<>();

        for(String typeName: PhpIndex.getInstance(project).completeType(project, phpType, new HashSet<>()).getTypes()) {
            if(typeName.startsWith("\\")) {

                // we clean array types \Foo[]
                if(typeName.endsWith("[]")) {
                    typeName = typeName.substring(0, typeName.length() - 2);
                }

                PhpClass phpClass = PhpElementsUtil.getClassInterface(project, typeName);
                if(phpClass != null) {
                    phpClasses.add(phpClass);
                }
            }
        }

        return phpClasses;
    }
 
Example #7
Source File: ContextTypeProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb() || !TYPO3CMSProjectSettings.isEnabled(psiElement)) {
        return null;
    }

    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "getAspect")) {
        return null;
    }

    MethodReference methodReference = (MethodReference) psiElement;
    if (methodReference.getParameters().length == 0) {
        return null;
    }

    PsiElement firstParam = methodReference.getParameters()[0];
    if (firstParam instanceof StringLiteralExpression) {
        StringLiteralExpression contextAlias = (StringLiteralExpression) firstParam;
        if (!contextAlias.getContents().isEmpty()) {
            return new PhpType().add("#" + this.getKey() + contextAlias.getContents());
        }
    }

    return null;
}
 
Example #8
Source File: ObjectRepositoryTypeProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "getRepository")) {
        return null;
    }


    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
    return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}
 
Example #9
Source File: LattePsiImplUtil.java    From intellij-latte with MIT License 6 votes vote down vote up
public static @NotNull LattePhpType getReturnType(@NotNull BaseLattePhpElement element) {
	Collection<PhpClass> phpClasses = element.getPhpType().getPhpClasses(element.getProject());
	if (phpClasses.size() == 0) {
		return LattePhpType.MIXED;
	}

	List<PhpType> types = new ArrayList<>();
	for (PhpClass phpClass : phpClasses) {
		for (Field field : phpClass.getFields()) {
			if (field.getName().equals(LattePhpUtil.normalizePhpVariable(element.getPhpElementName()))) {
				types.add(field.getType());
			}
		}
	}
	return types.size() > 0 ? LattePhpType.create(types).withDepth(element.getPhpArrayLevel()) : LattePhpType.MIXED;
}
 
Example #10
Source File: MethodArgumentDroppedMatcherInspection.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildRealVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
    return new PhpElementVisitor() {
        @Override
        public void visitPhpMethodReference(MethodReference reference) {
            ParameterList parameterList = reference.getParameterList();
            PhpExpression classReference = reference.getClassReference();
            if (classReference != null) {
                PhpType inferredType = classReference.getInferredType();
                String compiledClassMethodKey = inferredType.toString() + "->" + reference.getName();
                if (ExtensionScannerUtil.classMethodHasDroppedArguments(reference.getProject(), compiledClassMethodKey)) {
                    int maximumNumberOfArguments = ExtensionScannerUtil.getMaximumNumberOfArguments(reference.getProject(), compiledClassMethodKey);

                    if (parameterList != null && maximumNumberOfArguments != -1 && parameterList.getParameters().length != maximumNumberOfArguments) {
                        problemsHolder.registerProblem(reference, "Number of arguments changes with upcoming TYPO3 version, consider refactoring");
                    }
                }
            }

            super.visitPhpMethodReference(reference);
        }
    };
}
 
Example #11
Source File: LattePsiImplUtil.java    From intellij-latte with MIT License 6 votes vote down vote up
public static @NotNull LattePhpType getReturnType(@NotNull LattePhpMethod element) {
	LattePhpType type = element.getPhpType();
	Collection<PhpClass> phpClasses = type.getPhpClasses(element.getProject());
	String name = element.getMethodName();
	if (phpClasses.size() == 0) {
		LatteFunctionSettings customFunction = LatteConfiguration.getInstance(element.getProject()).getFunction(name);
		return customFunction == null ? LattePhpType.MIXED : LattePhpType.create(customFunction.getFunctionReturnType());
	}

	List<PhpType> types = new ArrayList<>();
	for (PhpClass phpClass : phpClasses) {
		for (Method phpMethod : phpClass.getMethods()) {
			if (phpMethod.getName().equals(name)) {
				types.add(phpMethod.getType());
			}
		}
	}
	return types.size() > 0 ? LattePhpType.create(types).withDepth(element.getPhpArrayLevel()) : LattePhpType.MIXED;
}
 
Example #12
Source File: GeneralUtilityTypeProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb() || !TYPO3CMSProjectSettings.isEnabled(psiElement)) {
        return null;
    }

    if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstance")) {
        return null;
    }

    MethodReference methodReference = (MethodReference) psiElement;

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter(methodReference, TRIM_KEY);
    return  signature == null ? null : new PhpType().add("#" + getKey() + signature);
}
 
Example #13
Source File: PhpTypeProviderUtil.java    From idea-php-laravel-plugin with MIT License 6 votes vote down vote up
private static boolean isPhpTypeEqual(@NotNull PhpType phpType, @NotNull PhpClass phpClass) {

        Symfony2InterfacesUtil util = null;

        for (String s : phpType.getTypes()) {
            if(util == null) {
                util = new Symfony2InterfacesUtil();
            }

            if(util.isInstanceOf(phpClass, s)) {
                return true;
            }
        }

        return false;
    }
 
Example #14
Source File: PhpLangUtil.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static String getClassName(@NotNull PsiElement element) {
    ParameterList parameterList = PsiTreeUtil.getParentOfType(element, ParameterList.class);
    if (parameterList == null) {
        return null;
    }

    MethodReference methodReference = PsiTreeUtil.getParentOfType(element, MethodReference.class);
    if (methodReference == null) {
        return null;
    }

    Variable variableBeingCalledOn = PsiTreeUtil.findChildOfType(methodReference, Variable.class);
    if (variableBeingCalledOn != null) {
        variableBeingCalledOn.getInferredType();
        PhpType inferredType = variableBeingCalledOn.getInferredType();
        return inferredType.toString();
    }

    ClassReference classReference = PsiTreeUtil.getChildOfType(methodReference, ClassReference.class);

    return extractFqnFromClassReference(methodReference, classReference);
}
 
Example #15
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static Collection<PhpClass> getClassFromPhpTypeSet(Project project, Set<String> types) {

        PhpType phpType = new PhpType();
        for (String type : types) {
            phpType.add(type);
        }

        List<PhpClass> phpClasses = new ArrayList<>();
        for (String typeName : PhpIndex.getInstance(project).completeType(project, phpType, new HashSet<>()).getTypes()) {
            if (typeName.startsWith("\\")) {
                PhpClass phpClass = getClassInterface(project, typeName);
                if (phpClass != null) {
                    phpClasses.add(phpClass);
                }
            }
        }

        return phpClasses;
    }
 
Example #16
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static Collection<PhpClass> getClassFromPhpTypeSetArrayClean(Project project, Set<String> types) {

        PhpType phpType = new PhpType();
        for (String type : types) {
            phpType.add(type);
        }

        ArrayList<PhpClass> phpClasses = new ArrayList<>();

        for (String typeName : PhpIndex.getInstance(project).completeType(project, phpType, new HashSet<>()).getTypes()) {
            if (typeName.startsWith("\\")) {

                // we clean array types \Foo[]
                if (typeName.endsWith("[]")) {
                    typeName = typeName.substring(0, typeName.length() - 2);
                }

                PhpClass phpClass = getClassInterface(project, typeName);
                if (phpClass != null) {
                    phpClasses.add(phpClass);
                }
            }
        }

        return phpClasses;
    }
 
Example #17
Source File: FluidTypeResolver.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
private static Collection<FluidTypeContainer> resolveFluidMethodName(Collection<FluidTypeContainer> previousElement, String typeName, Collection<List<FluidTypeContainer>> twigTypeContainer) {
    ArrayList<FluidTypeContainer> phpNamedElements = new ArrayList<>();
    for (FluidTypeContainer phpNamedElement : previousElement) {
        if (phpNamedElement.getPhpNamedElement() != null) {
            for (PhpNamedElement target : getFluidPhpNameTargets(phpNamedElement.getPhpNamedElement(), typeName)) {
                PhpType phpType = target.getType();
                for (String typeString : phpType.getTypes()) {
                    PhpNamedElement phpNamedElement1 = getClassInterface(phpNamedElement.getPhpNamedElement().getProject(), typeString);
                    if (phpNamedElement1 != null) {
                        phpNamedElements.add(new FluidTypeContainer(phpNamedElement1));
                    }
                }
            }
        }
    }

    return phpNamedElements;
}
 
Example #18
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static Collection<TwigTypeContainer> resolveTwigMethodName(Collection<TwigTypeContainer> previousElement, String typeName, Collection<List<TwigTypeContainer>> twigTypeContainer) {

        ArrayList<TwigTypeContainer> phpNamedElements = new ArrayList<>();

        for(TwigTypeContainer phpNamedElement: previousElement) {

            if(phpNamedElement.getPhpNamedElement() != null) {
                for(PhpNamedElement target : getTwigPhpNameTargets(phpNamedElement.getPhpNamedElement(), typeName)) {
                    PhpType phpType = target.getType();

                    // @TODO: provide extension
                    // custom resolving for Twig here: "app.user" => can also be a general solution just support the "getToken()->getUser()"
                    if (target instanceof Method && StaticVariableCollector.isUserMethod((Method) target)) {
                        phpNamedElements.addAll(getApplicationUserImplementations(target.getProject()));
                    }

                    // @TODO: use full resolving for object, that would allow using TypeProviders and core PhpStorm feature
                    for (String typeString: phpType.filterPrimitives().getTypes()) {
                        PhpClass phpClass = PhpElementsUtil.getClassInterface(phpNamedElement.getPhpNamedElement().getProject(), typeString);
                        if(phpClass != null) {
                            phpNamedElements.add(new TwigTypeContainer(phpClass));
                        }
                    }
                }
            }

            for(TwigTypeResolver twigTypeResolver: TWIG_TYPE_RESOLVERS) {
                twigTypeResolver.resolve(phpNamedElements, previousElement, typeName, twigTypeContainer, null);
            }

        }

        return phpNamedElements;
    }
 
Example #19
Source File: ObjectManagerFindTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "find")) {
        return null;
    }

    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    // we need the param key on getBySignature(), since we are already in the resolved method there attach it to signature
    // param can have dotted values split with \
    PsiElement[] parameters = ((MethodReference)e).getParameters();
    if (parameters.length >= 2) {
        String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
        if(signature != null) {
            return new PhpType().add("#" + this.getKey() + signature);
        }
    }

    return null;
}
 
Example #20
Source File: CreateMethodQuickFix.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
private void buildEventVariables(@NotNull final Project project, final StringBuilder stringBuilder, Method classMethod) {
    classMethod.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if ((element instanceof StringLiteralExpression) && ((StringLiteralExpression) element).getContents().equals(generatorContainer.getHookName())) {
                PsiElement parent = element.getParent();
                if(parent instanceof ParameterList) {
                    PsiElement[] parameterList = ((ParameterList) parent).getParameters();
                    if(parameterList.length > 1) {
                        if(parameterList[1] instanceof ArrayCreationExpression) {
                            Map<String, PsiElement> eventParameters = PhpElementsUtil.getArrayCreationKeyMap((ArrayCreationExpression) parameterList[1]);
                            for(Map.Entry<String, PsiElement> entrySet : eventParameters.entrySet()) {
                                stringBuilder.append("\n");
                                PhpPsiElement psiElement = PhpElementsUtil.getArrayValue((ArrayCreationExpression) parameterList[1], entrySet.getKey());
                                if(psiElement instanceof PhpTypedElement) {

                                    Set<String> classes = new HashSet<>();

                                    PhpType type = ((PhpTypedElement) psiElement).getType();
                                    for (PhpClass aClass : PhpElementsUtil.getClassFromPhpTypeSet(project, type.getTypes())) {
                                        // force absolute namespace
                                        classes.add("\\" + StringUtils.stripStart(aClass.getPresentableFQN(), "\\"));
                                    }

                                    if(classes.size() > 0) {
                                        stringBuilder.append("/** @var ").append(StringUtils.join(classes, "|")).append("$").append(entrySet.getKey()).append(" */\n");
                                    }
                                }
                                stringBuilder.append("$").append(entrySet.getKey()).append(" = ").append("$args->get('").append(entrySet.getKey()).append("');\n");
                            }
                        }
                    }
                }
            }
            super.visitElement(element);
        }
    });
}
 
Example #21
Source File: SymfonyContainerTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    // container calls are only on "get" methods
    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "get")) {
        return null;
    }

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
    return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}
 
Example #22
Source File: TwigExtensionInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private boolean isString(@NotNull PhpType type) {
    for (String s : type.getTypes()) {
        if(StringUtils.stripStart(s, "\\").equalsIgnoreCase("string")) {
            return true;
        }
    }

    return false;
}
 
Example #23
Source File: ObjectRepositoryResultTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!(e instanceof MethodReference) || !Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    MethodReference methodRef = (MethodReference) e;

    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    String methodRefName = methodRef.getName();

    if(null == methodRefName || (!Arrays.asList(new String[] {"find", "findAll"}).contains(methodRefName) && !methodRefName.startsWith("findOneBy") && !methodRefName.startsWith("findBy"))) {
        return null;
    }

    // we can get the repository name from the signature calls
    // #M#?#M#?#M#C\Foo\Bar\Controller\BarController.get?doctrine.getRepository?EntityBundle:User.find
    String repositorySignature = methodRef.getSignature();

    int lastRepositoryName = repositorySignature.lastIndexOf(ObjectRepositoryTypeProvider.TRIM_KEY);
    if(lastRepositoryName == -1) {
        return null;
    }

    repositorySignature = repositorySignature.substring(lastRepositoryName);
    int nextMethodCall = repositorySignature.indexOf('.' + methodRefName);
    if(nextMethodCall == -1) {
        return null;
    }

    repositorySignature = repositorySignature.substring(1, nextMethodCall);

    return new PhpType().add("#" + this.getKey() + refSignature + TRIM_KEY + repositorySignature);
}
 
Example #24
Source File: ObjectManagerFindContextTypeProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!(e instanceof MethodReference) || !Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    MethodReference methodRef = (MethodReference) e;

    String refSignature = ((MethodReference)e).getSignature();
    if(StringUtil.isEmpty(refSignature)) {
        return null;
    }

    String methodRefName = methodRef.getName();

    if(null == methodRefName || (!Arrays.asList(new String[] {"find", "findAll"}).contains(methodRefName) && !methodRefName.startsWith("findOneBy") && !methodRefName.startsWith("findBy"))) {
        return null;
    }

    String signature = null;
    PhpPsiElement firstPsiChild = methodRef.getFirstPsiChild();

    // reduce supported scope here; by checking via "instanceof"
    if (firstPsiChild instanceof Variable) {
        signature = ((Variable) firstPsiChild).getSignature();
    } else if(firstPsiChild instanceof PhpReference) {
        signature = ((PhpReference) firstPsiChild).getSignature();
    }

    if (signature == null || StringUtils.isBlank(signature)) {
        return null;
    }

    return new PhpType().add("#" + this.getKey() + signature + TRIM_KEY + methodRefName);
}
 
Example #25
Source File: LattePhpType.java    From intellij-latte with MIT License 5 votes vote down vote up
@NotNull
public static LattePhpType create(List<PhpType> phpTypes) {
    List<String> typesStrings = new ArrayList<>();
    for (PhpType type : phpTypes) {
        typesStrings.add(type.toString());
    }
    Set<String> temp = new LinkedHashSet<String>(
            Arrays.asList(String.join("|", typesStrings).split("\\|"))
    );
    return create(null, String.join("|", temp));
}
 
Example #26
Source File: PhpTypeProviderUtil.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private static boolean isPhpTypeEqual(@NotNull PhpType phpType, @NotNull PhpClass phpClass) {

        for (String s : phpType.getTypes()) {
            if(PhpElementsUtil.isInstanceOf(phpClass, s)) {
                return true;
            }
        }

        return false;
    }
 
Example #27
Source File: AnnotationUtil.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * Get attributes for @Foo("test", ATTR<caret>IBUTE)
 *
 * "@Attributes(@Attribute("accessControl", type="string"))"
 * "@Attributes({@Attribute("accessControl", type="string")})"
 * "class foo { public $foo; }"
 */
public static void visitAttributes(@NotNull PhpClass phpClass, TripleFunction<String, String, PsiElement, Void> fn) {
    for (Field field : phpClass.getFields()) {
        if(field.getModifier().isPublic() && !field.isConstant()) {
            String type = null;
            for (String type2 : field.getType().filterNull().getTypes()) {
                if (PhpType.isPrimitiveType(type2)) {
                    type = StringUtils.stripStart(type2, "\\");
                }
            }

            fn.fun(field.getName(), type, field);
        }
    }

    PhpDocComment docComment = phpClass.getDocComment();
    if (docComment != null) {
        for (PhpDocTag phpDocTag : docComment.getTagElementsByName("@Attributes")) {
            for (PhpDocTag docTag : PsiTreeUtil.collectElementsOfType(phpDocTag, PhpDocTag.class)) {
                String name = docTag.getName();
                if (!"@Attribute".equals(name)) {
                    continue;
                }

                String defaultPropertyValue = AnnotationUtil.getDefaultPropertyValue(docTag);
                if (defaultPropertyValue == null || StringUtils.isBlank(defaultPropertyValue)) {
                    continue;
                }

                fn.fun(defaultPropertyValue, AnnotationUtil.getPropertyValue(docTag, "type"), docTag);
            }
        }
    }
}
 
Example #28
Source File: BladeInjectTypeProvider.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement element) {
    if(!(element instanceof Variable) || !LaravelSettings.getInstance(element.getProject()).pluginEnabled){
        return null;
    }

    String name = ((Variable) element).getName();

    PsiFile bladeFile = getHostBladeFileForInjectionIfExists(element);
    if(bladeFile == null) {
        return null;
    }

    PhpType phpType = new PhpType();

    PsiTreeUtil.findChildrenOfType(bladeFile, BladePsiDirective.class).stream()
        .filter(bladePsiDirective -> "@inject".equals(bladePsiDirective.getName()))
        .forEach(bladePsiDirective -> {
            BladePsiDirectiveParameter parameter = PsiTreeUtil.findChildOfType(bladePsiDirective, BladePsiDirectiveParameter.class);
            if(parameter == null) {
                return;
            }

            List<String> strings = ContainerUtil.map(BladePsiUtil.extractParameters(parameter.getText()), PsiElementUtils::trimQuote);
            if(strings.size() > 1 && name.equals(strings.get(0))) {
                phpType.add("\\" + StringUtils.stripStart(strings.get(1), "\\"));
            }
        });

    return !phpType.isEmpty() ? phpType : null;
}
 
Example #29
Source File: DicTypeProvider.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (!LaravelSettings.getInstance(psiElement.getProject()).pluginEnabled) {
        return null;
    }

    // container calls are only on "get" methods
    if(psiElement instanceof FunctionReference && "app".equals(((FunctionReference) psiElement).getName())) {
        PsiElement[] parameters = ((FunctionReference) psiElement).getParameters();
        if(parameters.length > 0 && parameters[0] instanceof StringLiteralExpression) {
            String contents = ((StringLiteralExpression) parameters[0]).getContents();
            if(StringUtils.isNotBlank(contents)) {
                return new PhpType().add(
                    "#" + this.getKey() + ((FunctionReference) psiElement).getSignature() + TRIM_KEY + contents
                );
            }
        }
    }

    // container calls are only on "get" methods
    if(psiElement instanceof MethodReference && PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "make")) {
        String referenceSignature = PhpTypeProviderUtil.getReferenceSignature((MethodReference) psiElement, TRIM_KEY);
        if(referenceSignature != null) {
            return new PhpType().add("#" + this.getKey() + referenceSignature);
        }
    }

    return null;
}
 
Example #30
Source File: ShopwareApiResourcesTypeProvider.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public PhpType getType(PsiElement e) {
    if (!Settings.getInstance(e.getProject()).pluginEnabled) {
        return null;
    }

    // container calls are only on "get" methods
    if(!(e instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(e, "getResource")) {
        return null;
    }

    String signature = PhpTypeProviderUtil.getReferenceSignatureByFirstParameter((MethodReference) e, TRIM_KEY);
    return signature == null ? null : new PhpType().add("#" + this.getKey() + signature);
}