javascript - 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;
}
});
解决方案
推荐阅读
- html - 使多个元素悬停
- discord.py - 我正在尝试编写一个不和谐的音乐机器人并遇到了这个问题
- javascript - 仅将鼠标按下时的元素附加到特定的 div 元素中
- icalendar - 我无法弄清楚这是什么日期格式
- python - 尝试创建一个在调用类和 def 后进入循环的类
- javascript - 如何使用纯 JavaScript 将表格行拖放到选择字段?
- javascript - 我怎样才能做到这一点,以便用户在我的功能期间的任何时候都可以键入“reset”并将它们带回我的功能的开头?
- regex - 如何在 Kotlin 的替换函数中将函数作为参数传递?
- android - LazyColumn 中的 Jetpack Compose 常规项目
- javascript - 使用管道和地图创建过滤器