com.jetbrains.php.lang.psi.elements.Function Java Examples

The following examples show how to use com.jetbrains.php.lang.psi.elements.Function. 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: GenericsUtilTest.java    From idea-php-generics-plugin with MIT License 6 votes vote down vote up
public void testThatReturnElementsAreExtracted() {
    Function function = PhpPsiElementFactory.createPhpPsiFromText(getProject(), Function.class, "" +
        "/**\n" +
        "* @psalm-return array{foo: Foo, ?bar: int | string}\n" +
        "* @return array{foo2: Foo, ?bar2: int | string}\n" +
        "*/" +
        "function test() {}\n"
    );

    Collection<ParameterArrayType> vars = GenericsUtil.getReturnArrayTypes(function);

    ParameterArrayType bar = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("bar")).findFirst().get();
    assertTrue(bar.isOptional());
    assertContainsElements(Arrays.asList("string", "int"), bar.getValues());

    ParameterArrayType bar2 = Objects.requireNonNull(vars).stream().filter(parameterArrayType -> parameterArrayType.getKey().equalsIgnoreCase("bar2")).findFirst().get();
    assertTrue(bar2.isOptional());
    assertContainsElements(Arrays.asList("string", "int"), bar2.getValues());
}
 
Example #2
Source File: XmlHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Find argument of given service function / method scope
 *
 * <service>
 *     <argument key="$foobar"/>
 *     <argument index="0"/>
 *     <argument/>
 * </service>
 */
public static int getArgumentIndex(@NotNull XmlTag argumentTag, @NotNull Function function) {
    String indexAttr = argumentTag.getAttributeValue("index");
    if(indexAttr != null) {
        try {
            return Integer.valueOf(indexAttr);
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    String keyAttr = argumentTag.getAttributeValue("key");
    if(keyAttr != null) {
        int parameter = PhpElementsUtil.getFunctionArgumentByName(function, StringUtils.stripStart(keyAttr, "$"));
        if(parameter >= 0) {
            return parameter;
        }
    }

    return getArgumentIndexByCount(argumentTag);
}
 
Example #3
Source File: ServiceArgumentParameterHintsProvider.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
private Pair<String, Method> getParamater(@NotNull Project project, @NotNull String aClass, @NotNull java.util.function.Function<Void, Integer> function) {
    if(StringUtils.isNotBlank(aClass)) {
        PhpClass phpClass = ServiceUtil.getResolvedClassDefinition(project, aClass);
        if(phpClass != null) {
            Method constructor = phpClass.getConstructor();
            if(constructor != null) {
                Integer argumentIndex = function.apply(null);
                if(argumentIndex >= 0) {
                    String s = attachMethodInstances(constructor, argumentIndex);
                    if(s == null) {
                        return null;
                    }

                    return Pair.create(s, constructor);
                }
            }
        }
    }

    return null;
}
 
Example #4
Source File: PhpInjectFileReferenceIndex.java    From idea-php-advanced-autocomplete with MIT License 6 votes vote down vote up
public static PhpInjectFileReference getInjectFileReference(@NotNull Project project, @NotNull Function function, int argumentIndex) {
    FileBasedIndex index = FileBasedIndex.getInstance();
    GlobalSearchScope scope = GlobalSearchScope.allScope(project);
    Ref<List<PhpInjectFileReference>> result = new Ref<>(ContainerUtil.emptyList());
    result.set(index.getValues(KEY, function.getFQN() + ":" + argumentIndex, scope));

    if (result.get().isEmpty() && function instanceof PhpClassMember) {
        PhpClassHierarchyUtils.processSuperMembers((PhpClassMember)function, (classMember, subClass, baseClass) -> {
            List<PhpInjectFileReference> values = index.getValues(KEY, classMember.getFQN() + ":" + argumentIndex, scope);
            if (values.isEmpty()) {
                return true;
            } else {
                result.set(values);
                return false;
            }
        });
    }

    if (result.get().isEmpty()) {
        return null;
    }
    return result.get().get(0);
}
 
Example #5
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForVariablesReferences() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "$var = ['foobar1' => $myVar];\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', $var);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #6
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForArrayCreation() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', ['foobar' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
}
 
Example #7
Source File: LattePhpMethodReference.java    From intellij-latte with MIT License 6 votes vote down vote up
@NotNull
public ResolveResult[] multiResolveFunction() {
    List<ResolveResult> results = new ArrayList<ResolveResult>();

    final Collection<BaseLattePhpElement> functions = LatteUtil.findFunctions(getElement().getProject(), methodName);
    for (BaseLattePhpElement function : functions) {
        results.add(new PsiElementResolveResult(function));
    }

    Collection<Function> phpFunctions = LattePhpUtil.getFunctionByName(getElement().getProject(), methodName);
    for (Function currentFunction : phpFunctions) {
        if (currentFunction.getName().equals(methodName)) {
            results.add(new PsiElementResolveResult(currentFunction));
        }
    }

    return results.toArray(new ResolveResult[results.size()]);
}
 
Example #8
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForArrayMerge() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', array_merge($var, ['foobar1' => $myVar]));\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #9
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForBinaryExpression() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', $var + ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #10
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForOperatorSelfAssignment() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render('foo.html.twig', $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #11
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForTernary() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$myVar = new \\MyVars\\MyVar();\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render(true === true ? 'foo.html.twig' : 'foo', $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #12
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForCoalesce() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$test = 'foo.html.twig'\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render($foobar ?? $test, $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #13
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForVariable() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$test = 'foo.html.twig'\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render($test, $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertContainsElements(vars.keySet(), "foobar");
    assertContainsElements(vars.keySet(), "foobar1");
}
 
Example #14
Source File: PhpMethodVariableResolveUtilTest.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * @see PhpMethodVariableResolveUtil#collectMethodVariables
 */
public void testCollectMethodVariablesForVariableWithInvalidTemplateNameString() {
    Function function = PhpPsiElementFactory.createFunction(getProject(), "function foobar() {\n" +
        "$test = 'foo.html'\n" +
        "$var['foobar'] = $myVar;\n" +
        "\n" +
        "/** @var $x \\Symfony\\Component\\Templating\\EngineInterface */\n" +
        "$x->render($test, $var += ['foobar1' => $myVar]);\n" +
        "\n" +
        "}"
    );

    Map<String, PsiVariable> vars = PhpMethodVariableResolveUtil.collectMethodVariables(function);

    assertFalse(vars.containsKey("foobar"));
}
 
Example #15
Source File: TwigUtilTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil#getTwigFileMethodUsageOnIndex
 */
public void testGetTwigFileMethodUsageOnIndex() {
    myFixture.copyFileToProject("GetTwigFileMethodUsageOnIndex.php");
    Set<Function> methods = TwigUtil.getTwigFileMethodUsageOnIndex(getProject(), Collections.singletonList("car.html.twig"));

    assertNotNull(ContainerUtil.find(methods, method -> method.getFQN().equals("\\Template\\Bar\\MyTemplate.fooAction")));
    assertNotNull(ContainerUtil.find(methods, method -> method.getFQN().equals("\\foo")));
}
 
Example #16
Source File: TwigTemplateGoToDeclarationHandlerTest.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
/**
 * @see fr.adrienbrault.idea.symfony2plugin.templating.TwigTemplateGoToDeclarationHandler
 */
public void testSimpleTestNavigationToExtension() {
    myFixture.copyFileToProject("TwigFilterExtension.php");

    assertNavigationMatch(
        TwigFileType.INSTANCE,
        "{% if foo is bar<caret>_even %}",
        PlatformPatterns.psiElement(Function.class).withName("twig_test_even")
    );

    assertNavigationMatch(
        TwigFileType.INSTANCE,
        "{% if foo is bar ev<caret>en %}",
        PlatformPatterns.psiElement(Function.class).withName("twig_test_even")
    );

    assertNavigationMatch(
        TwigFileType.INSTANCE,
        "{% if foo is b<caret>ar even %}",
        PlatformPatterns.psiElement(Function.class).withName("twig_test_even")
    );

    assertNavigationMatch(
        TwigFileType.INSTANCE,
        "{% if foo is not bar<caret>_even %}",
        PlatformPatterns.psiElement(Function.class).withName("twig_test_even")
    );

    assertNavigationMatch(
        TwigFileType.INSTANCE,
        "{% if foo is not bar ev<caret>en %}",
        PlatformPatterns.psiElement(Function.class).withName("twig_test_even")
    );
}
 
Example #17
Source File: SymfonyLightCodeInsightFixtureTestCase.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #18
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #19
Source File: TwigExtensionLookupElement.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void buildTailText(@NotNull LookupElementPresentation presentation) {
    if(this.twigExtension.getTwigExtensionType() == TwigExtensionParser.TwigExtensionType.SIMPLE_TEST) {
        return;
    }

    String signature = this.twigExtension.getSignature();
    if(signature == null) {
        return;
    }

    Collection<? extends PhpNamedElement> phpNamedElements = PhpIndex.getInstance(this.project).getBySignature(signature);
    if(phpNamedElements.size() == 0) {
        return;
    }

    PhpNamedElement function = phpNamedElements.iterator().next();
    if(function instanceof Function) {
        List<Parameter> parameters = new LinkedList<>(Arrays.asList(((Function) function).getParameters()));

        if(this.twigExtension.getOption("needs_context") != null && parameters.size() > 0) {
            parameters.remove(0);
        }

        if(this.twigExtension.getOption("needs_environment") != null && parameters.size() > 0) {
            parameters.remove(0);
        }

        presentation.setTailText(PhpPresentationUtil.formatParameters(null, parameters.toArray(new Parameter[parameters.size()])).toString(), true);
    }
}
 
Example #20
Source File: ServiceArgumentParameterHintsProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private String attachMethodInstances(@NotNull Function function, int parameterIndex) {
    Parameter[] constructorParameter = function.getParameters();
    if(parameterIndex >= constructorParameter.length) {
        return null;
    }

    Parameter parameter = constructorParameter[parameterIndex];

    return createTypeHintFromParameter(function.getProject(), parameter);
}
 
Example #21
Source File: SymfonyLightCodeInsightFixtureTestCase.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #22
Source File: DrupalLightCodeInsightFixtureTestCase.java    From idea-php-drupal-symfony2-bridge with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #23
Source File: LattePhpMethodReference.java    From intellij-latte with MIT License 5 votes vote down vote up
@Nullable
@Override
public PsiElement resolve() {
    if (((LattePhpMethod) getElement()).isFunction()) {
        List<Function> phpFunctions = new ArrayList<>(LattePhpUtil.getFunctionByName(
                getElement().getProject(),
                ((LattePhpMethod) getElement()).getMethodName()
        ));
        return phpFunctions.size() > 0 ? phpFunctions.get(0) : null;

    } else {
        List<Method> phpMethods = LattePhpUtil.getMethodsForPhpElement((LattePhpMethod) getElement());
        return phpMethods.size() > 0 ? phpMethods.get(0) : null;
    }
}
 
Example #24
Source File: MethodUsagesInspection.java    From intellij-latte with MIT License 5 votes vote down vote up
private void processFunction(
		LattePhpMethod element,
		@NotNull List<ProblemDescriptor> problems,
		@NotNull final InspectionManager manager,
		final boolean isOnTheFly
) {
	String name = element.getMethodName();
	if (name == null) {
		return;
	}

	LatteFunctionSettings customFunction = LatteConfiguration.getInstance(element.getProject()).getFunction(name);
	if (customFunction != null) {
		return;
	}

	Collection<Function> existing = LattePhpUtil.getFunctionByName(element.getProject(), name);
	if (existing.size() == 0) {
		LocalQuickFix addFunctionFix = IntentionManager.getInstance().convertToFix(new AddCustomLatteFunction(name));
		addProblem(manager, problems, getElementToLook(element), "Function '" + name + "' not found", isOnTheFly, addFunctionFix);

	} else {
		for (Function function : existing) {
			if (function.isDeprecated()) {
				addDeprecated(manager, problems, getElementToLook(element), "Function '" + name + "' is deprecated", isOnTheFly);
			}
			if (function.isInternal()) {
				addDeprecated(manager, problems, getElementToLook(element), "Function '" + name + "' is internal", isOnTheFly);
			}
		}
	}
}
 
Example #25
Source File: ShopwareLightCodeInsightFixtureTestCase.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #26
Source File: LaravelLightCodeInsightFixtureTestCase.java    From idea-php-laravel-plugin with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #27
Source File: SymfonyLightCodeInsightFixtureTestCase.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }
 
Example #28
Source File: TemplateAnnotationIndex.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(@NotNull PsiElement element) {
    if (element instanceof PhpClass) {
        visitPhpClass((PhpClass) element);
    } else if (element instanceof Function) {
        visitPhpFunctionOrMethod((Function) element);
    }

    super.visitElement(element);
}
 
Example #29
Source File: UserFuncReferenceTest.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public void testReferenceCanResolveDefinition() {
    myFixture.copyFileToProject("classes.php");

    assertResolvesTo(
            "bar.php",
            "<?php \n['userFunc' => 'Foo\\Bar->q<caret>ux'];",
            UserFuncReference.class,
            Method.class,
            "A class method can be resolved"
    );

    assertResolvesTo(
            "bar.php",
            "<?php \n['userFunc' => 'coun<caret>t'];",
            UserFuncReference.class,
            Function.class,
            "A global function can be resolved"
    );

    assertResolvesTo(
            "bar.php",
            "<?php \n['userFunc' => 'user_calc_my_stuf<caret>f'];",
            UserFuncReference.class,
            Function.class,
            "A global function can be resolved"
    );

    assertResolvesTo(
            "bar.php",
            "<?php \n['userFunc' => Foo\\Bar::class . '->q<caret>ux'];",
            UserFuncReference.class,
            Method.class,
            "A class method can be resolved through concatenation"
    );
}
 
Example #30
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-generics-plugin with MIT License 5 votes vote down vote up
public void assertNavigationContains(PsiElement psiElement, String targetShortcut) {

        if(!targetShortcut.startsWith("\\")) {
            targetShortcut = "\\" + targetShortcut;
        }

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

        for (GotoDeclarationHandler gotoDeclarationHandler : Extensions.getExtensions(GotoDeclarationHandler.EP_NAME)) {
            PsiElement[] gotoDeclarationTargets = gotoDeclarationHandler.getGotoDeclarationTargets(psiElement, 0, myFixture.getEditor());
            if(gotoDeclarationTargets != null && gotoDeclarationTargets.length > 0) {

                for (PsiElement gotoDeclarationTarget : gotoDeclarationTargets) {
                    if(gotoDeclarationTarget instanceof Method) {

                        String meName = ((Method) gotoDeclarationTarget).getName();

                        String clName = ((Method) gotoDeclarationTarget).getContainingClass().getPresentableFQN();
                        if(!clName.startsWith("\\")) {
                            clName = "\\" + clName;
                        }

                        classTargets.add(clName + "::" + meName);
                    } else if(gotoDeclarationTarget instanceof Function) {
                        classTargets.add("\\" + ((Function) gotoDeclarationTarget).getName());
                    }
                }

            }
        }

        if(!classTargets.contains(targetShortcut)) {
            fail(String.format("failed that PsiElement (%s) navigate to %s on %s", psiElement.toString(), targetShortcut, classTargets.toString()));
        }

    }