|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Skunk |
| 4 | + # Centralized service for analyzing Skunk metrics from analysed modules. |
| 5 | + # This class encapsulates all the core business logic for calculating |
| 6 | + # Skunk scores, filtering test modules, and providing aggregated statistics. |
| 7 | + # |
| 8 | + # @example |
| 9 | + # analysis = Skunk::Analysis.new(analysed_modules) |
| 10 | + # puts "Total Skunk Score: #{analysis.skunk_score_total}" |
| 11 | + # puts "Average: #{analysis.skunk_score_average}" |
| 12 | + # puts "Worst module: #{analysis.worst_module.pathname}" |
| 13 | + class Analysis |
| 14 | + attr_reader :analysed_modules |
| 15 | + |
| 16 | + # @param analysed_modules [RubyCritic::AnalysedModulesCollection] Collection of analysed modules |
| 17 | + def initialize(analysed_modules) |
| 18 | + @analysed_modules = analysed_modules |
| 19 | + end |
| 20 | + |
| 21 | + # Returns the count of non-test modules |
| 22 | + # @return [Integer] |
| 23 | + def analysed_modules_count |
| 24 | + @analysed_modules_count ||= non_test_modules.count |
| 25 | + end |
| 26 | + |
| 27 | + # Returns the total Skunk score across all non-test modules |
| 28 | + # @return [Float] |
| 29 | + def skunk_score_total |
| 30 | + @skunk_score_total ||= non_test_modules.sum(&:skunk_score) |
| 31 | + end |
| 32 | + |
| 33 | + # Returns the average Skunk score across all non-test modules |
| 34 | + # @return [Float] |
| 35 | + def skunk_score_average |
| 36 | + return 0.0 if analysed_modules_count.zero? |
| 37 | + |
| 38 | + (skunk_score_total.to_d / analysed_modules_count).to_f.round(2) |
| 39 | + end |
| 40 | + |
| 41 | + # Returns the total churn times cost across all non-test modules |
| 42 | + # @return [Float] |
| 43 | + def total_churn_times_cost |
| 44 | + @total_churn_times_cost ||= non_test_modules.sum(&:churn_times_cost) |
| 45 | + end |
| 46 | + |
| 47 | + # Returns the module with the highest Skunk score (worst performing) |
| 48 | + # @return [RubyCritic::AnalysedModule, nil] |
| 49 | + def worst_module |
| 50 | + @worst_module ||= sorted_modules.first |
| 51 | + end |
| 52 | + |
| 53 | + # Returns modules sorted by Skunk score in descending order (worst first) |
| 54 | + # @return [Array<RubyCritic::AnalysedModule>] |
| 55 | + def sorted_modules |
| 56 | + @sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse! |
| 57 | + end |
| 58 | + |
| 59 | + # Returns only non-test modules (excludes test and spec directories) |
| 60 | + # @return [Array<RubyCritic::AnalysedModule>] |
| 61 | + def non_test_modules |
| 62 | + @non_test_modules ||= analysed_modules.reject do |a_module| |
| 63 | + test_module?(a_module) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + # Returns a hash representation of the analysis results |
| 68 | + # @return [Hash] |
| 69 | + def to_hash |
| 70 | + { |
| 71 | + analysed_modules_count: analysed_modules_count, |
| 72 | + skunk_score_total: skunk_score_total, |
| 73 | + skunk_score_average: skunk_score_average, |
| 74 | + total_churn_times_cost: total_churn_times_cost, |
| 75 | + worst_pathname: worst_module&.pathname, |
| 76 | + worst_score: worst_module&.skunk_score, |
| 77 | + files: files_as_hash |
| 78 | + } |
| 79 | + end |
| 80 | + |
| 81 | + private |
| 82 | + |
| 83 | + # Returns files as an array of hashes (for JSON serialization) |
| 84 | + # @return [Array<Hash>] |
| 85 | + def files_as_hash |
| 86 | + @files_as_hash ||= sorted_modules.map(&:to_hash) |
| 87 | + end |
| 88 | + |
| 89 | + # Determines if a module is a test module based on its path |
| 90 | + # @param a_module [RubyCritic::AnalysedModule] The module to check |
| 91 | + # @return [Boolean] |
| 92 | + def test_module?(a_module) |
| 93 | + pathname = a_module.pathname |
| 94 | + module_path = pathname.dirname.to_s |
| 95 | + filename = pathname.basename.to_s |
| 96 | + |
| 97 | + # Check if directory starts or ends with test/spec |
| 98 | + directory_is_test = module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec") |
| 99 | + |
| 100 | + # Check if filename ends with _test.rb or _spec.rb |
| 101 | + filename_is_test = filename.end_with?("_test.rb", "_spec.rb") |
| 102 | + |
| 103 | + directory_is_test || filename_is_test |
| 104 | + end |
| 105 | + end |
| 106 | +end |
0 commit comments