code: def fix_ar_analyses_inconsistencies(portal):
      
      def fix_analyses(request, status):
          wf_id = "bika_analysis_workflow"
          workflow = api.get_tool("portal_workflow").getWorkflowById(wf_id)
          review_states = ['assigned', 'unassigned', 'to_be_verified']
          query = dict(portal_type="Analysis",
                       getRequestUID=api.get_uid(request),
                       review_state=review_states)
          for brain in api.search(query, CATALOG_ANALYSIS_LISTING):
              analysis = api.get_object(brain)
              # If the analysis is assigned to a worksheet, unassign first
              ws = analysis.getWorksheet()
              if ws:
                  remove_analysis_from_worksheet(analysis)
                  reindex_request(analysis)
  
              # Force the new state
              changeWorkflowState(analysis, wf_id, status)
              workflow.updateRoleMappingsFor(analysis)
              analysis.reindexObject(idxs=["review_state", "is_active"])
  
      def fix_ar_analyses(status, wf_state_id="review_state"):
          brains = api.search({wf_state_id: status},
                              CATALOG_ANALYSIS_REQUEST_LISTING)
          total = len(brains)
          for num, brain in enumerate(brains):
              if num % 100 == 0:
                  logger.info("Fixing inconsistent analyses from {} ARs: {}/{}"
                              .format(status, num, total))
              fix_analyses(brain, status)
  
      logger.info("Fixing Analysis Request - Analyses inconsistencies ...")
      pool = ActionHandlerPool.get_instance()
      pool.queue_pool()
      fix_ar_analyses("cancelled")
      fix_ar_analyses("invalid")
      fix_ar_analyses("rejected")
      pool.resume()
      commit_transaction(portal)
docs: Fixes inconsistencies between analyses and the ARs they belong to when
      the AR is in a "cancelled", "invalidated" or "rejected state