use of com.jetbrains.php.lang.psi.elements.MethodReference in project phpinspectionsea by kalessil.
the class AssertFileEqualsStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
boolean result = false;
if (targetAssertions.contains(methodName)) {
final PsiElement[] assertionArguments = reference.getParameters();
if (assertionArguments.length > 1) {
/* try extracting file_get_contents arguments */
final List<PsiElement> extracts = Arrays.stream(assertionArguments).map(argument -> {
PsiElement mappingResult = null;
if (OpenapiTypesUtil.isFunctionReference(argument)) {
final FunctionReference candidate = (FunctionReference) argument;
final String functionName = candidate.getName();
if (functionName != null && functionName.equals("file_get_contents")) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length == 1) {
mappingResult = functionArguments[0];
}
}
}
return mappingResult;
}).collect(Collectors.toList());
/* now check if reporting is needed */
if (extracts.size() > 1) {
String[] suggestedArguments = null;
if (methodName.equals("assertStringEqualsFile") && extracts.get(1) != null) {
suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = assertionArguments[0].getText();
suggestedArguments[1] = extracts.get(1).getText();
if (assertionArguments.length > 2) {
suggestedArguments[2] = assertionArguments[2].getText();
}
} else if (extracts.get(0) != null && extracts.get(1) != null) {
suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = extracts.get(0).getText();
suggestedArguments[1] = extracts.get(1).getText();
if (assertionArguments.length > 2) {
suggestedArguments[2] = assertionArguments[2].getText();
}
}
if (suggestedArguments != null) {
final String suggestedAssertion = "assertFileEquals";
holder.registerProblem(reference, String.format(messagePattern, suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
extracts.clear();
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.MethodReference in project phpinspectionsea by kalessil.
the class PhpUnitAssertFixer method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement expression = descriptor.getPsiElement();
if (expression instanceof MethodReference && !project.isDisposed()) {
final MethodReference assertion = (MethodReference) expression;
final String pattern = String.format("pattern(%s)", String.join(", ", this.arguments));
final PsiElement donor = PhpPsiElementFactory.createFunctionReference(project, pattern).getParameterList();
final PsiElement socket = assertion.getParameterList();
if (donor != null && socket != null) {
socket.replace(donor);
assertion.handleElementRename(this.suggestedAssertion);
}
}
}
use of com.jetbrains.php.lang.psi.elements.MethodReference in project yii2support by nvlad.
the class MethodUtils method paramIndexByRef.
public static int paramIndexByRef(PsiElement paramRef) {
if (paramRef.getParent().getParent() instanceof MethodReference) {
MethodReference ref = (MethodReference) paramRef.getParent().getParent();
PsiElement[] parameters = ref.getParameters();
for (int i = 0; i < parameters.length; i++) {
PsiElement parameter = parameters[i];
if (parameter == paramRef)
return i;
}
}
return -1;
}
use of com.jetbrains.php.lang.psi.elements.MethodReference in project yii2support by nvlad.
the class CompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
final PsiElement psiElement = completionParameters.getPosition();
final MethodReference method = PsiTreeUtil.getParentOfType(psiElement, MethodReference.class);
if (method == null || method.getParameters().length == 0) {
return;
}
if (!ViewUtil.isValidRenderMethod(method)) {
return;
}
PsiElement viewParameter = psiElement;
while (viewParameter != null && !(viewParameter.getParent() instanceof ParameterList)) {
viewParameter = viewParameter.getParent();
}
if (viewParameter == null || !viewParameter.equals(method.getParameters()[0])) {
return;
}
final ViewResolve resolve = ViewUtil.resolveView(viewParameter);
if (resolve == null) {
return;
}
final Project project = psiElement.getProject();
final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
int prefixLength = resolve.key.length();
int lastSlashPosition = resolve.key.lastIndexOf('/');
if (lastSlashPosition != -1 && !resolve.key.endsWith("/")) {
prefixLength = lastSlashPosition + 1;
}
if (!completionParameters.isAutoPopup()) {
if (completionResultSet.getPrefixMatcher().getPrefix().startsWith("@") && lastSlashPosition == -1) {
final String prefix = completionResultSet.getPrefixMatcher().getPrefix();
completionResultSet = completionResultSet.withPrefixMatcher(prefix.substring(1));
prefixLength = 1;
} else {
completionResultSet = completionResultSet.withPrefixMatcher(resolve.key.substring(prefixLength));
}
}
if (completionResultSet.getPrefixMatcher().getPrefix().equals("@")) {
completionResultSet = completionResultSet.withPrefixMatcher("");
}
final String prefixFilter = resolve.key.substring(0, prefixLength);
final Set<String> keys = new HashSet<>();
fileBasedIndex.processAllKeys(ViewFileIndex.identity, key -> {
if (key.startsWith(prefixFilter)) {
keys.add(key);
}
return true;
}, scope, null);
final PsiManager psiManager = PsiManager.getInstance(project);
boolean localViewSearch = false;
if (resolve.from == ViewResolveFrom.View) {
final String value = PhpUtil.getValue(viewParameter);
localViewSearch = !value.startsWith("@") && !value.startsWith("//");
}
final String defaultViewExtension = '.' + Yii2SupportSettings.getInstance(psiElement.getProject()).defaultViewExtension;
for (String key : keys) {
Collection<ViewInfo> views = fileBasedIndex.getValues(ViewFileIndex.identity, key, scope);
for (ViewInfo view : views) {
if (!resolve.application.equals(view.application)) {
continue;
}
if (localViewSearch && !resolve.theme.equals(view.theme)) {
continue;
}
PsiFile psiFile = psiManager.findFile(view.getVirtualFile());
if (psiFile != null) {
String insertText = key.substring(prefixLength);
if (insertText.endsWith(defaultViewExtension)) {
insertText = insertText.substring(0, insertText.length() - defaultViewExtension.length());
}
completionResultSet.addElement(new ViewLookupElement(psiFile, insertText));
break;
} else {
System.out.println(view.fileUrl + " => not exists");
}
}
}
}
use of com.jetbrains.php.lang.psi.elements.MethodReference in project yii2support by nvlad.
the class MissedViewInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpMethodReference(MethodReference reference) {
if (!ViewUtil.isValidRenderMethod(reference)) {
return;
}
if (ArrayUtil.contains(reference.getName(), ViewUtil.renderMethods)) {
if (reference.getParameters().length > 0) {
final PsiElement pathParameter = reference.getParameters()[0];
final ViewResolve resolve = ViewUtil.resolveView(pathParameter);
if (resolve == null) {
return;
}
String key = resolve.key;
if (Files.getFileExtension(key).isEmpty()) {
key = key + '.' + Yii2SupportSettings.getInstance(reference.getProject()).defaultViewExtension;
}
final Project project = reference.getProject();
final Collection<ViewInfo> views = FileBasedIndex.getInstance().getValues(ViewFileIndex.identity, key, GlobalSearchScope.projectScope(project));
final String application = YiiApplicationUtils.getApplicationName(reference.getContainingFile());
final boolean localViewSearch;
final String value = PhpUtil.getValue(pathParameter);
if (resolve.from == ViewResolveFrom.View) {
localViewSearch = !value.startsWith("@") && !value.startsWith("//");
} else {
localViewSearch = false;
}
views.removeIf(view -> {
if (!application.equals(view.application)) {
return true;
}
return localViewSearch && !resolve.theme.equals(view.theme);
});
if (views.size() != 0) {
return;
}
if (pathParameter instanceof StringLiteralExpression) {
Collection<String> paths = ViewUtil.viewResolveToPaths(resolve, project);
if (!paths.iterator().hasNext()) {
return;
}
VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
if (yiiRoot == null) {
return;
}
int projectUrlLength = project.getBaseDir().getUrl().length();
String yiiRootUrl = yiiRoot.getUrl();
String path;
if (projectUrlLength > yiiRootUrl.length()) {
path = paths.iterator().next();
} else {
path = yiiRootUrl.substring(projectUrlLength) + paths.iterator().next();
}
final String viewNotFoundMessage = "View file for \"" + value + "\" not found in \"" + path + "\".";
final MissedViewLocalQuickFix quickFix = new MissedViewLocalQuickFix(value, path, RenderUtil.getViewArguments(reference));
final PsiElement stringPart = pathParameter.findElementAt(1);
if (stringPart != null) {
problemsHolder.registerProblem(stringPart, viewNotFoundMessage, quickFix);
}
}
}
}
}
};
}
Aggregations