use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.
the class AssertStringEqualsFileStrategy 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 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
final FunctionReference candidate = (FunctionReference) assertionArguments[0];
final String functionName = candidate.getName();
if (functionName != null && functionName.equals("file_get_contents")) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length == 1) {
final Function scope = ExpressionSemanticUtil.getScope(reference);
final boolean shouldReport = scope == null || !validContexts.contains(scope.getName());
if (shouldReport) {
final String[] suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = functionArguments[0].getText();
suggestedArguments[1] = assertionArguments[1].getText();
if (assertionArguments.length > 2) {
suggestedArguments[2] = assertionArguments[2].getText();
}
final String suggestedAssertion = "assertStringEqualsFile";
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
}
}
return result;
}
use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.
the class AssertSameStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
boolean result = false;
if (targetMapping.containsKey(methodName)) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length > 1 && isPrimitiveScalar(holder.getProject(), arguments[1]) && isPrimitiveScalar(holder.getProject(), arguments[0])) {
final String suggestedAssertion = targetMapping.get(methodName);
final String[] suggestedArguments = new String[arguments.length];
Arrays.stream(arguments).map(PsiElement::getText).collect(Collectors.toList()).toArray(suggestedArguments);
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
return result;
}
use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.
the class ExpectsOnceStrategy method apply.
public static void apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
if (methodName.equals("expects")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 1 && arguments[0] instanceof MethodReference) {
final MethodReference innerReference = (MethodReference) arguments[0];
final String innerMethodName = innerReference.getName();
if (innerMethodName != null && innerMethodName.equals("exactly")) {
final PsiElement[] innerArguments = innerReference.getParameters();
if (innerArguments.length == 1 && OpenapiTypesUtil.isNumber(innerArguments[0])) {
final boolean isResult = innerArguments[0].getText().equals("1");
if (isResult) {
holder.registerProblem(innerReference, MessagesPresentationUtil.prefixWithEa(message), new PhpUnitAssertFixer("once", new String[] {}));
}
}
}
}
}
}
use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.
the class AssertConstantStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
boolean result = false;
if (targetMapping.containsKey(methodName)) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length > 1) {
for (final PsiElement argument : arguments) {
if (argument instanceof ConstantReference) {
final String constantName = ((ConstantReference) argument).getName();
if (constantName != null) {
final String constantNameNormalized = constantName.toLowerCase();
if (targetConstants.contains(constantNameNormalized)) {
final String suggestedAssertion = String.format(targetMapping.get(methodName), StringUtils.capitalize(constantNameNormalized));
final String[] suggestedArguments = new String[arguments.length - 1];
suggestedArguments[0] = Arrays.stream(arguments).filter(a -> a != argument).findFirst().get().getText();
if (arguments.length > 2) {
suggestedArguments[1] = arguments[2].getText();
}
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
break;
}
}
}
}
}
}
return result;
}
use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer 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;
}
Aggregations