Search in sources :

Example 1 with ClassConstImpl

use of com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl in project yii2support by nvlad.

the class DatabaseUtils method getTableByActiveRecordClass.

@Nullable
public static String getTableByActiveRecordClass(PhpClass phpClass) {
    Method method = phpClass.findMethodByName("tableName");
    if (method != null) {
        Collection<PhpReturn> returns = PsiTreeUtil.findChildrenOfType(method, PhpReturn.class);
        for (PhpReturn element : returns) {
            if ((element).getChildren().length > 0) {
                if ((element).getChildren()[0] instanceof ClassConstantReference) {
                    PsiElement resolved = ((ClassConstantReference) (element).getChildren()[0]).resolve();
                    if (resolved != null && resolved instanceof ClassConstImpl) {
                        ClassConstImpl constant = (ClassConstImpl) resolved;
                        if (constant.getChildren().length > 0) {
                            String table = ((StringLiteralExpressionImpl) constant.getChildren()[0]).getContents();
                            return AddTablePrefix(table, false, phpClass.getProject());
                        }
                    }
                } else if ((element).getChildren()[0] instanceof StringLiteralExpression) {
                    String table = (element).getChildren()[0].getText();
                    return AddTablePrefix(table, false, phpClass.getProject());
                }
            }
        }
    }
    String table = StringUtils.CamelToId(phpClass.getName());
    if (method != null && method.getContainingClass() != null && method.getContainingClass().getFQN().equals("\\yii\\db\\ActiveRecord")) {
        table = "{{%" + table + "}}";
    }
    return AddTablePrefix(table, false, phpClass.getProject());
}
Also used : StringLiteralExpressionImpl(com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl) ClassConstImpl(com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 ClassConstImpl (com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl)1 StringLiteralExpressionImpl (com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl)1 Nullable (org.jetbrains.annotations.Nullable)1