use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class InconsistentQueryBuildInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final String function = reference.getName();
if (function != null && function.equals("ksort")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 1) {
/* pre-condition satisfied, now check if http_build_query used in the scope */
final Function scope = ExpressionSemanticUtil.getScope(reference);
if (scope != null) {
for (final FunctionReference call : PsiTreeUtil.findChildrenOfType(scope, FunctionReference.class)) {
/* skip inspected call and calls without arguments */
if (call == reference || !OpenapiTypesUtil.isFunctionReference(call)) {
continue;
}
/* skip non-target function */
final String callFunctionName = call.getName();
if (callFunctionName == null || !callFunctionName.equals("http_build_query")) {
continue;
}
final PsiElement[] callArguments = call.getParameters();
if (callArguments.length == 0) {
continue;
}
/* pattern match: ksort and http_build_query operating on the same expression */
if (OpeanapiEquivalenceUtil.areEqual(callArguments[0], arguments[0])) {
final String message = messagePattern.replace("%a%", arguments[0].getText());
holder.registerProblem(reference, message, new TheLocalFix());
break;
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class PowerOperatorCanBeUsedInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final PhpLanguageLevel php = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
if (php.compareTo(PhpLanguageLevel.PHP560) >= 0) {
final String functionName = reference.getName();
if (functionName != null && functionName.equals("pow")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 2) {
final String replacement = (reference.getParent() instanceof BinaryExpression ? "(%b% ** %p%)" : "%b% ** %p%").replace("%p%", arguments[1] instanceof BinaryExpression ? "(%p%)" : "%p%").replace("%b%", arguments[0] instanceof BinaryExpression ? "(%b%)" : "%b%").replace("%p%", arguments[1].getText()).replace("%b%", arguments[0].getText());
holder.registerProblem(reference, String.format(messagePattern, replacement), new UseTheOperatorFix(replacement));
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class AssertInternalTypeStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
boolean result = false;
if (targetMapping.containsKey(methodName)) {
final PsiElement[] assertionArguments = reference.getParameters();
if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
final FunctionReference functionReference = (FunctionReference) assertionArguments[0];
final String functionName = functionReference.getName();
if (functionName != null && targetFunctionMapping.containsKey(functionName)) {
final PsiElement[] functionArguments = functionReference.getParameters();
if (functionArguments.length > 0) {
/* generate QF arguments */
final String suggestedAssertion = targetMapping.get(methodName);
final String suggestedType = targetFunctionMapping.get(functionName);
final String[] suggestedArguments = new String[assertionArguments.length + 1];
suggestedArguments[0] = String.format("'%s'", suggestedType);
suggestedArguments[1] = functionArguments[0].getText();
if (assertionArguments.length > 1) {
suggestedArguments[2] = assertionArguments[1].getText();
}
/* register an issue */
holder.registerProblem(reference, String.format(messagePattern, suggestedAssertion, suggestedType), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class FilePutContentsMissUseInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final String functionName = reference.getName();
if (functionName != null && functionName.equals("file_put_contents")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 2) {
/* inner call can be silenced, un-wrap it */
PsiElement innerCandidate = arguments[1];
if (innerCandidate instanceof UnaryExpression) {
final UnaryExpression unary = (UnaryExpression) innerCandidate;
if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opSILENCE)) {
innerCandidate = unary.getValue();
}
}
/* analyze the call */
if (OpenapiTypesUtil.isFunctionReference(innerCandidate)) {
final FunctionReference innerReference = (FunctionReference) innerCandidate;
final String innerName = innerReference.getName();
if (innerName != null && innerName.equals("file_get_contents")) {
final PsiElement[] innerArguments = innerReference.getParameters();
if (innerArguments.length == 1) {
final String replacement = "copy(%s%, %d%)".replace("%s%", innerArguments[0].getText()).replace("%d%", arguments[0].getText());
holder.registerProblem(reference, String.format(messagePattern, replacement), ProblemHighlightType.GENERIC_ERROR, new UseCopyFix(replacement));
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class CallableReferenceNameMismatchInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpMethodReference(@NotNull MethodReference reference) {
final String methodName = reference.getName();
if (methodName != null && !methodName.isEmpty()) {
this.inspectCaseIdentity(reference, methodName, false);
}
}
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final String functionName = reference.getName();
if (functionName != null && !functionName.isEmpty() && !cache.containsKey(functionName)) {
this.inspectCaseIdentity(reference, functionName, true);
}
}
private void inspectCaseIdentity(@NotNull FunctionReference reference, @NotNull String referenceName, boolean useCache) {
/* resolve callable and ensure the case matches */
final PsiElement resolved = OpenapiResolveUtil.resolveReference(reference);
if (resolved instanceof Function) {
final Function function = (Function) resolved;
final String realName = function.getName();
if (useCache && function.getFQN().equals('\\' + realName)) {
cache.putIfAbsent(realName, realName);
}
if (!realName.equals(referenceName) && realName.equalsIgnoreCase(referenceName)) {
holder.registerProblem(reference, messagePattern.replace("%n%", realName), new CallableReferenceNameMismatchQuickFix());
}
}
}
};
}
Aggregations