use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression 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.StringLiteralExpression 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]));
}
};
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project idea-php-typo3-plugin by cedricziel.
the class ClassNameMatcherInspection method getDeprecatedClasses.
private Set<String> getDeprecatedClasses(PhpPsiElement element) {
Set<PsiElement> elements = new HashSet<>();
PsiFile[] classNameMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassNameMatcher.php", GlobalSearchScope.allScope(element.getProject()));
for (PsiFile file : classNameMatcherFiles) {
Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
}
return elements.stream().map(stringLiteral -> "\\" + ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project idea-php-typo3-plugin by cedricziel.
the class ConstantMatcherInspection method getRemovedConstantsFQNs.
private Set<String> getRemovedConstantsFQNs(ConstantReference element) {
Set<PsiElement> elements = new HashSet<>();
PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ConstantMatcher.php", GlobalSearchScope.allScope(element.getProject()));
for (PsiFile file : constantMatcherFiles) {
Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
}
return elements.stream().map(stringLiteral -> "\\" + ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project idea-php-typo3-plugin by cedricziel.
the class FunctionCallMatcherInspection method getRemovedGlobalFuntions.
private Set<String> getRemovedGlobalFuntions(PhpPsiElement element) {
Set<PsiElement> elements = new HashSet<>();
PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "FunctionCallMatcher.php", GlobalSearchScope.allScope(element.getProject()));
for (PsiFile file : constantMatcherFiles) {
Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
}
return elements.stream().map(stringLiteral -> "\\" + ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
Aggregations