private static void replaceArrayLoopWithForeach(@NotNull PsiForStatement forStatement) {
      final PsiExpression maximum = getMaximum(forStatement);
      if (!(maximum instanceof PsiReferenceExpression arrayLengthExpression)) {
        return;
      }
      final PsiVariable indexVariable = getIndexVariable(forStatement);
      if (indexVariable == null) {
        return;
      }
      PsiReferenceExpression arrayReference = (PsiReferenceExpression)PsiUtil
        .skipParenthesizedExprDown(arrayLengthExpression.getQualifierExpression());
      if (arrayReference == null) {
        final PsiElement target = arrayLengthExpression.resolve();
        if (!(target instanceof PsiVariable variable)) {
          return;
        }
        final PsiExpression initializer = PsiUtil.skipParenthesizedExprDown(variable.getInitializer());
        if (!(initializer instanceof PsiReferenceExpression referenceExpression)) {
          return;
        }
        arrayReference = (PsiReferenceExpression)PsiUtil.skipParenthesizedExprDown(referenceExpression.getQualifierExpression());
        if (arrayReference == null) {
          return;
        }
      }
      final PsiType type = arrayReference.getType();
      if (!(type instanceof PsiArrayType arrayType)) {
        return;
      }
      PsiType componentType = arrayType.getComponentType();
      final PsiElement target = arrayReference.resolve();
      if (!(target instanceof PsiVariable arrayVariable)) {
        return;
      }
      final PsiStatement body = forStatement.getBody();
      final PsiStatement firstStatement = getFirstStatement(body);
      final boolean isDeclaration = isArrayElementDeclaration(firstStatement, arrayVariable, indexVariable);
      final String contentVariableName;
      final @NonNls String finalString;
      final PsiStatement statementToSkip;
      if (isDeclaration) {
        final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)firstStatement;
        final PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
        final PsiElement declaredElement = declaredElements[0];
        if (!(declaredElement instanceof PsiVariable variable)) {
          return;
        }
        componentType = variable.getType();
        if (VariableAccessUtils.variableIsAssigned(variable, forStatement)) {
          final String collectionName = arrayReference.getReferenceName();
          contentVariableName = createNewVariableName(forStatement, componentType, collectionName);
          if (JavaCodeStyleSettings.getInstance(forStatement.getContainingFile()).GENERATE_FINAL_LOCALS) {
            finalString = "final ";
          }
          else {
            finalString = "";
          }
          statementToSkip = null;
        }
        else {
          contentVariableName = variable.getName();
          statementToSkip = declarationStatement;
          if (variable.hasModifierProperty(PsiModifier.FINAL)) {
            finalString = "final ";
          }
          else {
            finalString = "";
          }
        }
      }
      else {
        final String collectionName = arrayReference.getReferenceName();
        contentVariableName = createNewVariableName(forStatement, componentType, collectionName);
        if (JavaCodeStyleSettings.getInstance(forStatement.getContainingFile()).GENERATE_FINAL_LOCALS) {
          finalString = "final ";
        }
        else {
          finalString = "";
        }
        statementToSkip = null;
      }
      final CommentTracker ct = new CommentTracker();
      final @NonNls StringBuilder newStatement = new StringBuilder();
      newStatement.append("for(");
      newStatement.append(finalString);
      newStatement.append(componentType.getCanonicalText());
      newStatement.append(' ');
      newStatement.append(contentVariableName);
      newStatement.append(": ");
      final String arrayName = getVariableReferenceText(arrayReference, arrayVariable, forStatement);
      newStatement.append(arrayName);
      newStatement.append(')');
      if (body != null) {
        replaceArrayAccess(body, contentVariableName, arrayVariable, indexVariable, statementToSkip, ct, newStatement);
      }
      ct.replaceAndRestoreComments(forStatement, newStatement.toString());
    }
private static void replaceArrayLoopWithForeach(@NotNull PsiForStatement forStatement)