use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project phpinspectionsea by kalessil.
the class GreaterOrEqualInHashElementStrategy method apply.
public static boolean apply(@NotNull BinaryExpression expression, @NotNull ProblemsHolder holder) {
/* general structure expectations */
final PsiElement operation = expression.getOperation();
if (null == operation || PhpTokenTypes.opGREATER_OR_EQUAL != operation.getNode().getElementType()) {
return false;
}
final PsiElement left = expression.getLeftOperand();
if (!(left instanceof StringLiteralExpression)) {
return false;
}
/* analysis itself */
final PsiElement parent = expression.getParent();
if (null != parent && parent.getParent() instanceof ArrayCreationExpression) {
holder.registerProblem(operation, MessagesPresentationUtil.prefixWithEa(message));
return true;
}
return false;
}
use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project phpinspectionsea by kalessil.
the class ArgumentUnpackingCanBeUsedInspector 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("call_user_func_array")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 2) {
final boolean isContainerValid = arguments[1] instanceof Variable || arguments[1] instanceof ArrayCreationExpression || arguments[1] instanceof FunctionReference;
if (isContainerValid && arguments[0] instanceof StringLiteralExpression) {
/* do not process strings with injections */
final StringLiteralExpression targetFunction = (StringLiteralExpression) arguments[0];
if (targetFunction.getFirstPsiChild() == null) {
final String replacement = "%f%(...%a%)".replace("%a%", arguments[1].getText()).replace("%f%", targetFunction.getContents());
final String message = messagePattern.replace("%e%", replacement);
holder.registerProblem(reference, message, new UnpackFix(replacement));
}
}
}
}
}
// TODO: if (isContainerValid && params[0] instanceof ArrayCreationExpression) {
// TODO: call_user_func_array([...], ...); string method name must not contain ::
}
};
}
use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project yii2support by nvlad.
the class RequireParameterInspection 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;
}
final String name = reference.getName();
if (name == null || !ArrayUtil.contains(name, ViewUtil.renderMethods)) {
return;
}
final PsiElement[] renderParameters = reference.getParameters();
if (renderParameters.length == 0 || !(renderParameters[0] instanceof StringLiteralExpression)) {
return;
}
final ViewResolve resolve = ViewUtil.resolveView(renderParameters[0]);
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());
views.removeIf(viewInfo -> !application.equals(viewInfo.application));
if (views.size() == 0) {
return;
}
final Collection<String> viewParameters = new HashSet<>();
for (ViewInfo view : views) {
viewParameters.addAll(view.parameters);
}
if (viewParameters.size() == 0) {
return;
}
final Collection<String> existKeys;
if (renderParameters.length > 1) {
if (renderParameters[1] instanceof ArrayCreationExpression) {
existKeys = PhpUtil.getArrayKeys((ArrayCreationExpression) renderParameters[1]);
} else if (renderParameters[1] instanceof FunctionReference) {
FunctionReference function = (FunctionReference) renderParameters[1];
if (function.getName() != null && function.getName().equals("compact")) {
existKeys = new HashSet<>();
for (PsiElement element : function.getParameters()) {
if (element instanceof StringLiteralExpression) {
existKeys.add(((StringLiteralExpression) element).getContents());
}
}
} else {
return;
}
} else {
return;
}
} else {
existKeys = new HashSet<>();
}
viewParameters.removeIf(existKeys::contains);
if (viewParameters.size() == 0) {
return;
}
String description = "View " + renderParameters[0].getText() + " require ";
final Iterator<String> parameterIterator = viewParameters.iterator();
if (!isOnTheFly) {
while (parameterIterator.hasNext()) {
final String parameter = parameterIterator.next();
final String problemDescription = description + "\"" + parameter + "\" parameter.";
problemsHolder.registerProblem(reference, problemDescription, new RequireParameterLocalQuickFix(parameter));
}
return;
}
final Collection<LocalQuickFix> fixes = new HashSet<>();
if (viewParameters.size() > 1) {
fixes.add(new RequireParameterLocalQuickFix(viewParameters.toArray(new String[0])));
StringBuilder parameterString = new StringBuilder();
String parameter = parameterIterator.next();
while (parameterIterator.hasNext()) {
if (parameterString.length() > 0) {
parameterString.append(", ");
}
parameterString.append("\"").append(parameter).append("\"");
fixes.add(new RequireParameterLocalQuickFix(parameter));
parameter = parameterIterator.next();
}
parameterString.append(" and \"").append(parameter).append("\" parameters.");
description += parameterString.toString();
fixes.add(new RequireParameterLocalQuickFix(parameter));
} else {
String parameter = parameterIterator.next();
description += "\"" + parameter + "\" parameter.";
fixes.add(new RequireParameterLocalQuickFix(parameter));
}
problemsHolder.registerProblem(reference, description, fixes.toArray(new LocalQuickFix[0]));
}
};
}
Aggregations