Context (@NotNull Project project, @NotNull PsiMethodCallExpression call) { PsiExpression outerIfPresentQualifier = call.getMethodExpression().getQualifierExpression(); PsiMethodCallExpression qualifierCall = tryCast(outerIfPresentQualifier, PsiMethodCallExpression.class); PsiLambdaExpression outerIfPresentArgument = tryCast(call.getArgumentList().getExpressions()[0], PsiLambdaExpression.class); if (outerIfPresentArgument == null) return null; if (outerIfPresentArgument.getParameterList().getParametersCount() != 1) return null; PsiParameter outerParameter = outerIfPresentArgument.getParameterList().getParameters()[0]; if (outerParameter == null) return null; String outerIfPresentParameterName = outerParameter.getName(); PsiExpression outerIfPresentBodyExpr = LambdaUtil.extractSingleExpressionFromBody(outerIfPresentArgument.getBody()); PsiMethodCallExpression outerIfPresentBody = tryCast(outerIfPresentBodyExpr, PsiMethodCallExpression.class); if (!OPTIONAL_IF_PRESENT.test(outerIfPresentBody)) return null; PsiExpression innerIfPresentQualifier = outerIfPresentBody.getMethodExpression().getQualifierExpression(); PsiExpression nonTrivialQualifier = ExpressionUtils.isReferenceTo(innerIfPresentQualifier, outerParameter) ? null : innerIfPresentQualifier; PsiExpression innerIfPresentArgument = outerIfPresentBody.getArgumentList().getExpressions()[0]; if (ReferencesSearch.search(outerParameter, new LocalSearchScope(innerIfPresentArgument)).findFirst() != null) return null; PsiMethodCallExpression mapBefore = null; if (OPTIONAL_MAP.test(qualifierCall)) { // case when map(Value::getOptional).ifPresent(p -> p.ifPresent(...)) if (isOptionalTypeParameter(qualifierCall.getType())) { mapBefore = qualifierCall; } } return new Context(mapBefore, nonTrivialQualifier, outerIfPresentParameterName, innerIfPresentArgument); }