Skip to content

Commit 5efd555

Browse files
JuanVqzCopilotfbuys
authored
Refactor Skunk Analysis: centralize Skunk attributes (#127)
* Add analysis class * Move from class to module Refactor Skunk analysis integration by removing the Analysis class and adding its methods directly to RubyCritic::AnalysedModulesCollection. Update related files to utilize the new methods for Skunk score calculations and reporting. * Add CHANGELOG entry * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Update CHANGELOG.md Co-authored-by: Francois <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Francois <[email protected]>
1 parent 407f115 commit 5efd555

File tree

14 files changed

+436
-136
lines changed

14 files changed

+436
-136
lines changed

.reek.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ detectors:
2323
- initialize
2424
- Skunk::Cli::Application#execute
2525
- Skunk::Cli::Options::Argv#parse
26+
- Skunk::Analysis#test_module?
27+
- add_mock_methods_to_module
2628
TooManyInstanceVariables:
2729
exclude:
2830
- Skunk::Generator::Html::FileData
@@ -38,10 +40,20 @@ detectors:
3840
- Skunk::Command::StatusSharer#share_url_empty?
3941
- Skunk::Configuration#supported_format?
4042
- Skunk::Configuration#supported_formats
43+
- Skunk::Analysis#test_module?
44+
- Skunk::ConfigTest#setup
4145
ManualDispatch:
4246
exclude:
4347
- Skunk::Config#self.method_missing
4448
- Skunk::Config#self.respond_to_missing?
4549
BooleanParameter:
4650
exclude:
4751
- Skunk::Config#self.respond_to_missing?
52+
DuplicateMethodCall:
53+
exclude:
54+
- Skunk::ConfigTest#test_add_format_ignores_duplicates
55+
FeatureEnvy:
56+
exclude:
57+
- Skunk::Command::StatusReporter#table
58+
- Skunk::Generator::HtmlReport#create_directories_and_files
59+
- add_mock_methods_to_module

.rubocop_todo.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-10-17 11:20:00 UTC using RuboCop version 1.81.1.
3+
# on 2025-10-17 16:20:40 UTC using RuboCop version 1.81.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -36,11 +36,11 @@ Lint/MissingSuper:
3636
Metrics/AbcSize:
3737
Max: 18
3838

39-
# Offense count: 8
39+
# Offense count: 12
4040
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
4141
# AllowedMethods: refine
4242
Metrics/BlockLength:
43-
Max: 82
43+
Max: 233
4444

4545
# Offense count: 2
4646
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## main [(unreleased)](https://github.com/fastruby/skunk/compare/v0.5.4...HEAD)
99

10+
* [REFACTOR: Centralize Skunk analysis into RubyCritic module](https://github.com/fastruby/skunk/pull/127)
1011
* [FEATURE: Add Skunk HTML Report](https://github.com/fastruby/skunk/pull/123)
1112
* [FEATURE: Add Skunk::Config class](https://github.com/fastruby/skunk/pull/123)
1213
* [FEATURE: Add Ruby 3.4 compatibility](https://github.com/fastruby/skunk/pull/124)

lib/skunk/commands/status_reporter.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module Command
1010
class StatusReporter < RubyCritic::Command::StatusReporter
1111
attr_accessor :analysed_modules
1212

13+
def initialize(options = {})
14+
super(options)
15+
end
16+
1317
HEADINGS = %w[file skunk_score churn_times_cost churn cost coverage].freeze
1418
HEADINGS_WITHOUT_FILE = HEADINGS - %w[file]
1519
HEADINGS_WITHOUT_FILE_WIDTH = HEADINGS_WITHOUT_FILE.size * 17 # padding
@@ -38,36 +42,27 @@ def update_status_message
3842
private
3943

4044
def analysed_modules_count
41-
@analysed_modules_count ||= non_test_modules.count
42-
end
43-
44-
def non_test_modules
45-
@non_test_modules ||= analysed_modules.reject do |a_module|
46-
module_path = a_module.pathname.dirname.to_s
47-
module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec")
48-
end
45+
analysed_modules.analysed_modules_count
4946
end
5047

5148
def worst
52-
@worst ||= sorted_modules.first
49+
analysed_modules.worst_module
5350
end
5451

5552
def sorted_modules
56-
@sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse!
53+
analysed_modules.sorted_modules
5754
end
5855

5956
def total_skunk_score
60-
@total_skunk_score ||= non_test_modules.sum(&:skunk_score)
57+
analysed_modules.skunk_score_total
6158
end
6259

6360
def total_churn_times_cost
64-
non_test_modules.sum(&:churn_times_cost)
61+
analysed_modules.total_churn_times_cost
6562
end
6663

6764
def skunk_score_average
68-
return 0 if analysed_modules_count.zero?
69-
70-
(total_skunk_score.to_d / analysed_modules_count).to_f.round(2)
65+
analysed_modules.skunk_score_average
7166
end
7267

7368
def table_options

lib/skunk/generators/html/overview.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
require "rubycritic/generators/html/base"
44

55
require "skunk/generators/html/path_truncator"
6-
require "skunk/generators/html/skunk_data"
6+
require "skunk/generators/html/file_data"
7+
require "skunk/rubycritic/analysed_modules_collection"
78

89
module Skunk
910
module Generator
@@ -19,7 +20,8 @@ def self.erb_template(template_path)
1920

2021
def initialize(analysed_modules)
2122
@analysed_modules = analysed_modules
22-
@data = SkunkData.new(analysed_modules)
23+
@generated_at = Time.now.strftime("%Y-%m-%d %H:%M:%S")
24+
@skunk_version = Skunk::VERSION
2325
end
2426

2527
def file_name
@@ -29,6 +31,24 @@ def file_name
2931
def render
3032
TEMPLATE.result(binding)
3133
end
34+
35+
def analysed_modules_count
36+
@analysed_modules.analysed_modules_count
37+
end
38+
39+
def skunk_score_total
40+
@analysed_modules.skunk_score_total
41+
end
42+
43+
def skunk_score_average
44+
@analysed_modules.skunk_score_average
45+
end
46+
47+
def files
48+
@files ||= @analysed_modules.sorted_modules.map do |module_data|
49+
FileData.new(module_data)
50+
end
51+
end
3252
end
3353
end
3454
end

lib/skunk/generators/html/skunk_data.rb

Lines changed: 0 additions & 59 deletions
This file was deleted.

lib/skunk/generators/html/templates/skunk_overview.html.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@
242242

243243
<section class="cards">
244244
<div class="card">
245-
<div class="value"><%= @data.analysed_modules_count %></div>
245+
<div class="value"><%= analysed_modules_count %></div>
246246
<div class="label">Modules Analysed</div>
247247
</div>
248248
<div class="card">
249-
<div class="value"><%= @data.skunk_score_total %></div>
249+
<div class="value"><%= skunk_score_total %></div>
250250
<div class="label">Total Skunk Score</div>
251251
</div>
252252
<div class="card">
253-
<div class="value"><%= @data.skunk_score_average %></div>
253+
<div class="value"><%= skunk_score_average %></div>
254254
<div class="label">Average Skunk Score</div>
255255
</div>
256256
</section>
@@ -269,7 +269,7 @@
269269
</tr>
270270
</thead>
271271
<tbody>
272-
<% @data.files.each do |item| %>
272+
<% files.each do |item| %>
273273
<tr class="table-row">
274274
<td class="filename">
275275
<span><%= item.file %></span>
@@ -301,7 +301,7 @@
301301
</section>
302302

303303
<footer class="footer">
304-
<p>Generated with Skunk v<%= @data.skunk_version %> on <%= @data.generated_at %></p>
304+
<p>Generated with Skunk v<%= @skunk_version %> on <%= @generated_at %></p>
305305
</footer>
306306
</div>
307307
</body>

lib/skunk/generators/json/simple.rb

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "pathname"
44

55
require "rubycritic/configuration"
6+
require "skunk/rubycritic/analysed_modules_collection"
67

78
module Skunk
89
module Generator
@@ -20,14 +21,7 @@ def render
2021
end
2122

2223
def data
23-
{
24-
analysed_modules_count: analysed_modules_count,
25-
skunk_score_total: skunk_score_total,
26-
skunk_score_average: calculate_average,
27-
worst_pathname: find_worst_module&.pathname,
28-
worst_score: find_worst_module&.skunk_score,
29-
files: build_files
30-
}
24+
@analysed_modules.to_hash
3125
end
3226

3327
def file_directory
@@ -37,41 +31,6 @@ def file_directory
3731
def file_pathname
3832
Pathname.new(file_directory).join(FILE_NAME)
3933
end
40-
41-
private
42-
43-
def analysed_modules_count
44-
@analysed_modules_count ||= non_test_modules.count
45-
end
46-
47-
def calculate_average
48-
return 0 if analysed_modules_count.zero?
49-
50-
(skunk_score_total.to_d / analysed_modules_count).to_f.round(2)
51-
end
52-
53-
def skunk_score_total
54-
@skunk_score_total ||= non_test_modules.sum(&:skunk_score)
55-
end
56-
57-
def non_test_modules
58-
@non_test_modules ||= @analysed_modules.reject do |a_module|
59-
module_path = a_module.pathname.dirname.to_s
60-
module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec")
61-
end
62-
end
63-
64-
def find_worst_module
65-
@find_worst_module ||= sorted_modules.first
66-
end
67-
68-
def sorted_modules
69-
@sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse!
70-
end
71-
72-
def build_files
73-
@build_files ||= sorted_modules.map(&:to_hash)
74-
end
7534
end
7635
end
7736
end

0 commit comments

Comments
 (0)