use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.
the class AssertInstanceOfStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
boolean result = false;
if (binaryTargetMapping.containsKey(methodName)) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length > 0 && arguments[0] instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) arguments[0];
if (binary.getOperationType() == PhpTokenTypes.kwINSTANCEOF) {
final PsiElement subject = binary.getLeftOperand();
final PsiElement clazz = binary.getRightOperand();
if (subject != null && clazz != null) {
/* prepare class definition which can be used for QF-ing */
String classDefinition = clazz.getText();
if (clazz instanceof ClassReference) {
if (PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP550)) {
classDefinition = clazz.getText() + "::class";
} else {
final String fqn = ((ClassReference) clazz).getFQN();
if (fqn != null) {
classDefinition = '\'' + fqn.replaceAll("\\\\", "\\\\\\\\") + '\'';
}
}
}
/* report and provide QF */
final String suggestedAssertion = binaryTargetMapping.get(methodName);
final String[] suggestedArguments = new String[arguments.length + 1];
suggestedArguments[0] = classDefinition;
suggestedArguments[1] = subject.getText();
if (arguments.length > 1) {
suggestedArguments[2] = arguments[1].getText();
}
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
} else if (getClassTargetMapping.containsKey(methodName)) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length >= 2) {
final PsiElement literal = arguments[0] instanceof StringLiteralExpression ? arguments[0] : arguments[1];
if (literal instanceof StringLiteralExpression) {
final StringLiteralExpression string = (StringLiteralExpression) literal;
final String contents = string.getContents();
if (string.getFirstPsiChild() == null && contents.length() > 3) {
final PsiElement call = literal == arguments[0] ? arguments[1] : arguments[0];
if (OpenapiTypesUtil.isFunctionReference(call)) {
final FunctionReference candidate = (FunctionReference) call;
final String functionName = candidate.getName();
if (functionName != null && functionName.equals("get_class")) {
final PsiElement[] innerArguments = candidate.getParameters();
if (innerArguments.length == 1) {
/* prepare class definition which can be used for QF-ing */
final String fqn = '\\' + contents.replaceAll("\\\\\\\\", "\\\\");
final String classDefinition;
if (PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP550)) {
classDefinition = fqn + "::class";
} else {
classDefinition = '\'' + fqn.replaceAll("\\\\", "\\\\\\\\") + '\'';
}
/* report and provide QF */
final String suggestedAssertion = getClassTargetMapping.get(methodName);
final String[] suggestedArguments = new String[arguments.length];
suggestedArguments[0] = classDefinition;
suggestedArguments[1] = innerArguments[0].getText();
if (arguments.length > 2) {
suggestedArguments[2] = arguments[2].getText();
}
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 AssertRegexStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
boolean result = false;
if (numberCompareTargets.containsKey(methodName)) {
final PsiElement[] assertionArguments = reference.getParameters();
if (assertionArguments.length >= 2 && OpenapiTypesUtil.isNumber(assertionArguments[0])) {
final boolean isTarget = OpenapiTypesUtil.isFunctionReference(assertionArguments[1]);
if (isTarget) {
final FunctionReference candidate = (FunctionReference) assertionArguments[1];
final String candidateName = candidate.getName();
if (candidateName != null && candidateName.equals("preg_match")) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length == 2) {
final String suggestedAssertion = assertionArguments[0].getText().equals(numberCompareTargets.get(methodName)) ? "assertRegExp" : "assertNotRegExp";
final String[] suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = functionArguments[0].getText();
suggestedArguments[1] = functionArguments[1].getText();
if (assertionArguments.length > 2) {
suggestedArguments[2] = assertionArguments[2].getText();
}
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
}
} else if (binaryTargets.contains(methodName)) {
final PsiElement[] assertionArguments = reference.getParameters();
if (assertionArguments.length > 0 && assertionArguments[0] instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) assertionArguments[0];
if (binary.getOperationType() == PhpTokenTypes.opGREATER) {
final PsiElement left = binary.getLeftOperand();
final PsiElement right = binary.getRightOperand();
if (OpenapiTypesUtil.isNumber(right) && OpenapiTypesUtil.isFunctionReference(left)) {
final boolean isTargetNumber = right.getText().equals("0");
if (isTargetNumber) {
final FunctionReference candidate = (FunctionReference) left;
final String candidateName = candidate.getName();
if (candidateName != null && candidateName.equals("preg_match")) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length == 2) {
final String suggestedAssertion = methodName.equals("assertTrue") ? "assertRegExp" : "assertNotRegExp";
final String[] suggestedArguments = new String[assertionArguments.length + 1];
suggestedArguments[0] = functionArguments[0].getText();
suggestedArguments[1] = functionArguments[1].getText();
if (assertionArguments.length > 1) {
suggestedArguments[2] = assertionArguments[1].getText();
}
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 AssertCountStrategy 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 > 1 && OpenapiTypesUtil.isFunctionReference(assertionArguments[1])) {
final FunctionReference candidate = (FunctionReference) assertionArguments[1];
final String functionName = candidate.getName();
if (functionName != null && functionName.equals("count")) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length == 1) {
final String suggestedAssertion = targetMapping.get(methodName);
final String[] suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = assertionArguments[0].getText();
suggestedArguments[1] = functionArguments[0].getText();
if (assertionArguments.length > 2) {
suggestedArguments[2] = assertionArguments[2].getText();
}
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 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) {
final Function scope = ExpressionSemanticUtil.getScope(reference);
final boolean shouldReport = scope == null || !scope.getName().equals("assertFileEquals");
if (shouldReport) {
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(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
extracts.clear();
}
}
return result;
}
use of com.kalessil.phpStorm.phpInspectionsEA.fixers.PhpUnitAssertFixer in project phpinspectionsea by kalessil.
the class WillReturnStrategy method apply.
public static void apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder) {
if (methodName.equals("will")) {
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 && methodsMapping.containsKey(innerMethodName)) {
final PsiElement[] innerArguments = innerReference.getParameters();
if (innerArguments.length == 1) {
final String suggestedAssertion = methodsMapping.get(innerMethodName);
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestedAssertion), new PhpUnitAssertFixer(suggestedAssertion, new String[] { innerArguments[0].getText() }));
}
}
}
}
}
Aggregations