use of com.jetbrains.php.lang.psi.elements.Parameter in project phpinspectionsea by kalessil.
the class CanNotTakeArgumentsByReferenceStrategy method apply.
public static void apply(final Method method, final ProblemsHolder holder) {
for (Parameter parameter : method.getParameters()) {
if (parameter.isPassByRef() && null != method.getNameIdentifier()) {
final String strMessage = strProblemDescription.replace("%m%", method.getName());
holder.registerProblem(method.getNameIdentifier(), strMessage, ProblemHighlightType.ERROR);
return;
}
}
}
use of com.jetbrains.php.lang.psi.elements.Parameter in project phpinspectionsea by kalessil.
the class ParameterDefaultValueIsNotNullInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpMethod(@NotNull Method method) {
this.analyze(method);
}
@Override
public void visitPhpFunction(@NotNull Function function) {
this.analyze(function);
}
private void analyze(@NotNull Function function) {
final Parameter[] arguments = function.getParameters();
if (arguments.length > 0) {
/* collect violations */
final List<Parameter> violations = new ArrayList<>();
for (final Parameter argument : arguments) {
final PsiElement defaultValue = argument.getDefaultValue();
if (defaultValue != null && !PhpLanguageUtil.isNull(defaultValue)) {
/* false-positives: null can not be used due to implicit type hints */
final PhpType declared = OpenapiResolveUtil.resolveDeclaredType(argument);
if (declared.isEmpty() || declared.getTypes().stream().anyMatch(t -> Types.getType(t).equals(Types.strNull))) {
violations.add(argument);
}
}
}
if (!violations.isEmpty()) {
/* false-positives: methods overrides, so violation should be addressed in the parent */
if (function instanceof Method) {
final PhpClass clazz = ((Method) function).getContainingClass();
if (clazz != null) {
final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
if (parent != null) {
final Method parentMethod = OpenapiResolveUtil.resolveMethod(parent, function.getName());
if (parentMethod != null && !parentMethod.getAccess().isPrivate()) {
violations.clear();
return;
}
}
}
}
/* report violations */
violations.forEach(param -> holder.registerProblem(param, MessagesPresentationUtil.prefixWithEa(message)));
violations.clear();
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.Parameter in project phpinspectionsea by kalessil.
the class UselessUnsetInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
/* foreach is also a case, but there is no way to get flow entry point in actual JB platform API */
return new BasePhpElementVisitor() {
@Override
public void visitPhpMethod(@NotNull Method method) {
this.inspectUsages(method.getParameters(), method);
}
@Override
public void visitPhpFunction(@NotNull Function function) {
this.inspectUsages(function.getParameters(), function);
}
private void inspectUsages(@NotNull Parameter[] parameters, @NotNull PhpScopeHolder scope) {
if (parameters.length > 0) {
final PhpEntryPointInstruction entry = scope.getControlFlow().getEntryPoint();
for (final Parameter parameter : parameters) {
final String parameterName = parameter.getName();
if (!parameterName.isEmpty()) {
for (final PhpAccessVariableInstruction usage : OpenapiControlFlowUtil.getFollowingVariableAccessInstructions(entry, parameterName)) {
final PsiElement expression = usage.getAnchor();
final PsiElement parent = expression.getParent();
if (parent instanceof PhpUnset) {
int unsetParametersCount = ((PhpUnset) parent).getArguments().length;
final PsiElement target = (unsetParametersCount == 1 ? parent : expression);
holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(message), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
}
}
}
}
}
}
};
}
Aggregations