/// <summary>
        /// Parse method body.
        /// </summary>
        /// <param name="sourceLines">The source lines.</param>
        /// <param name="complexity">Return the complexity</param>
        /// <param name="maintainability">Return the maintainability</param>
        /// <param name="ReferenceCalls">The reference calls.</param>
        /// <param name="header"></param>
        public static void Method_Stats(List <string> sourceLines, out int complexity, out int maintainability,
                                        out List <string> ReferenceCalls, MethodNTHeader_ header = null)
        {
            ReferenceCalls = Method_ReferenceCalls(sourceLines);   // Calculate the reference calls

            string methodBody = Code_Simplify(sourceLines);

            complexity      = Method_Complexity(methodBody);
            maintainability = Method_Maintainability(methodBody, ReferenceCalls.Count);
        }
Esempio n. 2
0
        /// <summary>
        /// Merges the parameters with comments.
        /// </summary>
        /// <param name="header">The header.</param>
        /// <param name="comments">The comments.</param>
        public static void SyncParametersWithComments(MethodNTHeader_ header, MethodNTComment_ comments)
        {
            var parameters = header.Header_Parameters;

            foreach (MethodNTHeaderParameter_ parameter in parameters)
            {
                var name = parameter.ParameterName.Replace("@", "");
                MethodNTCommentParameter_ parmComment = comments.CommentParameters.FirstOrDefault(x => x.ParameterName == name);
                if (parmComment != null)
                {
                    parameter.ParameterComment = parmComment.ParameterComment;
                }
            }
        }
        /// <summary>
        /// Calculates the complexity of the given method body. The higher the count the more complex the method.
        /// </summary>
        /// <param name="methodBody">The function text.</param>
        /// <returns>
        /// The calculated complexity.
        /// </returns>
        public static int Method_Complexity(string methodBody, MethodNTHeader_ header = null)
        {
            // Branching
            int branching = Code_TotalBranching(methodBody);

            // Looping
            int looping = Code_TotalLooping(methodBody);

            // Complexity
            int tertiaryCount = Regex.Matches(methodBody, @"\s\?\s").Count;

            // Parameters
            int parameters = (header == null) ? 0 : header.Header_Parameters.Count;

            int complexity = 1 + branching + looping + tertiaryCount + parameters;

            return(complexity);
        }
Esempio n. 4
0
        /// <summary>
        /// Parse the method.
        /// </summary>
        /// <param name="sourceLines">The source lines.</param>
        /// <param name="ii">The ii.</param>
        /// <param name="iiMethodEnd">The ii method end.</param>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="sourceCode">The source code.</param>
        /// <param name="comment">The comment.</param>
        /// <param name="attribute_Lines">The attribute_ lines.</param>
        /// <param name="attribute_Breakdown">The attribute_ breakdown.</param>
        /// <param name="methodRule">The blueprint method rule.</param>
        /// <param name="methodAliasDef">The method alias definition.</param>
        /// <param name="header">The header.</param>
        /// <param name="stats">The body.</param>
        public static void Method_Parse(List <string> sourceLines, ref int ii, int iiMethodEnd, out string methodName, out List <string> sourceCode,
                                        out MethodNTComment_ comment, out List <string> attribute_Lines, out ClassNTAttributes_ attribute_Breakdown,
                                        out ClassNTBlueprintMethodRule_ methodRule, out ClassNTBlueprintMethodRule_AliasDef_ methodAliasDef,
                                        out MethodNTHeader_ header, out MethodNTstats_ stats)
        {
            if (iiMethodEnd == 0)
            {
                iiMethodEnd = sourceLines.Count - 1;
            }
            sourceCode = new List <string>();
            sourceCode.zFrom_IList(sourceLines, true, ii, iiMethodEnd);
            ii = iiMethodEnd;

            int jj = 0;

            comment             = MethodNTComment_.Create(sourceCode, ref jj, out attribute_Lines);
            methodRule          = ClassNTBlueprintMethodRule_.Create(attribute_Lines);
            methodAliasDef      = ClassNTBlueprintMethodRule_AliasDef_.Create(attribute_Lines);
            attribute_Breakdown = ClassNTAttributes_.Create(attribute_Lines);  // attributes
            header = MethodNTHeader_.Create(sourceCode, ref jj);
            stats  = MethodNTstats_.Create(sourceCode, ref jj, comment, header, attribute_Lines.Count);

            methodName = header.Header_Name;
        }
Esempio n. 5
0
        /// <summary>Creates the specified statistics for the source lines.</summary>
        /// <param name="sourceLines">The source lines.</param>
        /// <param name="ii">The ii.</param>
        /// <param name="comments">The comments.</param>
        /// <param name="header">The header.</param>
        /// <param name="attributeLines">The attribute lines.</param>
        /// <returns></returns>
        public static MethodNTstats_ Create(List <string> sourceLines, ref int ii, MethodNTComment_ comments = null, MethodNTHeader_ header = null, int attributeLines = 0)
        {
            var result = new MethodNTstats_(); // {Name = name, Value = value};

            // Execute static method to populate result parameters
            sourceLines.zTo_IList(result.SourceLines, true, ii);

            // comments
            var commentCount = 0;

            if (comments != null)
            {
                commentCount = comments.CommentParameters.Count;
                if (comments.CommentReturn != "")
                {
                    commentCount++;
                }
                if (comments.CommentSummary != "")
                {
                    commentCount++;
                }
            }

            result.MethodTotalCommentLines = commentCount;
            result.MethodTotalLines        = sourceLines.Count;
            result.MethodTotalBodyLines    = sourceLines.Count - commentCount - attributeLines - 3; // 3 = header + { + }
            if (result.MethodTotalBodyLines < 1)
            {
                result.MethodTotalBodyLines = 1;
            }

            MethodNTstats_Methods.Method_Stats(result.SourceLines, out result.CodeComplexity, out result.CodeMaintainability,
                                               out result.ReferenceCalls, header);
            return(result);
        }
        /// <summary>Maintainability calculation of method from the method body.</summary>
        /// <param name="methodBody">The method body</param>
        /// <param name="refMethodCount">The number of reference methods found in the code.</param>
        /// <param name="header">The header.</param>
        /// <returns>int</returns>
        public static int Method_Maintainability(string methodBody, int refMethodCount, MethodNTHeader_ header = null)
        {
            // Ideas:  Lines of code; parameters; complexity rating;
            int complexity = Method_Complexity(methodBody);

            // Parameters
            double parameters = (header == null) ? 0 : header.Header_Parameters.Count;

            // Lines
            double lineBreaks = Regex.Matches(methodBody, @"\r\n").Count - (refMethodCount * 2);

            // Factor (from parameters and lines)
            double factor = lineBreaks / 10 + parameters / 3;

            int result = (int)(complexity * factor);

            return(result);
        }