gocyclo

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2020 License: BSD-3-Clause Imports: 10 Imported by: 18

README

PkgGoDev Go Report Card

Gocyclo calculates cyclomatic complexities of functions in Go source code.

The cyclomatic complexity of a function is calculated according to the following rules:

 1 is the base complexity of a function
+1 for each 'if', 'for', 'case', '&&' or '||'

To install, run

$ go get github.com/fzipp/gocyclo/cmd/gocyclo

and put the resulting binary in one of your PATH directories if $GOPATH/bin isn't already in your PATH.

Usage:

$ gocyclo [<flag> ...] <Go file or directory> ...

Examples:

$ gocyclo .
$ gocyclo main.go
$ gocyclo -top 10 src/
$ gocyclo -over 25 docker
$ gocyclo -avg .
$ gocyclo -top 20 -ignore "_test|Godeps|vendor/" .

The output fields for each line are:

<complexity> <package> <function> <file:row:column>

Individual functions can be ignored with a gocyclo:ignore directive:

//gocyclo:ignore
func f() {
    // ...
}

//gocyclo:ignore
var g = func() {
}

Documentation

Overview

Package gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Complexity

func Complexity(fn ast.Node) int

Complexity calculates the cyclomatic complexity of a function. The 'fn' node is either a *ast.FuncDecl or a *ast.FuncLit.

Types

type Stat

type Stat struct {
	PkgName    string
	FuncName   string
	Complexity int
	Pos        token.Position
}

Stat holds the cyclomatic complexity of a function, along with its package and and function name and its position in the source code.

func (Stat) String

func (s Stat) String() string

String formats the cyclomatic complexity information of a function in the following format: "<complexity> <package> <function> <file:row:column>"

type Stats

type Stats []Stat

Stats hold the cyclomatic complexities of many functions.

func Analyze

func Analyze(paths []string, ignore *regexp.Regexp) Stats

Analyze calculates the cyclomatic complexities of the functions and methods in the Go source code files in the given paths. If a path is a directory all Go files under that directory are analyzed recursively. Files with paths matching the 'ignore' regular expressions are skipped. The 'ignore' parameter can be nil, meaning that no files are skipped.

func AnalyzeASTFile added in v0.3.0

func AnalyzeASTFile(f *ast.File, fs *token.FileSet, s Stats) Stats

AnalyzeASTFile calculates the cyclomatic complexities of the functions and methods in the abstract syntax tree (AST) of a parsed Go file and appends the results to the given Stats slice.

func (Stats) AverageComplexity

func (s Stats) AverageComplexity() float64

AverageComplexity calculates the average cyclomatic complexity of the cyclomatic complexities in s.

func (Stats) SortAndFilter

func (s Stats) SortAndFilter(top, over int) Stats

SortAndFilter sorts the cyclomatic complexities in s in descending order and returns a slice of s limited to the 'top' N entries with a cyclomatic complexity greater than 'over'. If 'top' is negative, i.e. -1, it does not limit the result. If 'over' is <= 0 it does not limit the result either, because a function has a base cyclomatic complexity of at least 1.

func (Stats) TotalComplexity

func (s Stats) TotalComplexity() uint64

TotalComplexity calculates the total sum of all cyclomatic complexities in s.

Directories

Path Synopsis
cmd
gocyclo command
Gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.
Gocyclo calculates the cyclomatic complexities of functions and methods in Go source code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL