Genese complexity report

<- ast-method.model.ts
Methods : 10
Complexity index : 124.1
Cyclomatic complexity : 33
Cognitive complexity
60 % Correct 6/10
30 % Warning 3/10 (threshold : 10)
10 % Error 1/10 (threshold : 20)
Cyclomatic complexity
80 % Correct 8/10
20 % Warning 2/10 (threshold : 5)
0 % Error 0/10 (threshold : 10)
Methods of ast-method.model.ts
evaluate Complexity Index 6.5 Cyclomatic complexity 1
                            
                                
                
            
            
            
                // ---------------------------------------------------------------------------------
                //                                  Other methods
                // ---------------------------------------------------------------------------------
            
            
            
                /**
                 * Creates the displayed code of this AstMethod and evaluates its complexity
                 */
                evaluate(): void { // ------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    this.createDisplayedCode(); // ------------------------------------------------------------ +1.2 Complexity index (+0.2 atomic, +1 structural)
                    // LogService.logMethod(this);
                    this.cognitiveStatus = this.getComplexityStatus(ComplexityType.COGNITIVE); // ------------- +1.7 Complexity index (+0.7 atomic, +1 structural)
                    this.cyclomaticCpx = CS.calculateCyclomaticCpx(this.astNode); // -------------------------- +1.7 Complexity index (+0.7 atomic, +1 structural)
                    this.cyclomaticStatus = this.getComplexityStatus(ComplexityType.CYCLOMATIC); // ----------- +1.7 Complexity index (+0.7 atomic, +1 structural)
                }
            
                            
                        
calculateCpxFactors Complexity Index 6.9 Cyclomatic complexity 6
                            
                                
                
            
            
                /**
                 * Calculates the Complexity Factors of the method
                 */
                private calculateCpxFactors(): void { // --------------------------------- +0.2 Complexity index (+0.2 atomic)
                    if (!(this._displayedCode?.lines?.length > 0)) { // ------------------ +1.7 Complexity index (+0.7 atomic, +1 structural)
                        this.createDisplayedCode(); // ----------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
                    }
                    this.cpxFactors = new CpxFactors(); // ------------------------------- +0.5 Complexity index (+0.5 atomic)
                    for (const line of this._displayedCode?.lines) { // ------------------ +1.5 Complexity index (+0.5 atomic, +1 structural)
                        this.cpxFactors = this.cpxFactors.add(line.cpxFactors); // ------- +1.8 Complexity index (+0.8 atomic, +1 structural)
                    }
                }
            
                            
                        
getComplexityStatus Complexity Index 16.4 Cyclomatic complexity 9
                            
                                
                
            
            
                /**
                 * Gets the complexity status of the method for a given complexity type
                 * @param cpxType
                 */
                getComplexityStatus(cpxType: ComplexityType): MethodStatus { // ------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    let status = MethodStatus.WARNING; // ----------------------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    if ( // ----------------------------------------------------------------------------------------------------------------- +1.1 Complexity index (+0.1 atomic, +1 structural)
                        (cpxType === ComplexityType.COGNITIVE && this.cpxIndex <= Options.cognitiveCpx.warningThreshold) // ----------------- +2.3 Complexity index (+1.3 atomic, +1 structural)
                        || // --------------------------------------------------------------------------------------------------------------- +1.1 Complexity index (+0.1 atomic, +1 structural)
                        (cpxType === ComplexityType.CYCLOMATIC && this.cyclomaticCpx <= Options.cyclomaticCpx.warningThreshold)) { // ------- +2.2 Complexity index (+1.2 atomic, +1 structural)
                        status = MethodStatus.CORRECT; // ----------------------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    } else if ( // ---------------------------------------------------------------------------------------------------------- +1.1 Complexity index (+0.1 atomic, +1 structural)
                        (cpxType === ComplexityType.COGNITIVE && Math.round(this.cpxIndex) > Options.cognitiveCpx.errorThreshold) // -------- +3.5 Complexity index (+1.5 atomic, +2 structural)
                        || // --------------------------------------------------------------------------------------------------------------- +1.1 Complexity index (+0.1 atomic, +1 structural)
                        (cpxType === ComplexityType.CYCLOMATIC && this.cyclomaticCpx > Options.cyclomaticCpx.errorThreshold)) { // ---------- +2.2 Complexity index (+1.2 atomic, +1 structural)
                        status = MethodStatus.ERROR; // ------------------------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    }
                    return status; // ------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                }
            
                            
                        
createDisplayedCode Complexity Index 9.4 Cyclomatic complexity 1
                            
                                
                
            
            
                /**
                 * Creates the method's code to display, with comments
                 * @param astNode  // The AstNode to analyse (by default: the AstNode associated to this AstMethod)
                 */
                createDisplayedCode(astNode: AstNode = this.astNode): void { // ---------------------------------------------- +0.6 Complexity index (+0.6 atomic)
                    this.setDisplayedCodeLines(); // ------------------------------------------------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
                    this.setDeclarationCpxFactors(); // ---------------------------------------------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
                    this.setCpxFactorsToDisplayedCode(astNode, false); // ---------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                    this._displayedCode.setLinesDepthAndNestingCpx(); // ----------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                    this.addCommentsToDisplayedCode(); // -------------------------------------------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
                    this.calculateCpxFactors(); // --------------------------------------------------------------------------- +1.2 Complexity index (+0.2 atomic, +1 structural)
                    this._displayedCode.setTextWithLines(); // --------------------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                }
            
                            
                        
setDisplayedCodeLines Complexity Index 7.6 Cyclomatic complexity 2
                            
                                
                
            
            
                /**
                 * Creates the Code object corresponding to the code to display
                 */
                private setDisplayedCodeLines(): void { // -------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    this._displayedCode = new Code(); // ---------------------------------------- +0.5 Complexity index (+0.5 atomic)
                    for (const line of this.codeLines) { // ------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        const displayedLine = new CodeLine(); // -------------------------------- +0.4 Complexity index (+0.4 atomic)
                        displayedLine.issue = line.issue; // ------------------------------------ +0.5 Complexity index (+0.5 atomic)
                        displayedLine.end = line.end; // ---------------------------------------- +0.5 Complexity index (+0.5 atomic)
                        displayedLine.start = line.start; // ------------------------------------ +0.5 Complexity index (+0.5 atomic)
                        displayedLine.text = line.text; // -------------------------------------- +0.5 Complexity index (+0.5 atomic)
                        displayedLine.text = this.getDisplayedLineText(displayedLine); // ------- +1.6 Complexity index (+0.6 atomic, +1 structural)
                        this._displayedCode.lines.push(displayedLine); // ----------------------- +1.5 Complexity index (+0.5 atomic, +1 structural)
                    }
                }
            
                            
                        
getDisplayedLineText Complexity Index 15.7 Cyclomatic complexity 5
                            
                                
                
            
            
                /**
                 * Returns the text to display for a given line. Removes characters of the first and the last lines which are not inside the AstMethod
                 * @param line      // The line to display
                 */
                private getDisplayedLineText(line: CodeLine): string { // --------------------------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    let text = line.text; // -------------------------------------------------------------------------------------------------------------------- +0.4 Complexity index (+0.4 atomic)
                    if (line.issue === this.codeLines[0]?.issue) { // ------------------------------------------------------------------------------------------- +1.8 Complexity index (+0.8 atomic, +1 structural)
                        const firstCharPosition = this.start - line.start; // ----------------------------------------------------------------------------------- +0.7 Complexity index (+0.7 atomic)
                        const indentation = text.slice(0, text.length - text.trimLeft().length) // -------------------------------------------------------------- +4.1 Complexity index (+1.1 atomic, +2 structural, +1 use)
                        text = `\n${indentation}${text.slice(firstCharPosition)}`; // --------------------------------------------------------------------------- +2.6 Complexity index (+0.6 atomic, +1 structural, +1 use)
                    }
                    if (line.issue === this.codeLines[this.codeLines.length - 1]?.issue) { // ------------------------------------------------------------------- +2.2 Complexity index (+1.2 atomic, +1 structural)
                        const lastCharPosition = this.end - line.start; // -------------------------------------------------------------------------------------- +0.7 Complexity index (+0.7 atomic)
                        text = text.slice(0, lastCharPosition); // ---------------------------------------------------------------------------------------------- +2.6 Complexity index (+0.6 atomic, +1 structural, +1 use)
                    }
                    return text; // ----------------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                }
            
                            
                        
setDeclarationCpxFactors Complexity Index 6.2 Cyclomatic complexity 1
                            
                                
                
            
            
                private setDeclarationCpxFactors(): void { // ---------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    this.increaseLineCpxFactors(this.astNode, this._displayedCode.getLine(this.astNode.lineStart)); // ------- +3.0 Complexity index (+1.0 atomic, +2 structural)
                    this._displayedCode.getLine(this.astNode.lineStart).astNodes.push(this.astNode); // ---------------------- +3.0 Complexity index (+1.0 atomic, +2 structural)
                }
            
                            
                        
setCpxFactorsToDisplayedCode Complexity Index 19.1 Cyclomatic complexity 4
                            
                                
                
            
            
                /**
                 * Calculates the complexity factors of each CodeLine
                 * @param astNode                   // The AstNode of the method
                 * @param startedUncommentedLines   // Param for recursion (checks if the current line is the first uncommented one)
                 */
                private setCpxFactorsToDisplayedCode(astNode: AstNode, startedUncommentedLines = false): void { // ---------------------------- +2.6 Complexity index (+0.6 atomic, +2 recursivity)
                    for (const childAst of astNode.children) { // ----------------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        let issue = Math.max(childAst.lineStart, this.codeLines[0]?.issue); // ------------------------------------------------ +2.0 Complexity index (+1.0 atomic, +1 structural)
                        const codeLine: CodeLine = this._displayedCode.lines.find(l => l.issue === issue); // --------------------------------- +3.8 Complexity index (+1.3 atomic, +0.5 nesting, +2 structural)
                        if (Ast.isElseStatement(childAst)) { // ------------------------------------------------------------------------------- +2.9 Complexity index (+0.4 atomic, +0.5 nesting, +2 structural)
                            childAst.cpxFactors.atomic.node = cpxFactors.atomic.node; // ------------------------------------------------------ +0.8 Complexity index (+0.8 atomic)
                            issue--; // ------------------------------------------------------------------------------------------------------- +0.1 Complexity index (+0.1 atomic)
                        }
                        this.increaseLineCpxFactors(childAst, codeLine); // ------------------------------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                        this._displayedCode.getLine(issue).astNodes.push(childAst); // -------------------------------------------------------- +2.7 Complexity index (+0.7 atomic, +2 structural)
                        this.setCpxFactorsToDisplayedCode(childAst, startedUncommentedLines); // ---------------------------------------------- +1.4 Complexity index (+0.4 atomic, +1 structural)
                    }
                }
            
                            
                        
increaseLineCpxFactors Complexity Index 3.7 Cyclomatic complexity 3
                            
                                
                
            
            
                /**
                 * Adds the Complexity of a AstNode to its CodeLine
                 * @param astNode      // The AstNode inside the line of code
                 * @param codeLine      // The CodeLine containing the AstNode
                 */
                private increaseLineCpxFactors(astNode: AstNode, codeLine: CodeLine): void { // ------- +0.6 Complexity index (+0.6 atomic)
                    if (!codeLine.isCommented) { // --------------------------------------------------- +1.3 Complexity index (+0.3 atomic, +1 structural)
                        codeLine.cpxFactors = codeLine.cpxFactors.add(astNode?.cpxFactors); // -------- +1.8 Complexity index (+0.8 atomic, +1 structural)
                    }
            
                }
            
                            
                        
addCommentsToDisplayedCode Complexity Index 32.6 Cyclomatic complexity 1
                            
                                
                
            
            
                /**
                 * Adds information about complexity factors for each line of the displayed code
                 */
                private addCommentsToDisplayedCode(): void { // ------------------------------------------------------------------------------------------------------------------- +0.2 Complexity index (+0.2 atomic)
                    this._displayedCode.lines // ---------------------------------------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
                        .filter(line => line.cpxFactors.total > 0) // ------------------------------------------------------------------------------------------------------------- +2.8 Complexity index (+0.8 atomic, +2 structural)
                        .forEach(line => { // ------------------------------------------------------------------------------------------------------------------------------------- +2.3 Complexity index (+0.3 atomic, +2 structural)
                            let comment = `+${line.cpxFactors.total.toFixed(1)} Complexity index (+${line.cpxFactors.totalAtomic.toFixed(1)} ${FactorCategory.ATOMIC}`; // -------- +3.4 Complexity index (+1.4 atomic, +2 structural)
                            comment = line.cpxFactors.totalAggregation > 0 ? `${comment}, +${line.cpxFactors.totalAggregation} ${FactorCategory.AGGREGATION}` : comment; // ------- +3.5 Complexity index (+1.5 atomic, +1 nesting, +1 structural)
                            comment = line.cpxFactors.totalNesting > 0 ? `${comment}, +${line.cpxFactors.totalNesting} nesting` : comment; // ------------------------------------- +3.3 Complexity index (+1.3 atomic, +1 nesting, +1 structural)
                            comment = line.cpxFactors.totalDepth > 0 ? `${comment}, +${line.cpxFactors.totalDepth} depth` : comment; // ------------------------------------------- +3.3 Complexity index (+1.3 atomic, +1 nesting, +1 structural)
                            comment = line.cpxFactors.totalRecursion > 0 ? `${comment}, +${line.cpxFactors.totalRecursion} recursivity` : comment; // ----------------------------- +3.3 Complexity index (+1.3 atomic, +1 nesting, +1 structural)
                            comment = line.cpxFactors.totalStructural > 0 ? `${comment}, +${line.cpxFactors.totalStructural} ${FactorCategory.STRUCTURAL}` : comment; // ---------- +3.5 Complexity index (+1.5 atomic, +1 nesting, +1 structural)
                            comment = line.cpxFactors.totalUse > 0 ? `${comment}, +${line.cpxFactors.totalUse} ${FactorCategory.USE}` : comment; // ------------------------------- +3.5 Complexity index (+1.5 atomic, +1 nesting, +1 structural)
                            comment = `${comment})`; // --------------------------------------------------------------------------------------------------------------------------- +0.3 Complexity index (+0.3 atomic)
                            this._displayedCode.getLine(line.issue).addComment(comment, this.maxLineLength); // ------------------------------------------------------------------- +2.9 Complexity index (+0.9 atomic, +2 structural)
                        });
                }