1
0
mirror of https://github.com/fzipp/gocyclo.git synced 2025-02-06 14:10:08 +00:00

Refactoring: implement AverageComplexity and TotalComplexity as methods on Stats

This commit is contained in:
Frederik Zipp 2020-10-17 02:12:41 +02:00
parent 56589e6586
commit 62951d8050
3 changed files with 31 additions and 42 deletions

View File

@ -60,13 +60,12 @@ func main() {
}
allStats := gocyclo.Analyze(paths)
shownStats := gocyclo.SortAndFilterStats(allStats, *top, *over)
printStats(shownStats)
shownStats := allStats.SortAndFilter(*top, *over)
printStats(shownStats)
if *avg {
printAverage(allStats)
}
if *total {
printTotal(allStats)
}
@ -76,6 +75,20 @@ func main() {
}
}
func printStats(s gocyclo.Stats) {
for _, stat := range s {
fmt.Println(stat)
}
}
func printAverage(s gocyclo.Stats) {
fmt.Printf("Average: %.3g\n", s.AverageComplexity())
}
func printTotal(s gocyclo.Stats) {
fmt.Printf("Total: %d\n", s.TotalComplexity())
}
func usage() {
_, _ = fmt.Fprintf(os.Stderr, usageDoc)
os.Exit(2)

View File

@ -1,36 +0,0 @@
// Copyright 2020 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/fzipp/gocyclo"
)
func printStats(stats []gocyclo.Stat) {
for _, stat := range stats {
fmt.Println(stat)
}
}
func printAverage(stats []gocyclo.Stat) {
fmt.Printf("Average: %.3g\n", average(stats))
}
func printTotal(stats []gocyclo.Stat) {
fmt.Printf("Total: %d\n", sumTotal(stats))
}
func average(stats []gocyclo.Stat) float64 {
return float64(sumTotal(stats)) / float64(len(stats))
}
func sumTotal(stats []gocyclo.Stat) uint64 {
total := uint64(0)
for _, s := range stats {
total += uint64(s.Complexity)
}
return total
}

View File

@ -23,9 +23,21 @@ func (s Stat) String() string {
type Stats []Stat
func SortAndFilterStats(stats Stats, top, over int) Stats {
result := make(Stats, len(stats))
copy(result, stats)
func (s Stats) AverageComplexity() float64 {
return float64(s.TotalComplexity()) / float64(len(s))
}
func (s Stats) TotalComplexity() uint64 {
total := uint64(0)
for _, stat := range s {
total += uint64(stat.Complexity)
}
return total
}
func (s Stats) SortAndFilter(top, over int) Stats {
result := make(Stats, len(s))
copy(result, s)
sort.Sort(byComplexityDesc(result))
for i, stat := range result {
if i == top {