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, @NotNull PhpUnitVersion version) {
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;
final String suggestedType;
final String[] suggestedArguments;
final String message;
if (version.below(PhpUnitVersion.PHPUNIT80)) {
suggestedAssertion = targetMapping.get(methodName);
suggestedType = targetFunctionMapping.get(functionName);
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();
}
message = String.format(messagePatternInternalType, suggestedAssertion, suggestedType);
} else {
/* internal type assertions were deprecated */
suggestedAssertion = String.format(targetMapping.get(methodName).replace("InternalType", "Is%s").replace("NotIs", "IsNot"), StringUtils.capitalize(targetFunctionMapping.get(functionName)));
suggestedType = null;
suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = functionArguments[0].getText();
if (assertionArguments.length > 1) {
suggestedArguments[1] = assertionArguments[1].getText();
}
message = String.format(messagePattern, suggestedAssertion);
}
/* register an issue */
holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(message), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class AssertResourceExistsStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder, @NotNull PhpUnitVersion version) {
boolean result = false;
if (targetMapping.containsKey(methodName)) {
final PsiElement[] assertionArguments = reference.getParameters();
if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
final FunctionReference candidate = (FunctionReference) assertionArguments[0];
final String functionName = candidate.getName();
if (functionName != null && targetFunctions.containsKey(functionName)) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length == 1) {
final String suggestedAssertion = String.format(targetMapping.get(methodName), targetFunctions.get(functionName));
final String[] suggestedArguments = new String[assertionArguments.length];
suggestedArguments[0] = functionArguments[0].getText();
if (assertionArguments.length > 1) {
suggestedArguments[1] = assertionArguments[1].getText();
}
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), suggestNotDeprecated(suggestedAssertion, version)), new PhpUnitAssertFixer(suggestedAssertion, suggestedArguments));
result = true;
}
}
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class AssertContainsStrategy method apply.
public static boolean apply(@NotNull String methodName, @NotNull MethodReference reference, @NotNull ProblemsHolder holder, @NotNull PhpUnitVersion version) {
boolean result = false;
if (version.below(PhpUnitVersion.PHPUNIT90) && targetMapping.containsKey(methodName)) {
final PsiElement[] assertionArguments = reference.getParameters();
if (assertionArguments.length > 0 && OpenapiTypesUtil.isFunctionReference(assertionArguments[0])) {
final FunctionReference candidate = (FunctionReference) assertionArguments[0];
final String functionName = candidate.getName();
if (functionName != null && functionName.equals("in_array")) {
final PsiElement[] functionArguments = candidate.getParameters();
if (functionArguments.length >= 2) {
final String suggestedAssertion = targetMapping.get(methodName);
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.jetbrains.php.lang.psi.elements.FunctionReference 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.jetbrains.php.lang.psi.elements.FunctionReference 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;
}
Aggregations