private static void replaceCollectionLoopWithForeach(@NotNull PsiForStatement forStatement) {
      final PsiStatement body = forStatement.getBody();
      final PsiStatement firstStatement = getFirstStatement(body);
      final PsiStatement initialization = forStatement.getInitialization();
      if (!(initialization instanceof PsiDeclarationStatement declaration)) {
        return;
      }
      final PsiElement declaredIterator = declaration.getDeclaredElements()[0];
      if (!(declaredIterator instanceof PsiVariable iterator)) {
        return;
      }
      final PsiMethodCallExpression initializer = (PsiMethodCallExpression)PsiUtil.skipParenthesizedExprDown(iterator.getInitializer());
      if (initializer == null) {
        return;
      }
      final PsiReferenceExpression methodExpression = initializer.getMethodExpression();
      final PsiExpression collection = PsiUtil.skipParenthesizedExprDown(ExpressionUtils.getEffectiveQualifier(methodExpression));
      if (collection == null) {
        return;
      }
      final PsiType collectionType = collection.getType();
      if (collectionType == null) {
        return;
      }
      final PsiType contentType = getContentType(collectionType, CommonClassNames.JAVA_LANG_ITERABLE);
      if (contentType == null) {
        return;
      }
      PsiType iteratorContentType = getContentType(iterator.getType(), CommonClassNames.JAVA_UTIL_ITERATOR);
      if (TypeUtils.isJavaLangObject(iteratorContentType)) {
        iteratorContentType = getContentType(initializer.getType(), CommonClassNames.JAVA_UTIL_ITERATOR);
      }
      if (iteratorContentType == null) {
        return;
      }

      final boolean isDeclaration = isIteratorNextDeclaration(firstStatement, iterator, contentType);
      final PsiStatement statementToSkip;
      final @NonNls String finalString;
      final String contentVariableName;
      if (isDeclaration) {
        final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)firstStatement;
        final PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
        final PsiElement declaredElement = declaredElements[0];
        if (!(declaredElement instanceof PsiVariable variable)) {
          return;
        }
        iteratorContentType = variable.getType();
        contentVariableName = variable.getName();
        statementToSkip = declarationStatement;
        finalString = variable.hasModifierProperty(PsiModifier.FINAL) ? "final " : "";
      }
      else {
        if (collection instanceof PsiReferenceExpression referenceExpression) {
          final String collectionName = referenceExpression.getReferenceName();
          contentVariableName = createNewVariableName(forStatement, contentType, collectionName);
        }
        else {
          contentVariableName = createNewVariableName(forStatement, contentType, null);
        }
        finalString = JavaCodeStyleSettings.getInstance(forStatement.getContainingFile()).GENERATE_FINAL_LOCALS ? "final " : "";
        statementToSkip = null;
      }
      final String contentTypeString = iteratorContentType.getCanonicalText();
      final CommentTracker ct = new CommentTracker();
      final @NonNls StringBuilder newStatement = new StringBuilder();
      newStatement.append("for(");
      newStatement.append(finalString);
      newStatement.append(contentTypeString);
      newStatement.append(' ');
      newStatement.append(contentVariableName);
      newStatement.append(": ");
      if (!TypeConversionUtil.isAssignable(iteratorContentType, contentType)) {
        newStatement.append('(').append("java.lang.Iterable<").append(contentTypeString).append('>').append(')');
      }
      newStatement.append(ct.text(collection));
      newStatement.append(')');
      replaceIteratorNext(body, contentVariableName, iterator, contentType, statementToSkip, ct, newStatement);
      ct.replaceAndRestoreComments(forStatement, newStatement.toString());
    }
private static void replaceCollectionLoopWithForeach(@NotNull PsiForStatement forStatement)