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