From 1169387b1a310f8df8882b183290eabf6bcd4280 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 27 Dec 2019 12:49:33 +0800 Subject: [PATCH] feat: add method num filter --- README.md | 4 + cmd/api.go | 10 +- cmd/evaluate.go | 11 +++ core/domain/evaluate/analyser.go | 5 + core/domain/evaluate/evaluator/models.go | 2 + docs/patterns/evaluate.md | 116 +++++++++++++++++++++++ 6 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 docs/patterns/evaluate.md diff --git a/README.md b/README.md index 0c72dc2..2d7a3d2 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ Todo: - Evaluate API - [ ] Average Method Length - [ ] Average Class Method Count + - Date Collections + - [ ] monolithic + - [ ] microservice + - [ ] big data ## Usage diff --git a/cmd/api.go b/cmd/api.go index 0d97ce3..ca8e9ae 100644 --- a/cmd/api.go +++ b/cmd/api.go @@ -1,14 +1,14 @@ package cmd import ( + "encoding/json" + "github.com/olekukonko/tablewriter" "github.com/phodal/coca/config" "github.com/phodal/coca/core/adapter" . "github.com/phodal/coca/core/adapter/api" "github.com/phodal/coca/core/domain/call_graph" "github.com/phodal/coca/core/models" . "github.com/phodal/coca/core/support" - "encoding/json" - "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" "log" "os" @@ -31,11 +31,11 @@ type ApiCmdConfig struct { var ( apiCmdConfig ApiCmdConfig - restApis []RestApi + restApis []RestApi - identifiers = adapter.LoadIdentify(apiCmdConfig.DependencePath) + identifiers = adapter.LoadIdentify(apiCmdConfig.DependencePath) identifiersMap = adapter.BuildIdentifierMap(identifiers) - diMap = adapter.BuildDIMap(identifiers, identifiersMap) + diMap = adapter.BuildDIMap(identifiers, identifiersMap) ) var apiCmd = &cobra.Command{ diff --git a/cmd/evaluate.go b/cmd/evaluate.go index f3fd18a..4296cf5 100644 --- a/cmd/evaluate.go +++ b/cmd/evaluate.go @@ -61,6 +61,12 @@ var evaluateCmd = &cobra.Command{ staticCount := result.Summary.StaticMethodCount table.Append([]string{"Static Method", strconv.Itoa(staticCount), "Method", strconv.Itoa(methodCount), Percent(utilsCount, methodCount)}) + table.Append([]string{"Average Method Num", strconv.Itoa(methodCount), "Method/Class", strconv.Itoa(classCount), Rate(methodCount, classCount)}) + + totalLength := result.Summary.TotalMethodLength + normalMethodCount := result.Summary.NormalMethodCount + table.Append([]string{"Average Method Length", strconv.Itoa(totalLength), "Without Getter/Setter", strconv.Itoa(normalMethodCount), Rate(totalLength, normalMethodCount)}) + table.Render() }, } @@ -70,6 +76,11 @@ func Percent(pcent int, all int) string { return fmt.Sprintf("%3.2f%%", percent) } +func Rate(pcent int, all int) string { + percent := float64(pcent) / float64(all) + return fmt.Sprintf("%f", percent) +} + func init() { rootCmd.AddCommand(evaluateCmd) diff --git a/core/domain/evaluate/analyser.go b/core/domain/evaluate/analyser.go index 5adb0cb..f075f98 100644 --- a/core/domain/evaluate/analyser.go +++ b/core/domain/evaluate/analyser.go @@ -46,6 +46,11 @@ func (a Analyser) Analysis(classNodes []models.JClassNode, identifiers []models. if support.Contains(method.Modifiers, "static") { result.Summary.StaticMethodCount++ } + + if !strings.HasPrefix(method.Name, "set") && !strings.HasPrefix(method.Name, "get") { + result.Summary.NormalMethodCount++ + result.Summary.TotalMethodLength = result.Summary.TotalMethodLength + method.StopLine - method.StartLine + 1 + } } } diff --git a/core/domain/evaluate/evaluator/models.go b/core/domain/evaluate/evaluator/models.go index d358327..d90410f 100644 --- a/core/domain/evaluate/evaluator/models.go +++ b/core/domain/evaluate/evaluator/models.go @@ -20,6 +20,8 @@ type Summary struct { UtilsCount int ClassCount int MethodCount int + NormalMethodCount int + TotalMethodLength int StaticMethodCount int } diff --git a/docs/patterns/evaluate.md b/docs/patterns/evaluate.md new file mode 100644 index 0000000..2379177 --- /dev/null +++ b/docs/patterns/evaluate.md @@ -0,0 +1,116 @@ +url: https://github.com/macrozheng/mall + +``` ++------------------------+-------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+-------+-----------------------+-------+-----------+ +| Nullable / Return Null | 22 | Method | 12682 | 0.17% | +| Utils | 2 | Class | 458 | 0.44% | +| Static Method | 7 | Method | 12682 | 0.02% | +| Average Method Num | 12682 | Method/Class | 458 | 27.689956 | +| Average Method Length | 45091 | Without Getter/Setter | 10205 | 4.418520 | ++------------------------+-------+-----------------------+-------+-----------+ +``` + +// issues: big data class / god service + +url: https://github.com/shuzheng/zheng + +``` ++------------------------+-------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+-------+-----------------------+-------+-----------+ +| Nullable / Return Null | 0 | Method | 5256 | 0.00% | +| Utils | 18 | Class | 366 | 4.92% | +| Static Method | 0 | Method | 5256 | 0.34% | +| Average Method Num | 5256 | Method/Class | 366 | 14.360656 | +| Average Method Length | 19644 | Without Getter/Setter | 4328 | 4.538817 | ++------------------------+-------+-----------------------+-------+-----------+ +``` + +// data class + +big data + +``` ++------------------------+-------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+-------+-----------------------+-------+-----------+ +| Nullable / Return Null | 128 | Method | 3041 | 4.21% | +| Utils | 18 | Class | 496 | 7.06% | +| Static Method | 400 | Method | 3041 | 1.15% | +| Average Method Num | 3041 | Method/Class | 496 | 6.13 | +| Average Method Length | 17730 | Without Getter/Setter | 1551 | 11.43 | ++------------------------+-------+-----------------------+-------+-----------+ +``` + +// null issues + +Common Project + +``` ++------------------------+-------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+-------+-----------------------+-------+-----------+ +| Nullable / Return Null | 234 | Method | 16642 | 1.41% | +| Utils | 26 | Class | 1007 | 7.06% | +| Static Method | 2062 | Method | 16642 | 0.16% | +| Average Method Num | 16642 | Method/Class | 1007 | 16.52 | +| Average Method Length | 69012 | Without Getter/Setter | 6020 | 11.46 | ++------------------------+-------+-----------------------+-------+-----------+ +``` + +// static projects + +Algo Project + +``` ++------------------------+-------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+-------+-----------------------+-------+-----------+ +| Nullable / Return Null | 101 | Method | 4914 | 2.06% | +| Utils | 43 | Class | 926 | 4.64% | +| Static Method | 542 | Method | 4914 | 0.88% | +| Average Method Num | 4914 | Method/Class | 927 | 5.30 | +| Average Method Length | 51056 | Without Getter/Setter | 2603 | 19.61 | ++------------------------+-------+-----------------------+-------+-----------+ +``` + +// longest method + +CMS + +url: https://github.com/sanluan/PublicCMS + +``` ++------------------------+-------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+-------+-----------------------+-------+-----------+ +| Nullable / Return Null | 131 | Method | 2707 | 4.84% | +| Utils | 21 | Class | 483 | 4.35% | +| Static Method | 246 | Method | 2707 | 0.78% | +| Average Method Num | 2707 | Method/Class | 483 | 5.604555 | +| Average Method Length | 13622 | Without Getter/Setter | 1350 | 10.090370 | ++------------------------+-------+-----------------------+-------+-----------+ +``` + +// to many null + +ERP + +url: https://github.com/ilscipio/scipio-erp + +``` ++------------------------+--------+-----------------------+-------+-----------+ +| TYPE | COUNT | LEVEL | TOTAL | RATE | ++------------------------+--------+-----------------------+-------+-----------+ +| Nullable / Return Null | 1660 | Method | 23461 | 7.08% | +| Utils | 86 | Class | 2288 | 3.76% | +| Static Method | 6469 | Method | 23461 | 0.37% | +| Average Method Num | 23461 | Method/Class | 2288 | 10.253934 | +| Average Method Length | 228550 | Without Getter/Setter | 14080 | 16.232244 | ++------------------------+--------+-----------------------+-------+-----------+ +``` + +// longest method +// to many null (bugs) \ No newline at end of file -- GitLab