Java Code Examples for com.jetbrains.php.lang.psi.elements.Field#getName()

The following examples show how to use com.jetbrains.php.lang.psi.elements.Field#getName() . 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
/**
 * "phpNamedElement.variableName", "phpNamedElement.getVariableName" will resolve php type eg method
 *
 * @param phpNamedElement php class method or field
 * @param variableName    variable name shortcut property possible
 * @return matched php types
 */
public static Collection<? extends PhpNamedElement> getFluidPhpNameTargets(PhpNamedElement phpNamedElement, String variableName) {

    Collection<PhpNamedElement> targets = new ArrayList<>();
    if (phpNamedElement instanceof PhpClass) {

        for (Method method : ((PhpClass) phpNamedElement).getMethods()) {
            String methodName = method.getName();
            if (method.getModifier().isPublic() && (methodName.equalsIgnoreCase(variableName) || isPropertyShortcutMethodEqual(methodName, variableName))) {
                targets.add(method);
            }
        }

        for (Field field : ((PhpClass) phpNamedElement).getFields()) {
            String fieldName = field.getName();
            if (field.getModifier().isPublic() && fieldName.equalsIgnoreCase(variableName)) {
                targets.add(field);
            }
        }

    }

    return targets;
}
 
Example 2
Source File: DoctrineTypeGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public Collection<LookupElement> getLookupElements() {
    String modelNameInScope = DoctrineMetadataUtil.findModelNameInScope(getElement());
    if(modelNameInScope == null) {
        return Collections.emptyList();
    }

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

    Collection<LookupElement> lookupElements = new ArrayList<>();
    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(getProject(), modelNameInScope)) {
        for (Field field : phpClass.getFields()) {
            if(field.isConstant() || unique.contains(field.getName())) {
                continue;
            }

            String name = field.getName();
            unique.add(name);
            lookupElements.add(LookupElementBuilder.create(name).withIcon(field.getIcon()).withTypeText(phpClass.getPresentableFQN(), true));
        }
    }

    return lookupElements;
}
 
Example 3
Source File: ConstraintPropertyReference.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public ResolveResult[] multiResolve(boolean b) {

    List<PsiElement> psiElements = new ArrayList<>();

    String content = ((StringLiteralExpression) getElement()).getContents();
    for(Field field: constraintPhpClass.getFields()) {
        String name = field.getName();
        if(!field.isConstant() && field.getModifier().isPublic() && content.equals(name)) {
            psiElements.add(field);
        }
    }

    return PsiElementResolveResult.createResults(psiElements);
}
 
Example 4
Source File: TwigTypeResolveUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 *
 * "phpNamedElement.variableName", "phpNamedElement.getVariableName" will resolve php type eg method
 *
 * @param phpNamedElement php class method or field
 * @param variableName variable name shortcut property possible
 * @return matched php types
 */
public static Collection<? extends PhpNamedElement> getTwigPhpNameTargets(PhpNamedElement phpNamedElement, String variableName) {

    Collection<PhpNamedElement> targets = new ArrayList<>();
    if(phpNamedElement instanceof PhpClass) {

        for(Method method: ((PhpClass) phpNamedElement).getMethods()) {
            String methodName = method.getName();
            if(method.getModifier().isPublic() && (methodName.equalsIgnoreCase(variableName) || isPropertyShortcutMethodEqual(methodName, variableName))) {
                targets.add(method);
            }
        }

        for(Field field: ((PhpClass) phpNamedElement).getFields()) {
            String fieldName = field.getName();
            if(field.getModifier().isPublic() && fieldName.equalsIgnoreCase(variableName)) {
                targets.add(field);
            }
        }

    }

    return targets;
}
 
Example 5
Source File: ColumnNameCompletionProvider.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
public void getPropertyValueCompletions(AnnotationPropertyParameter annotationPropertyParameter, AnnotationCompletionProviderParameter completionParameter) {

    String propertyName = annotationPropertyParameter.getPropertyName();
    if(propertyName == null) {
        return;
    }

    if(propertyName.equals("name") && PhpLangUtil.equalsClassNames(annotationPropertyParameter.getPhpClass().getPresentableFQN(), "Doctrine\\ORM\\Mapping\\Column")) {
        PhpDocComment phpDocComment = PsiTreeUtil.getParentOfType(annotationPropertyParameter.getElement(), PhpDocComment.class);
        if(phpDocComment != null) {
            PhpPsiElement classField = phpDocComment.getNextPsiSibling();
            if(classField != null && classField.getNode().getElementType() == PhpElementTypes.CLASS_FIELDS) {
                Field field = PsiTreeUtil.getChildOfType(classField, Field.class);
                if(field != null) {
                    String name = field.getName();
                    if(StringUtils.isNotBlank(name)) {
                        completionParameter.getResult().addElement(LookupElementBuilder.create(underscore(name)));
                    }
                }
            }
        }
    }

}
 
Example 6
Source File: CreateInjectorQuickFix.java    From idea-php-typo3-plugin with MIT License 4 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
    PhpDocComment docCommentElement = (PhpDocComment) element.getElement().getParent();
    PsiElement owner = docCommentElement.getOwner();
    if (!(owner instanceof Field)) {
        return;
    }
    Field field = (Field) owner;

    final String fieldName = field.getName();
    final PhpClass containingClass = PsiTreeUtil.getParentOfType(element.getElement(), PhpClass.class);
    if (containingClass == null) {
        return;
    }

    PhpType type = field.getType();
    if (type == PhpType.SCALAR) {
        // Scalar type - can't inject those, heh.
        return;
    }

    final String methodName = "inject" + StringUtils.capitalize(fieldName);
    Method injectorFunction = PhpPsiElementFactory.createMethod(
            project,
            "public function " + methodName + " (" + type + " $" + fieldName + ") {\n" +
                    "  $this->" + fieldName + " = $" + fieldName + ";\n" +
                    "}\n"

    );

    final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, element.getContainingFile().getVirtualFile()), true);
    if (editor == null) {
        return;
    }

    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());

    final int insertPos = CodeUtil.getMethodInsertPosition(containingClass, methodName);
    if (insertPos == -1) {
        return;
    }

    new WriteCommandAction(project) {
        @Override
        protected void run(@NotNull Result result) {
            StringBuffer textBuf = new StringBuffer();
            textBuf.append("\n");
            textBuf.append(injectorFunction.getText());

            editor.getDocument().insertString(insertPos, textBuf);
            final int endPos = insertPos + textBuf.length();

            CodeStyleManager.getInstance(project).reformatText(containingClass.getContainingFile(), insertPos, endPos + 1);
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());

            CodeStyleManager.getInstance(project).reformatText(docCommentElement.getContainingFile(), ContainerUtil.list(docCommentElement.getTextRange()));
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());

            Method insertedMethod = containingClass.findMethodByName(methodName);
            if (insertedMethod != null) {
                editor.getCaretModel().moveToOffset(insertedMethod.getTextRange().getStartOffset());
                editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);

                element.getElement().delete();
            }
        }

        @Override
        public String getGroupID() {
            return "Create Injection Method";
        }
    }.execute();
}
 
Example 7
Source File: DoctrinePhpMappingDriver.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Override
public DoctrineMetadataModel getMetadata(@NotNull DoctrineMappingDriverArguments args) {
    PsiFile psiFile = args.getPsiFile();
    if(!(psiFile instanceof PhpFile)) {
        return null;
    }

    Collection<DoctrineModelField> fields = new ArrayList<>();
    DoctrineMetadataModel model = new DoctrineMetadataModel(fields);

    for (PhpClass phpClass : PhpElementsUtil.getClassesInterface(args.getProject(), args.getClassName())) {
        // remove duplicate code
        // @TODO: fr.adrienbrault.idea.symfony2plugin.doctrine.EntityHelper.getModelFields()
        PhpDocComment docComment = phpClass.getDocComment();
        if(docComment == null) {
            continue;
        }

        // Doctrine ORM
        // @TODO: external split
        if(AnnotationBackportUtil.hasReference(docComment, "\\Doctrine\\ORM\\Mapping\\Entity", "\\TYPO3\\Flow\\Annotations\\Entity")) {

            // @TODO: reuse annotations plugin
            PhpDocTag phpDocTag = AnnotationBackportUtil.getReference(docComment, "\\Doctrine\\ORM\\Mapping\\Table");
            if(phpDocTag != null) {
                Matcher matcher = Pattern.compile("name[\\s]*=[\\s]*[\"|']([\\w_\\\\]+)[\"|']").matcher(phpDocTag.getText());
                if (matcher.find()) {
                    model.setTable(matcher.group(1));
                }
            }

            Map<String, Map<String, String>> maps = new HashMap<>();
            for(Field field: phpClass.getFields()) {
                if (field.isConstant()) {
                    continue;
                }

                if (!AnnotationBackportUtil.hasReference(field.getDocComment(), EntityHelper.ANNOTATION_FIELDS)) {
                    continue;
                }

                // context change is case of "trait" or extends
                PhpClass containingClass = field.getContainingClass();
                if (containingClass == null) {
                    continue;
                }

                // collect import context based on the class name
                String fqn = containingClass.getFQN();
                if (!maps.containsKey(fqn)) {
                    maps.put(fqn, AnnotationUtil.getUseImportMap(field.getDocComment()));
                }

                DoctrineModelField modelField = new DoctrineModelField(field.getName());
                EntityHelper.attachAnnotationInformation(containingClass, field, modelField.addTarget(field), maps.get(fqn));
                fields.add(modelField);
            }
        }
    }

    if(fields.size() == 0) {
        return null;
    }

    return model;
}