Documentation
¶
Overview ¶
Package cohesion provides cohesion functionality.
Index ¶
- Constants
- Variables
- func RegisterPlotSections()
- type AggregateData
- type AggregateMetric
- type Aggregator
- type Analyzer
- func (c *Analyzer) Analyze(root *node.Node) (analyze.Report, error)
- func (c *Analyzer) Configure(_ map[string]any) error
- func (c *Analyzer) CreateAggregator() analyze.ResultAggregator
- func (c *Analyzer) CreateReportSection(report analyze.Report) analyze.ReportSection
- func (c *Analyzer) CreateVisitor() analyze.AnalysisVisitor
- func (c *Analyzer) Description() string
- func (c *Analyzer) Descriptor() analyze.Descriptor
- func (c *Analyzer) Flag() string
- func (c *Analyzer) FormatReport(report analyze.Report, w io.Writer) error
- func (c *Analyzer) FormatReportBinary(report analyze.Report, w io.Writer) error
- func (c *Analyzer) FormatReportJSON(report analyze.Report, w io.Writer) error
- func (c *Analyzer) FormatReportPlot(report analyze.Report, w io.Writer) error
- func (c *Analyzer) FormatReportYAML(report analyze.Report, w io.Writer) error
- func (c *Analyzer) ListConfigurationOptions() []pipeline.ConfigurationOption
- func (c *Analyzer) Name() string
- func (c *Analyzer) Thresholds() analyze.Thresholds
- type ComputedMetrics
- type DistributionMetric
- type Function
- type FunctionCohesionData
- type FunctionCohesionMetric
- type FunctionData
- type FunctionReportItem
- type LowCohesionFunctionData
- type LowCohesionFunctionMetric
- type ReportData
- type ReportSection
- type Visitor
Constants ¶
const ( MetricDistExcellent = "excellent" MetricDistGood = "good" MetricDistFair = "fair" MetricDistPoor = "poor" )
MetricDist* constants are JSON-compatible distribution keys for metrics output.
const ( CohesionThresholdExcellent = 0.6 CohesionThresholdGood = 0.4 CohesionThresholdFair = 0.3 )
Cohesion quality thresholds.
const ( SectionTitle = "COHESION" // MetricTotalFunctions and related constants define metric labels. MetricTotalFunctions = "Total Functions" MetricLCOM = "LCOM Score" MetricCohesionScore = "Cohesion Score" MetricFunctionCohesion = "Avg Cohesion" // DistExcellentMin and related constants define distribution thresholds for function cohesion. DistExcellentMin = 0.6 DistGoodMin = 0.4 DistFairMin = 0.3 DistLabelExcellent = "Excellent (>0.6)" DistLabelGood = "Good (0.4-0.6)" DistLabelFair = "Fair (0.3-0.4)" DistLabelPoor = "Poor (<0.3)" // IssueSeverityFairMax and related constants define issue severity thresholds. IssueSeverityFairMax = 0.4 IssueSeverityPoorMax = 0.3 IssueValuePrefix = "cohesion=" // KeyTotalFunctions and related constants define report key names. KeyTotalFunctions = "total_functions" KeyLCOM = "lcom" KeyCohesionScore = "cohesion_score" KeyFunctionCohesion = "function_cohesion" KeyMessage = "message" KeyFunctions = "functions" KeyFuncName = "name" KeyFuncCohesion = "cohesion" // DefaultStatusMessage is the default status message. DefaultStatusMessage = "No cohesion data available" )
Section rendering constants.
const (
MaxDepthValue = 10
)
MaxDepthValue is the default maximum UAST traversal depth for cohesion analysis.
Variables ¶
var ErrInvalidFunctions = errors.New("invalid cohesion report: expected []map[string]any for functions")
ErrInvalidFunctions indicates the report doesn't contain expected functions data.
Functions ¶
func RegisterPlotSections ¶
func RegisterPlotSections()
RegisterPlotSections registers the cohesion plot section renderer with the analyze package.
Types ¶
type AggregateData ¶
type AggregateData struct {
TotalFunctions int `json:"total_functions" yaml:"total_functions"`
LCOM float64 `json:"lcom" yaml:"lcom"`
LCOMVariant string `json:"lcom_variant" yaml:"lcom_variant"`
CohesionScore float64 `json:"cohesion_score" yaml:"cohesion_score"`
FunctionCohesion float64 `json:"function_cohesion" yaml:"function_cohesion"`
HealthScore float64 `json:"health_score" yaml:"health_score"`
Message string `json:"message" yaml:"message"`
}
AggregateData contains summary statistics.
type AggregateMetric ¶
type AggregateMetric struct {
metrics.MetricMeta
}
AggregateMetric computes summary statistics.
func NewAggregateMetric ¶
func NewAggregateMetric() *AggregateMetric
NewAggregateMetric creates the aggregate metric.
func (*AggregateMetric) Compute ¶
func (m *AggregateMetric) Compute(input *ReportData) AggregateData
Compute calculates aggregate statistics.
type Aggregator ¶
type Aggregator struct {
*common.Aggregator
common.PerFileRetainer
}
Aggregator aggregates results from multiple cohesion analyses.
type Analyzer ¶
type Analyzer struct {
// contains filtered or unexported fields
}
Analyzer performs cohesion analysis on UAST.
func NewAnalyzer ¶
func NewAnalyzer() *Analyzer
NewAnalyzer creates a new Analyzer with generic components.
func (*Analyzer) CreateAggregator ¶
func (c *Analyzer) CreateAggregator() analyze.ResultAggregator
CreateAggregator creates a new aggregator for cohesion analysis.
func (*Analyzer) CreateReportSection ¶
func (c *Analyzer) CreateReportSection(report analyze.Report) analyze.ReportSection
CreateReportSection creates a ReportSection from report data.
func (*Analyzer) CreateVisitor ¶
func (c *Analyzer) CreateVisitor() analyze.AnalysisVisitor
CreateVisitor creates a new visitor for cohesion analysis.
func (*Analyzer) Description ¶
Description returns the analyzer description.
func (*Analyzer) Descriptor ¶
func (c *Analyzer) Descriptor() analyze.Descriptor
Descriptor returns stable analyzer metadata.
func (*Analyzer) FormatReport ¶
FormatReport formats the analysis report for display.
func (*Analyzer) FormatReportBinary ¶
FormatReportBinary formats cohesion analysis results as binary envelope.
func (*Analyzer) FormatReportJSON ¶
FormatReportJSON formats the analysis report as JSON.
func (*Analyzer) FormatReportPlot ¶
FormatReportPlot generates an HTML plot visualization for cohesion analysis.
func (*Analyzer) FormatReportYAML ¶
FormatReportYAML formats the analysis report as YAML.
func (*Analyzer) ListConfigurationOptions ¶
func (c *Analyzer) ListConfigurationOptions() []pipeline.ConfigurationOption
ListConfigurationOptions returns the configuration options for the analyzer.
func (*Analyzer) Thresholds ¶
func (c *Analyzer) Thresholds() analyze.Thresholds
Thresholds returns the color-coded thresholds for cohesion metrics. LCOM-HS: lower is better (0 = perfect cohesion, 1 = no cohesion). Cohesion score and function cohesion: higher is better.
type ComputedMetrics ¶
type ComputedMetrics struct {
FunctionCohesion []FunctionCohesionData `json:"function_cohesion" yaml:"function_cohesion"`
Distribution map[string]int `json:"distribution" yaml:"distribution"`
LowCohesionFunctions []LowCohesionFunctionData `json:"low_cohesion_functions" yaml:"low_cohesion_functions"`
Aggregate AggregateData `json:"aggregate" yaml:"aggregate"`
}
ComputedMetrics holds all computed metric results for the cohesion analyzer.
func ComputeAllMetrics ¶
func ComputeAllMetrics(report analyze.Report) (*ComputedMetrics, error)
ComputeAllMetrics runs all cohesion metrics and returns the results.
func (*ComputedMetrics) AnalyzerName ¶
func (m *ComputedMetrics) AnalyzerName() string
AnalyzerName returns the name of the analyzer that produced these metrics.
func (*ComputedMetrics) ToJSON ¶
func (m *ComputedMetrics) ToJSON() any
ToJSON returns the metrics in a format suitable for JSON marshaling.
func (*ComputedMetrics) ToYAML ¶
func (m *ComputedMetrics) ToYAML() any
ToYAML returns the metrics in a format suitable for YAML marshaling.
type DistributionMetric ¶
type DistributionMetric struct {
metrics.MetricMeta
}
DistributionMetric computes cohesion distribution.
func NewDistributionMetric ¶
func NewDistributionMetric() *DistributionMetric
NewDistributionMetric creates the distribution metric.
func (*DistributionMetric) Compute ¶
func (m *DistributionMetric) Compute(input *ReportData) map[string]int
Compute calculates cohesion distribution.
type FunctionCohesionData ¶
type FunctionCohesionData struct {
Name string `json:"name" yaml:"name"`
SourceFile string `json:"source_file,omitempty" yaml:"source_file,omitempty"`
Language string `json:"language,omitempty" yaml:"language,omitempty"`
Directory string `json:"directory,omitempty" yaml:"directory,omitempty"`
Cohesion float64 `json:"cohesion" yaml:"cohesion"`
QualityLevel string `json:"quality_level" yaml:"quality_level"`
}
FunctionCohesionData contains cohesion data for a function.
type FunctionCohesionMetric ¶
type FunctionCohesionMetric struct {
metrics.MetricMeta
}
FunctionCohesionMetric computes per-function cohesion data.
func NewFunctionCohesionMetric ¶
func NewFunctionCohesionMetric() *FunctionCohesionMetric
NewFunctionCohesionMetric creates the function cohesion metric.
func (*FunctionCohesionMetric) Compute ¶
func (m *FunctionCohesionMetric) Compute(input *ReportData) []FunctionCohesionData
Compute calculates function cohesion data.
type FunctionData ¶
type FunctionData struct {
Name string
SourceFile string
Language string
Directory string
Cohesion float64
}
FunctionData holds cohesion data for a single function.
type FunctionReportItem ¶
type FunctionReportItem struct {
Name string
CohesionAssessment string
VariableAssessment string
SizeAssessment string
LineCount int
VariableCount int
Cohesion float64
}
FunctionReportItem is a typed representation of a per-function cohesion report item.
type LowCohesionFunctionData ¶
type LowCohesionFunctionData struct {
Name string `json:"name" yaml:"name"`
SourceFile string `json:"source_file,omitempty" yaml:"source_file,omitempty"`
Language string `json:"language,omitempty" yaml:"language,omitempty"`
Directory string `json:"directory,omitempty" yaml:"directory,omitempty"`
Cohesion float64 `json:"cohesion" yaml:"cohesion"`
RiskLevel string `json:"risk_level" yaml:"risk_level"`
Recommendation string `json:"recommendation" yaml:"recommendation"`
}
LowCohesionFunctionData identifies functions with poor cohesion.
type LowCohesionFunctionMetric ¶
type LowCohesionFunctionMetric struct {
metrics.MetricMeta
}
LowCohesionFunctionMetric identifies functions needing attention.
func NewLowCohesionFunctionMetric ¶
func NewLowCohesionFunctionMetric() *LowCohesionFunctionMetric
NewLowCohesionFunctionMetric creates the low cohesion metric.
func (*LowCohesionFunctionMetric) Compute ¶
func (m *LowCohesionFunctionMetric) Compute(input *ReportData) []LowCohesionFunctionData
Compute identifies low cohesion functions.
type ReportData ¶
type ReportData struct {
TotalFunctions int
LCOM float64
CohesionScore float64
FunctionCohesion float64
Functions []FunctionData
Message string
}
ReportData is the parsed input data for cohesion metrics computation.
func ParseReportData ¶
func ParseReportData(report analyze.Report) (*ReportData, error)
ParseReportData extracts ReportData from an analyzer report.
type ReportSection ¶
type ReportSection struct {
analyze.BaseReportSection
// contains filtered or unexported fields
}
ReportSection implements analyze.ReportSection for cohesion analysis.
func NewReportSection ¶
func NewReportSection(report analyze.Report) *ReportSection
NewReportSection creates a ReportSection from a cohesion report.
func (*ReportSection) AllIssues ¶
func (s *ReportSection) AllIssues() []analyze.Issue
AllIssues returns all functions sorted by cohesion ascending (worst first).
func (*ReportSection) Distribution ¶
func (s *ReportSection) Distribution() []analyze.DistributionItem
Distribution returns cohesion distribution categories.
func (*ReportSection) KeyMetrics ¶
func (s *ReportSection) KeyMetrics() []analyze.Metric
KeyMetrics returns the key metrics for the cohesion section.