use of com.jetbrains.php.PhpIndex in project phpinspectionsea by kalessil.
the class ComparableCoreClassesStrategy method apply.
public static boolean apply(@Nullable PsiElement leftOperand, @Nullable PsiElement rightOperand, ProblemsHolder holder) {
/* validate parameters and prepare needed objects */
if (null == leftOperand || null == rightOperand) {
return false;
}
final Function scope = ExpressionSemanticUtil.getScope(leftOperand);
if (null == scope) {
return false;
}
final PhpIndex index = PhpIndex.getInstance(holder.getProject());
return isComparableObject(leftOperand, index) && isComparableObject(rightOperand, index);
}
use of com.jetbrains.php.PhpIndex in project yii2support by nvlad.
the class ViewUtil method resolveView.
@Nullable
public static ViewResolve resolveView(PsiElement element) {
String value = PhpUtil.getValue(element);
if (value.startsWith("@")) {
ViewResolve resolve = new ViewResolve(value);
resolve.application = YiiApplicationUtils.getApplicationName(element.getContainingFile());
return resolve;
}
if (value.startsWith("//")) {
ViewResolve resolve = new ViewResolve("@app/views" + value.substring(1));
resolve.application = YiiApplicationUtils.getApplicationName(element.getContainingFile());
return resolve;
}
final MethodReference method = PsiTreeUtil.getParentOfType(element, MethodReference.class);
if (method == null || method.getClassReference() == null) {
return null;
}
PhpClass callerClass = ClassUtils.getPhpClassByCallChain(method);
if (callerClass == null) {
return null;
}
final PhpIndex phpIndex = PhpIndex.getInstance(element.getProject());
final ViewResolve viewResolve;
try {
if (callerClass.getName().endsWith("Controller") && ClassUtils.isClassInheritsOrEqual(callerClass, "\\yii\\base\\Controller", phpIndex)) {
viewResolve = resolveViewFromController(callerClass, value);
} else if (ClassUtils.isClassInheritsOrEqual(callerClass, "\\yii\\base\\View", phpIndex)) {
viewResolve = resolveViewFromView(element, value);
} else if (ClassUtils.isClassInheritsOrEqual(callerClass, "\\yii\\base\\Widget", phpIndex)) {
viewResolve = resolveViewFromWidget(callerClass, value);
} else {
return null;
}
} catch (InvalidPathException e) {
return null;
}
viewResolve.application = YiiApplicationUtils.getApplicationName(element.getContainingFile());
return viewResolve;
}
use of com.jetbrains.php.PhpIndex in project yii2support by nvlad.
the class QueryCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
MethodReference methodRef = ClassUtils.getMethodRef(completionParameters.getPosition(), 10);
if (methodRef != null) {
String prefix = completionResultSet.getPrefixMatcher().getPrefix();
char[] prefixes = { ' ', ',', '.', '[', '{', '%', '(' };
completionResultSet = adjustPrefixes(prefixes, completionResultSet);
Method method = (Method) methodRef.resolve();
if (method == null) {
return;
}
int paramPosition = ClassUtils.indexForElementInParameterList(completionParameters.getPosition());
PhpClass phpClass = method.getContainingClass();
if (phpClass == null)
return;
PhpIndex index = PhpIndex.getInstance(method.getProject());
if ((ClassUtils.isClassInheritsOrEqual(phpClass, "\\yii\\db\\Query", index) || ClassUtils.isClassInheritsOrEqual(phpClass, "\\yii\\db\\QueryTrait", index) || ClassUtils.isClassInherit(phpClass, "\\yii\\db\\BaseActiveRecord", index) || ClassUtils.isClassInheritsOrEqual(phpClass, "\\yii\\db\\Connection", index) || ClassUtils.isClassInheritsOrEqual(phpClass, "\\yii\\db\\Command", index) || ClassUtils.isClassInheritsOrEqual(phpClass, "\\yii\\db\\Migration", index))) {
PhpClass activeRecordClass = null;
PhpClass possibleActiveRecordClass = ClassUtils.getPhpClassByCallChain(methodRef);
if (ClassUtils.isClassInherit(possibleActiveRecordClass, ClassUtils.getClass(index, "\\yii\\db\\BaseActiveRecord")))
activeRecordClass = possibleActiveRecordClass;
else // Calls inside ActiveQuery paired with ActiveRecord
if (ClassUtils.isClassInheritsOrEqual(possibleActiveRecordClass, ClassUtils.getClass(index, "\\yii\\db\\ActiveQuery"))) {
if (possibleActiveRecordClass.getDocComment() != null) {
activeRecordClass = ClassUtils.findClassInSeeTags(index, possibleActiveRecordClass, "\\yii\\db\\BaseActiveRecord");
}
}
/*----- ActiveQuery condition and column paramters ----*/
Project project = completionParameters.getPosition().getProject();
if (activeRecordClass != null && paramPosition >= 0 && method.getParameters().length > paramPosition && method.getParameters().length > 0 && (method.getParameters()[paramPosition].getName().equals("condition") || method.getParameters()[paramPosition].getName().equals("link") || method.getParameters()[paramPosition].getName().equals("sql") || method.getParameters()[paramPosition].getName().equals("on") || method.getParameters()[paramPosition].getName().equals("attributes") || method.getParameters()[paramPosition].getName().startsWith("column"))) {
String tableName = null;
// Override activeRecord and tableName for hasOne, hasMany and viaTable method
if ((method.getName().equals("hasOne") || method.getName().equals("hasMany") || method.getName().equals("viaTable")) && method.getParameters()[paramPosition].getName().equals("link") && (completionParameters.getPosition().getParent().getParent().toString().equals("Array key") || completionParameters.getPosition().getParent().getParent().getParent() instanceof ArrayCreationExpression) && paramPosition > 0) {
if (methodRef.getParameters()[paramPosition - 1] instanceof StringLiteralExpression) {
activeRecordClass = null;
tableName = ClassUtils.removeQuotes(methodRef.getParameters()[paramPosition - 1].getText());
} else {
activeRecordClass = ClassUtils.getPhpClassUniversal(project, (PhpPsiElement) methodRef.getParameters()[paramPosition - 1]);
}
}
if (activeRecordClass != null)
tableName = getTable(prefix, activeRecordClass);
if (tableName == null || tableName.isEmpty())
return;
ArrayList<LookupElementBuilder> lookups = DatabaseUtils.getLookupItemsByTable(tableName, project, (PhpExpression) completionParameters.getPosition().getParent());
if (lookups != null && !lookups.isEmpty()) {
// columns
addAllElementsWithPriority(lookups, completionResultSet, 2, true);
} else {
ArrayList<LookupElementBuilder> items = DatabaseUtils.getLookupItemsByAnnotations(activeRecordClass, (PhpExpression) completionParameters.getPosition().getParent());
// fields
addAllElementsWithPriority(items, completionResultSet, 2, true);
}
if (!isTabledPrefix(prefix)) {
lookups = DatabaseUtils.getLookupItemsTables(project, (PhpExpression) completionParameters.getPosition().getParent());
if (lookups != null && lookups.size() == 0)
lookups = DatabaseUtils.getLookupItemsByAnnotations(activeRecordClass, (PhpExpression) completionParameters.getPosition().getParent());
// tables
addAllElementsWithPriority(lookups, completionResultSet, 1);
}
/*--- table parameter -----*/
} else if (method.getParameters().length > paramPosition && method.getParameters().length > 0 && (method.getParameters()[paramPosition].getName().startsWith("table") || method.getParameters()[paramPosition].getName().startsWith("refTable"))) {
// cancel codecompletion in case of "table" have ,
if (method.getParameters()[paramPosition].getName().equals("table") && methodRef.getParameters().length > paramPosition) {
PsiElement element = methodRef.getParameters()[paramPosition];
String content = ClassUtils.getStringByElement(element);
if (content.indexOf(',') >= 0)
return;
}
if (!isTabledPrefix(prefix)) {
ArrayList<LookupElementBuilder> lookups = DatabaseUtils.getLookupItemsTables(project, (PhpExpression) completionParameters.getPosition().getParent());
// tables
addAllElementsWithPriority(lookups, completionResultSet, 1);
}
/*--- Query & Command -----*/
} else if (activeRecordClass == null && method.getParameters().length > paramPosition && (method.getParameters()[paramPosition].getName().equals("condition") || method.getParameters()[paramPosition].getName().startsWith("column") || method.getParameters()[paramPosition].getName().startsWith("refColumn") || method.getParameters()[paramPosition].getName().startsWith("sql"))) {
ArrayList<LookupElementBuilder> lookups = null;
PhpExpression expr = (PhpExpression) completionParameters.getPosition().getParent();
if ((ClassUtils.isClassInheritsOrEqual(phpClass, ClassUtils.getClass(index, "\\yii\\db\\Command")) || ClassUtils.isClassInheritsOrEqual(phpClass, ClassUtils.getClass(index, "\\yii\\db\\Migration"))) && paramPosition > 0) {
PsiElement paramRef = methodRef.getParameters()[paramPosition - 1];
Parameter param = method.getParameters()[paramPosition - 1];
if (param.getName().equals("table") || param.getName().equals("refTable")) {
String table = paramRef.getText();
if (table != null) {
table = ClassUtils.removeQuotes(table);
lookups = DatabaseUtils.getLookupItemsByTable(table, project, expr);
}
}
} else if (isTabledPrefix(prefix)) {
String table = getTable(prefix, null);
lookups = DatabaseUtils.getLookupItemsByTable(table, project, expr);
} else {
lookups = DatabaseUtils.getLookupItemsTables(project, expr);
}
// tables
addAllElementsWithPriority(lookups, completionResultSet, 1);
}
}
}
}
use of com.jetbrains.php.PhpIndex in project yii2support by nvlad.
the class UndetectableTableInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpClass(PhpClass clazz) {
PhpIndex index = PhpIndex.getInstance(problemsHolder.getProject());
if (DatabaseUtils.HasConnections(problemsHolder.getProject()) && ClassUtils.isClassInheritsOrEqual(clazz, ClassUtils.getClass(index, "\\yii\\db\\ActiveRecord"))) {
String table = DatabaseUtils.getTableByActiveRecordClass(clazz);
if (table == null) {
problemsHolder.registerProblem(clazz.getFirstChild(), "Can not detect database table for class " + clazz.getFQN(), ProblemHighlightType.WEAK_WARNING);
} else if (!DatabaseUtils.isTableExists(table, problemsHolder.getProject())) {
problemsHolder.registerProblem(clazz.getFirstChild(), "Table '" + table + "' not found in database connections", ProblemHighlightType.WEAK_WARNING);
}
}
super.visitPhpClass(clazz);
}
};
}
use of com.jetbrains.php.PhpIndex in project yii2support by nvlad.
the class PropertiesInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof PhpDocComment && DatabaseUtils.HasConnections(element.getProject())) {
PhpDocComment docComment = (PhpDocComment) element;
PhpIndex index = PhpIndex.getInstance(element.getProject());
PhpClass phpClass = DatabaseUtils.getClassByClassPhpDoc(docComment);
if (phpClass != null && ClassUtils.isClassInheritsOrEqual(phpClass, ClassUtils.getClass(index, "\\yii\\db\\BaseActiveRecord"))) {
Collection<Field> fields = phpClass.getFields();
String table = DatabaseUtils.getTableByActiveRecordClass(phpClass);
ArrayList<VirtualProperty> notDeclaredColumns = DatabaseUtils.getNotDeclaredColumns(table, fields, element.getProject());
if (notDeclaredColumns.size() > 0) {
MissingPropertiesQuickFix qFix = new MissingPropertiesQuickFix(notDeclaredColumns, docComment);
String str1 = notDeclaredColumns.size() > 1 ? "properties" : "property";
problemsHolder.registerProblem(docComment, "Class " + phpClass.getFQN() + " is missing " + notDeclaredColumns.size() + " " + str1 + " that corresponds to database columns", ProblemHighlightType.WEAK_WARNING, qFix);
}
ArrayList<PhpDocPropertyTag> unusedProperties = DatabaseUtils.getUnusedProperties(table, docComment.getPropertyTags(), phpClass);
if (unusedProperties.size() > 0) {
for (PhpDocPropertyTag tag : unusedProperties) {
problemsHolder.registerProblem(tag, "Property is unused in class " + phpClass.getFQN(), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
}
}
}
}
super.visitElement(element);
}
};
}
Aggregations