churn_vs_complexity 1.8.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +46 -23
  4. data/bin/churn_vs_complexity +6 -1
  5. data/lib/churn_vs_complexity/analysis_scope.rb +20 -0
  6. data/lib/churn_vs_complexity/churn.rb +41 -8
  7. data/lib/churn_vs_complexity/cli/main.rb +58 -19
  8. data/lib/churn_vs_complexity/cli/parser.rb +85 -121
  9. data/lib/churn_vs_complexity/cli.rb +2 -3
  10. data/lib/churn_vs_complexity/complexity/flog_calculator.rb +0 -3
  11. data/lib/churn_vs_complexity/complexity/lizard_calculator.rb +64 -0
  12. data/lib/churn_vs_complexity/complexity.rb +1 -7
  13. data/lib/churn_vs_complexity/complexity_validator.rb +1 -16
  14. data/lib/churn_vs_complexity/concurrent_calculator.rb +20 -23
  15. data/lib/churn_vs_complexity/delta/checker.rb +35 -21
  16. data/lib/churn_vs_complexity/delta/commit_hydrator.rb +1 -1
  17. data/lib/churn_vs_complexity/delta/complexity_annotator.rb +23 -5
  18. data/lib/churn_vs_complexity/delta/config.rb +15 -16
  19. data/lib/churn_vs_complexity/delta/factory.rb +2 -2
  20. data/lib/churn_vs_complexity/delta/multi_checker.rb +24 -25
  21. data/lib/churn_vs_complexity/delta/serializer.rb +7 -4
  22. data/lib/churn_vs_complexity/delta.rb +11 -49
  23. data/lib/churn_vs_complexity/diff/checker.rb +10 -3
  24. data/lib/churn_vs_complexity/diff/config.rb +29 -20
  25. data/lib/churn_vs_complexity/file_selector.rb +63 -114
  26. data/lib/churn_vs_complexity/focus/config.rb +42 -32
  27. data/lib/churn_vs_complexity/focus/serializer.rb +17 -4
  28. data/lib/churn_vs_complexity/gate/config.rb +35 -25
  29. data/lib/churn_vs_complexity/gate/serializer.rb +9 -2
  30. data/lib/churn_vs_complexity/git_strategy.rb +29 -10
  31. data/lib/churn_vs_complexity/hotspots/config.rb +29 -25
  32. data/lib/churn_vs_complexity/language_registry.rb +108 -0
  33. data/lib/churn_vs_complexity/language_validator.rb +1 -3
  34. data/lib/churn_vs_complexity/normal/config.rb +31 -100
  35. data/lib/churn_vs_complexity/normal/serializer/graph.rb +3 -2
  36. data/lib/churn_vs_complexity/normal/serializer/summary.rb +18 -14
  37. data/lib/churn_vs_complexity/normal/serializer/summary_hash.rb +36 -21
  38. data/lib/churn_vs_complexity/normal/serializer.rb +2 -1
  39. data/lib/churn_vs_complexity/normal.rb +17 -13
  40. data/lib/churn_vs_complexity/risk_classifier.rb +3 -19
  41. data/lib/churn_vs_complexity/timetravel/config.rb +34 -47
  42. data/lib/churn_vs_complexity/timetravel/schedule.rb +15 -0
  43. data/lib/churn_vs_complexity/timetravel/serializer/stats_calculator.rb +36 -32
  44. data/lib/churn_vs_complexity/timetravel/serializer.rb +16 -24
  45. data/lib/churn_vs_complexity/timetravel/traveller.rb +46 -15
  46. data/lib/churn_vs_complexity/timetravel.rb +36 -14
  47. data/lib/churn_vs_complexity/triage/checker.rb +24 -28
  48. data/lib/churn_vs_complexity/triage/collaborator_set.rb +11 -0
  49. data/lib/churn_vs_complexity/triage/config.rb +29 -26
  50. data/lib/churn_vs_complexity/triage.rb +1 -0
  51. data/lib/churn_vs_complexity/validator_set.rb +9 -0
  52. data/lib/churn_vs_complexity/version.rb +1 -1
  53. data/lib/churn_vs_complexity.rb +3 -0
  54. metadata +18 -59
  55. data/.devcontainer/devcontainer.json +0 -26
  56. data/.rubocop.yml +0 -25
  57. data/CLAUDE.md +0 -60
  58. data/Dockerfile +0 -34
  59. data/Dockerfile.dev +0 -27
  60. data/Rakefile +0 -6
  61. data/TODO +0 -11
  62. data/lib/churn_vs_complexity/complexity/eslint_calculator.rb +0 -40
  63. data/lib/churn_vs_complexity/complexity/go_calculator.rb +0 -48
  64. data/lib/churn_vs_complexity/complexity/kotlin_calculator.rb +0 -54
  65. data/lib/churn_vs_complexity/complexity/pmd/files_calculator.rb +0 -26
  66. data/lib/churn_vs_complexity/complexity/pmd/folder_calculator.rb +0 -20
  67. data/lib/churn_vs_complexity/complexity/pmd.rb +0 -54
  68. data/lib/churn_vs_complexity/complexity/python_calculator.rb +0 -50
  69. data/lib/churn_vs_complexity/complexity/rust_calculator.rb +0 -54
  70. data/lib/churn_vs_complexity/complexity/swift_calculator.rb +0 -54
  71. data/package-lock.json +0 -6
  72. data/tmp/eslint-support/complexity-calculator.js +0 -51
  73. data/tmp/eslint-support/package.json +0 -11
  74. data/tmp/pmd-support/ruleset.xml +0 -21
  75. data/tmp/test-support/delta/ruby-summary.txt +0 -55
  76. data/tmp/test-support/delta/ruby.csv +0 -12
  77. data/tmp/test-support/go/main.go +0 -11
  78. data/tmp/test-support/go/utils.go +0 -13
  79. data/tmp/test-support/java/small-example/src/main/java/org/example/Main.java +0 -10
  80. data/tmp/test-support/java/small-example/src/main/java/org/example/spice/Checker.java +0 -28
  81. data/tmp/test-support/javascript/complex.js +0 -43
  82. data/tmp/test-support/javascript/moderate.js +0 -12
  83. data/tmp/test-support/javascript/simple.js +0 -5
  84. data/tmp/test-support/javascript/typescript-example.ts +0 -26
  85. data/tmp/test-support/kotlin/Main.kt +0 -7
  86. data/tmp/test-support/kotlin/Utils.kt +0 -11
  87. data/tmp/test-support/python/example.py +0 -6
  88. data/tmp/test-support/python/utils.py +0 -9
  89. data/tmp/test-support/rust/main.rs +0 -7
  90. data/tmp/test-support/rust/utils.rs +0 -20
  91. data/tmp/test-support/swift/main.swift +0 -7
  92. data/tmp/test-support/swift/utils.swift +0 -24
  93. data/tmp/test-support/txt/abc.txt +0 -0
  94. data/tmp/test-support/txt/d.txt +0 -0
  95. data/tmp/test-support/txt/ef.txt +0 -0
  96. data/tmp/test-support/txt/ghij.txt +0 -0
  97. data/tmp/test-support/txt/klm.txt +0 -0
  98. data/tmp/test-support/txt/nopq.txt +0 -0
  99. data/tmp/test-support/txt/r.txt +0 -0
  100. data/tmp/test-support/txt/st.txt +0 -0
  101. data/tmp/test-support/txt/uvx.txt +0 -0
  102. data/tmp/test-support/txt/yz.txt +0 -0
  103. data/tmp/timetravel/.keep +0 -0
@@ -4,45 +4,55 @@ module ChurnVsComplexity
4
4
  module Gate
5
5
  class Config
6
6
  DEFAULT_MAX_GAMMA = 25
7
+ SUPPORTED_SERIALIZERS = %i[json].freeze
7
8
 
8
- def initialize(
9
- language:,
10
- serializer: :json,
11
- max_gamma: DEFAULT_MAX_GAMMA,
12
- since: nil,
13
- excluded: [],
14
- complexity_validator: ComplexityValidator,
15
- since_validator: Normal::SinceValidator,
16
- **options
17
- )
18
- @language = language
9
+ def self.from_options(options)
10
+ new(
11
+ scope: AnalysisScope.from_options(options),
12
+ serializer: options[:serializer] || :json,
13
+ max_gamma: options[:max_gamma] || DEFAULT_MAX_GAMMA,
14
+ )
15
+ end
16
+
17
+ def initialize(scope:, serializer: :json, max_gamma: DEFAULT_MAX_GAMMA, validators: ValidatorSet.new)
18
+ @scope = scope
19
19
  @serializer = serializer
20
20
  @max_gamma = max_gamma
21
- @since = since
22
- @excluded = excluded
23
- @complexity_validator = complexity_validator
24
- @since_validator = since_validator
25
- @options = options
21
+ @validators = validators
26
22
  end
27
23
 
28
24
  def validate!
29
- LanguageValidator.validate!(@language)
30
- @since_validator.validate!(since: @since, relative_period: nil)
31
- @complexity_validator.validate!(@language)
25
+ LanguageValidator.validate!(@scope.language)
26
+ validate_serializer!
27
+ validate_period!
28
+ @validators.complexity.validate!(@scope.language)
29
+ validate_max_gamma!
32
30
  end
33
31
 
34
32
  def checker
35
- normal_config = Normal::Config.new(
36
- language: @language,
37
- serializer: :none,
38
- since: @since,
39
- excluded: @excluded,
40
- )
33
+ normal_config = Normal::Config.new(scope: @scope, serializer: :none)
41
34
  Checker.new(engine: normal_config.checker, serializer: gate_serializer, max_gamma: @max_gamma)
42
35
  end
43
36
 
44
37
  private
45
38
 
39
+ def validate_serializer!
40
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
41
+
42
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
43
+ end
44
+
45
+ def validate_period!
46
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
47
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
48
+ end
49
+
50
+ def validate_max_gamma!
51
+ return unless @max_gamma.negative?
52
+
53
+ raise ValidationError, "Max gamma must not be negative, got #{@max_gamma}"
54
+ end
55
+
46
56
  def gate_serializer
47
57
  case @serializer
48
58
  when :json then Serializer::Json
@@ -24,10 +24,17 @@ module ChurnVsComplexity
24
24
  gamma = GammaScore.calculate(values[0], values[1]).round(2)
25
25
  next unless gamma > max_gamma
26
26
 
27
- exceeds_pct = (((gamma - max_gamma) / max_gamma) * 100).round(0)
28
- { file:, gamma_score: gamma, exceeds_by: "#{exceeds_pct}%" }
27
+ { file:, gamma_score: gamma, exceeds_by: excess(gamma, max_gamma) }
29
28
  end
30
29
  end
30
+
31
+ # There is no percentage by which a score exceeds zero, so a zero
32
+ # threshold reports the violation without one.
33
+ def excess(gamma, max_gamma)
34
+ return nil if max_gamma.zero?
35
+
36
+ "#{(((gamma - max_gamma) / max_gamma) * 100).round(0)}%"
37
+ end
31
38
  end
32
39
  end
33
40
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'open3'
4
+
3
5
  module ChurnVsComplexity
4
6
  class GitStrategy
5
7
  EMPTY_TREE_SHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
@@ -12,7 +14,7 @@ module ChurnVsComplexity
12
14
  def valid_commit?(commit:)
13
15
  @repo.object(commit)
14
16
  true
15
- rescue Git::GitExecuteError
17
+ rescue Git::Error
16
18
  false
17
19
  end
18
20
 
@@ -22,8 +24,8 @@ module ChurnVsComplexity
22
24
 
23
25
  def surrounding(commit:)
24
26
  current = object(commit)
25
- child_sha = `git -C #{@folder} log --reverse --ancestry-path #{current.sha}..HEAD --format=%H`.lines.first&.chomp
26
- next_commit = child_sha&.empty? ? nil : child_sha
27
+ child_sha = next_commit_sha(current.sha)
28
+ next_commit = child_sha.nil? || child_sha.empty? ? nil : child_sha
27
29
  [current.parent&.sha, next_commit]
28
30
  end
29
31
 
@@ -35,12 +37,24 @@ module ChurnVsComplexity
35
37
  end
36
38
 
37
39
  def checkout_in_worktree(worktree_folder, sha)
38
- command = "(cd #{worktree_folder} && git checkout #{sha}) > /dev/null 2>&1"
39
- `#{command}`
40
+ Open3.capture3('git', '-C', worktree_folder, 'checkout', sha)
41
+ end
42
+
43
+ # Both ends are given an explicit time. A bare date is an approximate date
44
+ # to git, resolved against the clock and the timezone, so the window it
45
+ # produces is not the window that was asked for.
46
+ def commits_in(git_period)
47
+ log = @repo.log(1_000_000).until(end_of_day(git_period.end_date))
48
+ log = log.since(start_of_day(git_period.effective_start_date)) if git_period.requested_start_date
49
+ log.execute.to_a
40
50
  end
41
51
 
52
+ def start_of_day(date) = date.strftime('%Y-%m-%dT00:00:00')
53
+
54
+ def end_of_day(date) = date.strftime('%Y-%m-%dT23:59:59')
55
+
42
56
  def resolve_commits_with_interval(git_period:, jump_days:)
43
- candidates = @repo.log(1_000_000).since(git_period.effective_start_date).until(git_period.end_date).to_a
57
+ candidates = commits_in(git_period)
44
58
 
45
59
  commits_by_date = candidates.filter { |c| c.date.to_date >= git_period.effective_start_date }
46
60
  .group_by { |c| c.date.to_date }
@@ -51,17 +65,22 @@ module ChurnVsComplexity
51
65
  end
52
66
 
53
67
  def add_worktree(wt_folder)
54
- command = "(cd #{@folder} && git worktree add -f #{wt_folder}) > /dev/null 2>&1"
55
- `#{command}`
68
+ Open3.capture3('git', '-C', @folder, 'worktree', 'add', '--detach', '-f', wt_folder)
56
69
  end
57
70
 
58
71
  def remove_worktree(worktree_folder)
59
- command = "(cd #{worktree_folder} && git worktree remove -f #{worktree_folder}) > /dev/null 2>&1"
60
- `#{command}`
72
+ Open3.capture3('git', '-C', @folder, 'worktree', 'remove', '-f', worktree_folder)
61
73
  end
62
74
 
63
75
  private
64
76
 
77
+ def next_commit_sha(sha)
78
+ output, = Open3.capture2(
79
+ 'git', '-C', @folder, 'log', '--reverse', '--ancestry-path', "#{sha}..HEAD", '--format=%H'
80
+ )
81
+ output.lines.first&.chomp
82
+ end
83
+
65
84
  def diff_from_parent(commit_object)
66
85
  base = commit_object.parent
67
86
  base ? base.diff(commit_object) : @repo.diff(EMPTY_TREE_SHA, commit_object.sha)
@@ -3,42 +3,46 @@
3
3
  module ChurnVsComplexity
4
4
  module Hotspots
5
5
  class Config
6
- def initialize(
7
- language:,
8
- serializer: :json,
9
- since: nil,
10
- excluded: [],
11
- complexity_validator: ComplexityValidator,
12
- since_validator: Normal::SinceValidator,
13
- **options
14
- )
15
- @language = language
6
+ SUPPORTED_SERIALIZERS = %i[json markdown].freeze
7
+
8
+ def self.from_options(options)
9
+ new(
10
+ scope: AnalysisScope.from_options(options),
11
+ serializer: options[:serializer] || :json,
12
+ )
13
+ end
14
+
15
+ def initialize(scope:, serializer: :json, validators: ValidatorSet.new)
16
+ @scope = scope
16
17
  @serializer = serializer
17
- @since = since
18
- @excluded = excluded
19
- @complexity_validator = complexity_validator
20
- @since_validator = since_validator
21
- @options = options
18
+ @validators = validators
22
19
  end
23
20
 
24
21
  def validate!
25
- LanguageValidator.validate!(@language)
26
- @since_validator.validate!(since: @since, relative_period: nil)
27
- @complexity_validator.validate!(@language)
22
+ LanguageValidator.validate!(@scope.language)
23
+ validate_serializer!
24
+ validate_period!
25
+ @validators.complexity.validate!(@scope.language)
28
26
  end
29
27
 
30
28
  def checker
31
- normal_config = Normal::Config.new(
32
- language: @language,
33
- serializer: :none,
34
- since: @since,
35
- excluded: @excluded,
36
- )
37
- Checker.new(engine: normal_config.checker, serializer: hotspots_serializer, language: @language)
29
+ normal_config = Normal::Config.new(scope: @scope, serializer: :none)
30
+ Checker.new(engine: normal_config.checker, serializer: hotspots_serializer, language: @scope.language)
38
31
  end
39
32
 
40
33
  private
41
34
 
35
+ def validate_serializer!
36
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
37
+
38
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
39
+ end
40
+
41
+ def validate_period!
42
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
43
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
44
+ end
45
+
42
46
  def hotspots_serializer
43
47
  case @serializer
44
48
  when :json then Serializer::Json
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChurnVsComplexity
4
+ module LanguageRegistry
5
+ module NoopValidator
6
+ def self.check_dependencies! = nil
7
+ end
8
+
9
+ JAVASCRIPT_DEFAULT_EXCLUDES = %w[node_modules].freeze
10
+ PYTHON_DEFAULT_EXCLUDES = %w[venv .venv env .env __pycache__ .tox site-packages].freeze
11
+
12
+ Entry = Data.define(
13
+ :key,
14
+ :label,
15
+ :cli_flags,
16
+ :extensions,
17
+ :default_excludes,
18
+ :calculator,
19
+ :validator,
20
+ :cli_description,
21
+ ) do
22
+ def file_selector
23
+ FileSelector::Factory.new(
24
+ extensions:,
25
+ default_excludes:,
26
+ )
27
+ end
28
+
29
+ def description
30
+ cli_description || "Check complexity of #{label.downcase} files"
31
+ end
32
+ end
33
+
34
+ class << self
35
+ def all = ENTRIES
36
+ def keys = BY_KEY.keys
37
+ def supported?(key) = BY_KEY.key?(key)
38
+
39
+ def fetch(key)
40
+ BY_KEY.fetch(key) { raise ValidationError, "Unsupported language: #{key}" }
41
+ end
42
+
43
+ def file_selector_for(key)
44
+ fetch(key).file_selector
45
+ end
46
+
47
+ def complexity_for(key)
48
+ fetch(key).calculator
49
+ end
50
+
51
+ private
52
+
53
+ def register(key, label, cli_flags, extensions)
54
+ Entry.new(
55
+ key:,
56
+ label:,
57
+ cli_flags: cli_flags.freeze,
58
+ extensions: extensions.freeze,
59
+ default_excludes: [].freeze,
60
+ calculator: Complexity::LizardCalculator,
61
+ validator: Complexity::LizardCalculator,
62
+ cli_description: nil,
63
+ )
64
+ end
65
+ end
66
+
67
+ ENTRIES = [
68
+ register(:java, 'Java', ['--java'], ['.java']).with(
69
+ cli_description: 'Check complexity of java classes',
70
+ ),
71
+ register(:ruby, 'Ruby', ['--ruby'], ['.rb']).with(
72
+ calculator: Complexity::FlogCalculator,
73
+ validator: NoopValidator,
74
+ ),
75
+ register(:javascript, 'JavaScript and TypeScript',
76
+ %w[--js --ts --javascript --typescript --jsx --tsx],
77
+ %w[.js .cjs .mjs .jsx .ts .tsx]).with(
78
+ default_excludes: JAVASCRIPT_DEFAULT_EXCLUDES,
79
+ ),
80
+ register(:python, 'Python', ['--python'], ['.py']).with(
81
+ default_excludes: PYTHON_DEFAULT_EXCLUDES,
82
+ ),
83
+ register(:go, 'Go', ['--go'], ['.go']),
84
+ register(:kotlin, 'Kotlin', ['--kotlin'], ['.kt', '.kts']),
85
+ register(:rust, 'Rust', ['--rust'], ['.rs']),
86
+ register(:swift, 'Swift', ['--swift'], ['.swift']),
87
+ register(:cpp, 'C/C++', %w[--cpp --c++ --c], %w[.c .cpp .cc .cxx .h .hpp]),
88
+ register(:csharp, 'C#', %w[--csharp --cs], %w[.cs]),
89
+ register(:objc, 'Objective-C', %w[--objc --objective-c --objectivec], %w[.m .mm]),
90
+ register(:php, 'PHP', %w[--php], %w[.php]),
91
+ register(:scala, 'Scala', %w[--scala], %w[.scala]),
92
+ register(:gdscript, 'GDScript', %w[--gdscript], %w[.gd]),
93
+ register(:lua, 'Lua', %w[--lua], %w[.lua]),
94
+ register(:fortran, 'Fortran', %w[--fortran], %w[.f70 .f90 .f95 .f03 .f08 .f .for .ftn .fpp]),
95
+ register(:solidity, 'Solidity', %w[--solidity], %w[.sol]),
96
+ register(:erlang, 'Erlang', %w[--erlang], %w[.erl .hrl .es .escript]),
97
+ register(:zig, 'Zig', %w[--zig], %w[.zig]),
98
+ register(:vue, 'Vue', %w[--vue --vuejs], %w[.vue]),
99
+ register(:perl, 'Perl', %w[--perl], %w[.pl .pm]),
100
+ register(:ttcn, 'TTCN-3', %w[--ttcn --ttcn3], %w[.ttcn .ttcnpp]),
101
+ register(:st, 'Structured Text', %w[--st --structured-text], %w[.st]),
102
+ register(:r, 'R', %w[--r], %w[.r .R]),
103
+ register(:plsql, 'PL/SQL', %w[--plsql], %w[.sql .pks .pkb .pls .plb .pck]),
104
+ ].freeze
105
+
106
+ BY_KEY = ENTRIES.to_h { |entry| [entry.key, entry] }.freeze
107
+ end
108
+ end
@@ -2,10 +2,8 @@
2
2
 
3
3
  module ChurnVsComplexity
4
4
  module LanguageValidator
5
- SUPPORTED = %i[java ruby javascript python go kotlin rust swift].freeze
6
-
7
5
  def self.validate!(language)
8
- raise ValidationError, "Unsupported language: #{language}" unless SUPPORTED.include?(language)
6
+ raise ValidationError, "Unsupported language: #{language}" unless LanguageRegistry.supported?(language)
9
7
  end
10
8
  end
11
9
  end
@@ -3,103 +3,40 @@
3
3
  module ChurnVsComplexity
4
4
  module Normal
5
5
  class Config
6
- def initialize(
7
- language:,
8
- serializer:,
9
- excluded: [],
10
- since: nil,
11
- relative_period: nil,
12
- complexity_validator: ComplexityValidator,
13
- since_validator: SinceValidator,
14
- **options
15
- )
16
- @language = language
6
+ def self.from_options(options)
7
+ new(
8
+ scope: AnalysisScope.from_options(options),
9
+ serializer: options[:serializer],
10
+ relative_period: options[:relative_period],
11
+ )
12
+ end
13
+
14
+ def initialize(scope:, serializer:, relative_period: nil, validators: ValidatorSet.new)
15
+ @scope = scope
17
16
  @serializer = serializer
18
- @excluded = excluded
19
- @since = since
20
- @relative_period = relative_period
21
- @complexity_validator = complexity_validator
22
- @since_validator = since_validator
23
- @options = options
17
+ @relative_period = relative_period || scope.relative_period
18
+ @validators = validators
24
19
  end
25
20
 
26
21
  def validate!
27
- LanguageValidator.validate!(@language)
28
-
22
+ LanguageValidator.validate!(@scope.language)
29
23
  SerializerValidator.validate!(serializer: @serializer)
30
-
31
- @since_validator.validate!(since: @since, relative_period: @relative_period)
24
+ @validators.since.validate!(since: @scope.since, relative_period: @relative_period)
32
25
  RelativePeriodValidator.validate!(relative_period: @relative_period)
33
- @complexity_validator.validate!(@language)
26
+ @validators.complexity.validate!(@scope.language)
34
27
  end
35
28
 
36
29
  def checker
37
- case @language
38
- when :java
39
- Engine.concurrent(
40
- complexity: Complexity::PMD::FolderCalculator,
41
- churn:,
42
- file_selector: FileSelector::Java.excluding(@excluded),
43
- serializer:,
44
- since: @since || @relative_period,
45
- )
46
- when :ruby
47
- Engine.concurrent(
48
- complexity: Complexity::FlogCalculator,
49
- churn:,
50
- file_selector: FileSelector::Ruby.excluding(@excluded),
51
- serializer:,
52
- since: @since || @relative_period,
53
- )
54
- when :javascript
55
- Engine.concurrent(
56
- complexity: Complexity::ESLintCalculator,
57
- churn:,
58
- file_selector: FileSelector::JavaScript.excluding(@excluded),
59
- serializer:,
60
- since: @since || @relative_period,
61
- )
62
- when :python
63
- Engine.concurrent(
64
- complexity: Complexity::PythonCalculator,
65
- churn:,
66
- file_selector: FileSelector::Python.excluding(@excluded),
67
- serializer:,
68
- since: @since || @relative_period,
69
- )
70
- when :go
71
- Engine.concurrent(
72
- complexity: Complexity::GoCalculator,
73
- churn:,
74
- file_selector: FileSelector::Go.excluding(@excluded),
75
- serializer:,
76
- since: @since || @relative_period,
77
- )
78
- when :kotlin
79
- Engine.concurrent(
80
- complexity: Complexity::KotlinCalculator,
81
- churn:,
82
- file_selector: FileSelector::Kotlin.excluding(@excluded),
83
- serializer:,
84
- since: @since || @relative_period,
85
- )
86
- when :rust
87
- Engine.concurrent(
88
- complexity: Complexity::RustCalculator,
89
- churn:,
90
- file_selector: FileSelector::Rust.excluding(@excluded),
91
- serializer:,
92
- since: @since || @relative_period,
93
- )
94
- when :swift
95
- Engine.concurrent(
96
- complexity: Complexity::SwiftCalculator,
97
- churn:,
98
- file_selector: FileSelector::Swift.excluding(@excluded),
99
- serializer:,
100
- since: @since || @relative_period,
101
- )
102
- end
30
+ return unless LanguageRegistry.supported?(@scope.language)
31
+
32
+ entry = LanguageRegistry.fetch(@scope.language)
33
+ Engine.concurrent(
34
+ complexity: entry.calculator,
35
+ churn:,
36
+ file_selector: entry.file_selector.excluding(@scope.excluded),
37
+ serializer:,
38
+ since: @scope.since || @relative_period,
39
+ )
103
40
  end
104
41
 
105
42
  private
@@ -108,18 +45,12 @@ module ChurnVsComplexity
108
45
 
109
46
  def serializer
110
47
  case @serializer
111
- when :none
112
- Serializer::None
113
- when :csv
114
- Serializer::CSV
115
- when :graph
116
- Serializer::Graph.new
117
- when :summary
118
- Serializer::Summary
119
- when :pass_through
120
- Serializer::PassThrough
121
- when :json
122
- Serializer::Json
48
+ when :none then Serializer::None
49
+ when :csv then Serializer::CSV
50
+ when :graph then Serializer::Graph.new
51
+ when :summary then Serializer::Summary
52
+ when :pass_through then Serializer::PassThrough
53
+ when :json then Serializer::Json
123
54
  end
124
55
  end
125
56
  end
@@ -9,9 +9,10 @@ module ChurnVsComplexity
9
9
  end
10
10
 
11
11
  def serialize(result)
12
- data = result[:values_by_file].map do |file, values|
12
+ entries = result[:values_by_file].map do |file, values|
13
13
  "{ file_path: '#{file}', churn: #{values[0]}, complexity: #{values[1]} }"
14
- end.join(",\n") + "\n"
14
+ end.join(",\n")
15
+ data = "#{entries}\n"
15
16
  title = Serializer.title(result)
16
17
  @template.gsub("// INSERT DATA\n", data).gsub('INSERT TITLE', title)
17
18
  end
@@ -4,25 +4,29 @@ module ChurnVsComplexity
4
4
  module Normal
5
5
  module Serializer
6
6
  module Summary
7
+ SECTIONS = [
8
+ ['Churn', :mean_churn, :median_churn],
9
+ ['Complexity', :mean_complexity, :median_complexity],
10
+ ['Gamma score', :mean_gamma_score, :median_gamma_score],
11
+ ].freeze
12
+ private_constant :SECTIONS
13
+
7
14
  def self.serialize(result)
8
- values_by_file = result[:values_by_file]
9
15
  summary = SummaryHash.serialize(result)
16
+ "#{[header(result), *metric_sections(summary)].join("\n\n")}\n"
17
+ end
10
18
 
11
- <<~SUMMARY
12
- #{Serializer.title(result)}
13
-
14
- Number of observations: #{values_by_file.size}
15
-
16
- Churn:
17
- Mean #{summary[:mean_churn]}, Median #{summary[:median_churn]}
18
-
19
- Complexity:
20
- Mean #{summary[:mean_complexity]}, Median #{summary[:median_complexity]}
19
+ def self.header(result)
20
+ "#{Serializer.title(result)}\n\nNumber of observations: #{result[:values_by_file].size}"
21
+ end
21
22
 
22
- Gamma score:
23
- Mean #{summary[:mean_gamma_score]}, Median #{summary[:median_gamma_score]}
24
- SUMMARY
23
+ def self.metric_sections(summary)
24
+ SECTIONS.map do |label, mean_key, median_key|
25
+ "#{label}:\nMean #{summary[mean_key]}, Median #{summary[median_key]}"
26
+ end
25
27
  end
28
+
29
+ private_class_method :header, :metric_sections
26
30
  end
27
31
  end
28
32
  end
@@ -4,6 +4,20 @@ module ChurnVsComplexity
4
4
  module Normal
5
5
  module Serializer
6
6
  module SummaryHash
7
+ EMPTY_METRICS = {
8
+ mean_churn: 0.0,
9
+ median_churn: 0.0,
10
+ max_churn: 0.0,
11
+ min_churn: 0.0,
12
+ mean_complexity: 0.0,
13
+ median_complexity: 0.0,
14
+ max_complexity: 0.0,
15
+ min_complexity: 0.0,
16
+ mean_gamma_score: 0.0,
17
+ median_gamma_score: 0.0,
18
+ }.freeze
19
+ private_constant :EMPTY_METRICS
20
+
7
21
  class << self
8
22
  def serialize(result)
9
23
  values_by_file = result[:values_by_file]
@@ -11,36 +25,37 @@ module ChurnVsComplexity
11
25
 
12
26
  return empty_summary(end_date) if values_by_file.empty?
13
27
 
14
- churn_values = values_by_file.map { |_, values| values[0].to_f }
15
- complexity_values = values_by_file.map { |_, values| values[1].to_f }
16
- gamma_scores = values_by_file.map { |_, values| GammaScore.calculate(values[0], values[1]) }
17
-
18
- stats(churn_values).transform_keys { |k| :"#{k}_churn" }
19
- .merge(stats(complexity_values).transform_keys { |k| :"#{k}_complexity" })
20
- .merge(stats(gamma_scores).slice(:mean, :median).transform_keys { |k| :"#{k}_gamma_score" })
21
- .merge(end_date:)
28
+ summarise(values_by_file).merge(end_date:)
22
29
  end
23
30
 
24
31
  private
25
32
 
33
+ def summarise(values_by_file)
34
+ churn_stats(values_by_file)
35
+ .merge(complexity_stats(values_by_file), gamma_stats(values_by_file))
36
+ end
37
+
38
+ def churn_stats(values_by_file)
39
+ values = values_by_file.map { |_, pair| pair[0].to_f }
40
+ stats(values).transform_keys { |key| :"#{key}_churn" }
41
+ end
42
+
43
+ def complexity_stats(values_by_file)
44
+ values = values_by_file.map { |_, pair| pair[1].to_f }
45
+ stats(values).transform_keys { |key| :"#{key}_complexity" }
46
+ end
47
+
48
+ def gamma_stats(values_by_file)
49
+ values = values_by_file.map { |_, pair| GammaScore.calculate(pair[0], pair[1]) }
50
+ stats(values).slice(:mean, :median).transform_keys { |key| :"#{key}_gamma_score" }
51
+ end
52
+
26
53
  def stats(values)
27
54
  { mean: values.sum / values.size, median: values.sort[values.size / 2], max: values.max, min: values.min }
28
55
  end
29
56
 
30
57
  def empty_summary(end_date)
31
- {
32
- mean_churn: 0.0,
33
- median_churn: 0.0,
34
- max_churn: 0.0,
35
- min_churn: 0.0,
36
- mean_complexity: 0.0,
37
- median_complexity: 0.0,
38
- max_complexity: 0.0,
39
- min_complexity: 0.0,
40
- mean_gamma_score: 0.0,
41
- median_gamma_score: 0.0,
42
- end_date:,
43
- }
58
+ EMPTY_METRICS.merge(end_date:)
44
59
  end
45
60
  end
46
61
  end
@@ -16,7 +16,8 @@ module ChurnVsComplexity
16
16
  if requested_start_date.nil?
17
17
  "Churn until #{end_date.strftime('%Y-%m-%d')} vs complexity"
18
18
  else
19
- "Churn between #{requested_start_date.strftime('%Y-%m-%d')} and #{end_date.strftime('%Y-%m-%d')} vs complexity"
19
+ "Churn between #{requested_start_date.strftime('%Y-%m-%d')} " \
20
+ "and #{end_date.strftime('%Y-%m-%d')} vs complexity"
20
21
  end
21
22
  end
22
23