首页 > 解决方案 > Sonarqube 问题“重构此方法以将其认知复杂度从 18 降低到允许的 15。” 重写代码块

问题描述

Sonarqube 对以下代码存在问题,给出警告“重构此方法以将其认知复杂度从 18 降低到允许的 15”。

不确定我需要做什么,或者我如何重构函数以满足“认知复杂度最大 15”的标准。有什么帮助吗?

this.register(
        VehicleConceptSpecification,
        Action.Execute,
        async (_vehicle: VehicleInfo,
               conceptSpec: { result: TestConceptSpecification[] },
               controlRecord: Value | undefined,
               userPartnerId: string,
               onProgress?: (progress: number) => void): Promise<IVehicleData<RunRoutineResponse>> => {
          await this.setup;
          if (!this.productCommunicationClient?.canExecuteRoutine()) {
            return Promise.reject('Client is not able to execute routine');
          }
          if (conceptSpec.result[0]?.node == null) {
            return Promise.reject('concept specification does not contain node identifier');
          }
          if (conceptSpec.result[0]?.onVehicleTest?.value == null) {
            return Promise.reject(
              'Concept specification does not contain onVehicleTest with value');
          }
          const nodes = await this.readNodes([this.convertNodeIdentifier(conceptSpec.result[0].node)]);
          if (nodes?.length !== 1) {
            return Promise.reject('Failed to read node');
          }
          const iso14229routine: Iso14229RoutineSpecification = {
            communicationProtocol: CommunicationProtocol.Iso14229,
            value: controlRecord,
            node: nodes[0],
            partnerIdentifier: userPartnerId,
            diagnosticObjectIdentifier: conceptSpec.result[0].onVehicleTest.value
          };
          const observer = await this.productCommunicationClient.executeRoutine(iso14229routine);
          const result: RunRoutineResponse = await new Promise((resolve, reject) => {
            let routineResult: RunRoutineResponse | undefined;
            observer.subscribe({
              next: ({ progress, response }) => {
                if (onProgress != null) {
                  onProgress(progress);
                }
                if (response != null) {
                  routineResult = response;
                }
          });

标签: javascripttypescriptsonarqube

解决方案


推荐阅读