reek 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/History.txt +44 -4
  2. data/License.txt +20 -0
  3. data/README.rdoc +83 -0
  4. data/Rakefile +0 -1
  5. data/bin/reek +3 -11
  6. data/config/defaults.reek +20 -1
  7. data/features/masking_smells.feature +111 -0
  8. data/features/options.feature +49 -0
  9. data/features/reports.feature +90 -0
  10. data/features/samples.feature +284 -0
  11. data/features/stdin.feature +43 -0
  12. data/features/step_definitions/reek_steps.rb +35 -0
  13. data/features/support/env.rb +38 -0
  14. data/lib/reek.rb +1 -1
  15. data/lib/reek/adapters/application.rb +47 -0
  16. data/lib/reek/adapters/config_file.rb +31 -0
  17. data/lib/reek/adapters/core_extras.rb +72 -0
  18. data/lib/reek/{object_source.rb → adapters/object_source.rb} +15 -19
  19. data/lib/reek/{rake_task.rb → adapters/rake_task.rb} +2 -2
  20. data/lib/reek/adapters/report.rb +91 -0
  21. data/lib/reek/adapters/source.rb +53 -0
  22. data/lib/reek/{spec.rb → adapters/spec.rb} +45 -60
  23. data/lib/reek/block_context.rb +1 -1
  24. data/lib/reek/class_context.rb +26 -6
  25. data/lib/reek/code_context.rb +8 -0
  26. data/lib/reek/code_parser.rb +82 -39
  27. data/lib/reek/command_line.rb +85 -0
  28. data/lib/reek/configuration.rb +51 -0
  29. data/lib/reek/detector_stack.rb +39 -0
  30. data/lib/reek/exceptions.reek +8 -1
  31. data/lib/reek/method_context.rb +53 -11
  32. data/lib/reek/module_context.rb +1 -2
  33. data/lib/reek/name.rb +8 -2
  34. data/lib/reek/sexp_formatter.rb +2 -0
  35. data/lib/reek/smell_warning.rb +26 -8
  36. data/lib/reek/smells/control_couple.rb +8 -4
  37. data/lib/reek/smells/data_clump.rb +88 -0
  38. data/lib/reek/smells/duplication.rb +11 -9
  39. data/lib/reek/smells/feature_envy.rb +3 -4
  40. data/lib/reek/smells/large_class.rb +17 -17
  41. data/lib/reek/smells/long_method.rb +10 -8
  42. data/lib/reek/smells/long_parameter_list.rb +16 -10
  43. data/lib/reek/smells/long_yield_list.rb +1 -1
  44. data/lib/reek/smells/nested_iterators.rb +3 -3
  45. data/lib/reek/smells/simulated_polymorphism.rb +58 -0
  46. data/lib/reek/smells/smell_detector.rb +94 -27
  47. data/lib/reek/smells/uncommunicative_name.rb +23 -23
  48. data/lib/reek/smells/utility_function.rb +27 -11
  49. data/lib/reek/sniffer.rb +183 -0
  50. data/reek.gemspec +5 -5
  51. data/spec/quality/reek_source_spec.rb +15 -0
  52. data/spec/reek/adapters/report_spec.rb +49 -0
  53. data/spec/reek/adapters/should_reek_of_spec.rb +108 -0
  54. data/spec/reek/adapters/should_reek_only_of_spec.rb +87 -0
  55. data/spec/reek/adapters/should_reek_spec.rb +92 -0
  56. data/spec/reek/block_context_spec.rb +7 -1
  57. data/spec/reek/class_context_spec.rb +39 -16
  58. data/spec/reek/code_context_spec.rb +7 -7
  59. data/spec/reek/code_parser_spec.rb +6 -1
  60. data/spec/reek/config_spec.rb +3 -3
  61. data/spec/reek/configuration_spec.rb +12 -0
  62. data/spec/reek/method_context_spec.rb +2 -2
  63. data/spec/reek/name_spec.rb +24 -0
  64. data/spec/reek/object_source_spec.rb +23 -0
  65. data/spec/reek/singleton_method_context_spec.rb +2 -2
  66. data/spec/reek/smell_warning_spec.rb +53 -0
  67. data/spec/reek/smells/data_clump_spec.rb +87 -0
  68. data/spec/reek/smells/duplication_spec.rb +13 -17
  69. data/spec/reek/smells/feature_envy_spec.rb +23 -28
  70. data/spec/reek/smells/large_class_spec.rb +109 -34
  71. data/spec/reek/smells/long_method_spec.rb +140 -3
  72. data/spec/reek/smells/long_parameter_list_spec.rb +1 -2
  73. data/spec/reek/smells/simulated_polymorphism_spec.rb +50 -0
  74. data/spec/reek/smells/smell_detector_spec.rb +53 -0
  75. data/spec/reek/smells/uncommunicative_name_spec.rb +20 -7
  76. data/spec/reek/smells/utility_function_spec.rb +76 -67
  77. data/spec/reek/sniffer_spec.rb +10 -0
  78. data/spec/samples/all_but_one_masked/clean_one.rb +6 -0
  79. data/spec/samples/all_but_one_masked/dirty.rb +7 -0
  80. data/spec/samples/all_but_one_masked/masked.reek +5 -0
  81. data/spec/samples/clean_due_to_masking/clean_one.rb +6 -0
  82. data/spec/samples/clean_due_to_masking/clean_three.rb +6 -0
  83. data/spec/samples/clean_due_to_masking/clean_two.rb +6 -0
  84. data/spec/samples/clean_due_to_masking/dirty_one.rb +7 -0
  85. data/spec/samples/clean_due_to_masking/dirty_two.rb +7 -0
  86. data/spec/samples/clean_due_to_masking/masked.reek +7 -0
  87. data/spec/samples/corrupt_config_file/corrupt.reek +1 -0
  88. data/spec/samples/corrupt_config_file/dirty.rb +7 -0
  89. data/spec/samples/empty_config_file/dirty.rb +7 -0
  90. data/spec/samples/empty_config_file/empty.reek +0 -0
  91. data/spec/samples/exceptions.reek +4 -0
  92. data/spec/{slow/samples → samples}/inline.rb +0 -0
  93. data/spec/samples/masked/dirty.rb +7 -0
  94. data/spec/samples/masked/masked.reek +3 -0
  95. data/spec/samples/mixed_results/clean_one.rb +6 -0
  96. data/spec/samples/mixed_results/clean_three.rb +6 -0
  97. data/spec/samples/mixed_results/clean_two.rb +6 -0
  98. data/spec/samples/mixed_results/dirty_one.rb +7 -0
  99. data/spec/samples/mixed_results/dirty_two.rb +7 -0
  100. data/spec/samples/not_quite_masked/dirty.rb +8 -0
  101. data/spec/samples/not_quite_masked/masked.reek +5 -0
  102. data/spec/{slow/samples → samples}/optparse.rb +0 -0
  103. data/spec/samples/overrides/masked/dirty.rb +7 -0
  104. data/spec/samples/overrides/masked/lower.reek +5 -0
  105. data/spec/samples/overrides/upper.reek +5 -0
  106. data/spec/{slow/samples → samples}/redcloth.rb +0 -0
  107. data/spec/samples/three_clean_files/clean_one.rb +6 -0
  108. data/spec/samples/three_clean_files/clean_three.rb +6 -0
  109. data/spec/samples/three_clean_files/clean_two.rb +6 -0
  110. data/spec/samples/two_smelly_files/dirty_one.rb +7 -0
  111. data/spec/samples/two_smelly_files/dirty_two.rb +7 -0
  112. data/spec/spec.opts +1 -1
  113. data/spec/spec_helper.rb +4 -4
  114. data/tasks/reek.rake +8 -5
  115. data/tasks/test.rake +51 -0
  116. metadata +75 -25
  117. data/README.txt +0 -6
  118. data/lib/reek/options.rb +0 -92
  119. data/lib/reek/report.rb +0 -81
  120. data/lib/reek/smells/smells.rb +0 -81
  121. data/lib/reek/source.rb +0 -127
  122. data/spec/reek/options_spec.rb +0 -13
  123. data/spec/reek/report_spec.rb +0 -48
  124. data/spec/reek/smells/smell_spec.rb +0 -24
  125. data/spec/slow/inline_spec.rb +0 -43
  126. data/spec/slow/optparse_spec.rb +0 -108
  127. data/spec/slow/redcloth_spec.rb +0 -101
  128. data/spec/slow/reek_source_spec.rb +0 -20
  129. data/spec/slow/script_spec.rb +0 -55
  130. data/spec/slow/source_list_spec.rb +0 -40
  131. data/tasks/rspec.rake +0 -21
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ module Reek
4
+ class ConfigFile
5
+
6
+ def initialize(file_path)
7
+ @file_path = file_path
8
+ @hash = YAML.load_file(@file_path) || {}
9
+ problem('not a Hash') unless Hash === @hash
10
+ end
11
+
12
+ #
13
+ # Configure the given sniffer using the contents of the config file.
14
+ #
15
+ def configure(sniffer)
16
+ @hash.each { |klass_name, config|
17
+ sniffer.configure(find_class(klass_name), config)
18
+ }
19
+ end
20
+
21
+ def find_class(name)
22
+ klass = Reek::Smells.const_get(name)
23
+ problem("#{name} is not a code smell") unless klass
24
+ klass
25
+ end
26
+
27
+ def problem(reason)
28
+ raise "Invalid configuration file \"#{File.basename(@file_path)}\" -- #{reason}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,72 @@
1
+ require 'reek/adapters/source'
2
+ require 'reek/sniffer'
3
+
4
+ class Object
5
+ #
6
+ # Creates a new +Sniffer+ that assumes this object contains Ruby source
7
+ # code and examines that code for smells. Calls +to_reek_source+ on self
8
+ # to obtain the +Source+ object wrapper.
9
+ #
10
+ def sniff
11
+ Reek::Sniffer.new(self.to_reek_source)
12
+ end
13
+
14
+ #
15
+ # Constructs a Sniffer which examines this object for code smells.
16
+ # (This feature is only enabled if you have the ParseTree gem installed.)
17
+ #
18
+ def to_reek_source
19
+ ObjectSource.new(self, self.to_s)
20
+ end
21
+ end
22
+
23
+ class File
24
+ #
25
+ # Creates a new +Source+ that assumes this File contains Ruby source
26
+ # code and prepares it to be examined for code smells.
27
+ #
28
+ def to_reek_source
29
+ Reek::SourceFile.new(self)
30
+ end
31
+ end
32
+
33
+ class IO
34
+ #
35
+ # Creates a new +Source+ that assumes this IO stream contains Ruby source
36
+ # code and prepares it to be examined for code smells.
37
+ #
38
+ def to_reek_source(description = 'io')
39
+ Reek::Source.new(self.readlines.join, description)
40
+ end
41
+ end
42
+
43
+ class String
44
+ #
45
+ # Creates a new +Source+ that assumes this string contains Ruby source
46
+ # code and prepares it to be examined for code smells.
47
+ #
48
+ def to_reek_source
49
+ Reek::Source.new(self, 'string')
50
+ end
51
+ end
52
+
53
+ class Array
54
+ def paths
55
+ self.map do |path|
56
+ if test 'd', path
57
+ Dir["#{path}/**/*.rb"].paths
58
+ else
59
+ path
60
+ end
61
+ end.flatten.sort
62
+ end
63
+
64
+ #
65
+ # Creates a new +Sniffer+ that assumes this Array contains the names
66
+ # of Ruby source files and examines those files for smells.
67
+ #
68
+ def sniff
69
+ sniffers = paths.map {|path| File.new(path).sniff}
70
+ Reek::SnifferSet.new(sniffers, 'dir')
71
+ end
72
+ end
@@ -1,15 +1,7 @@
1
- module Reek
2
- class Source
3
- #
4
- # Factory method: creates a +Source+ from obj.
5
- # The code is not parsed until +report+ is called.
6
- # (This feature is only enabled if you have the ParseTree gem installed.)
7
- #
8
- def self.from_object(obj)
9
- return ObjectSource.new(obj, obj.to_s)
10
- end
11
- end
1
+ require 'reek/adapters/source'
2
+ require 'reek/configuration'
12
3
 
4
+ module Reek
13
5
  class ObjectSource < Source # :nodoc:
14
6
 
15
7
  def self.unify(sexp) # :nodoc:
@@ -20,7 +12,13 @@ module Reek
20
12
  return unifier.process(sexp[0])
21
13
  end
22
14
 
23
- def can_parse_objects?
15
+ def configure(sniffer)
16
+ super
17
+ disabled_config = {Reek::SmellConfiguration::ENABLED_KEY => false}
18
+ sniffer.configure(LargeClass, disabled_config)
19
+ end
20
+
21
+ def self.can_parse_objects?
24
22
  return true if Object.const_defined?(:ParseTree)
25
23
  begin
26
24
  require 'parse_tree'
@@ -30,8 +28,8 @@ module Reek
30
28
  end
31
29
  end
32
30
 
33
- def generate_syntax_tree
34
- if can_parse_objects?
31
+ def syntax_tree
32
+ if ObjectSource.can_parse_objects?
35
33
  ObjectSource.unify(ParseTree.new.parse_tree(@source))
36
34
  else
37
35
  throw ArgumentError.new('You must install the ParseTree gem to use this feature')
@@ -42,12 +40,10 @@ end
42
40
 
43
41
  class Object
44
42
  #
45
- # Constructs a Source representing this object; the source can then be used
46
- # to generate an abstract syntax tree for the object, which can in turn then
47
- # be examined for code smells.
43
+ # Constructs a Sniffer which examines this object for code smells.
48
44
  # (This feature is only enabled if you have the ParseTree gem installed.)
49
45
  #
50
- def to_source
51
- Reek::Source.from_object(self)
46
+ def to_reek_source
47
+ ObjectSource.new(self, self.to_s)
52
48
  end
53
49
  end
@@ -59,7 +59,7 @@ module Reek
59
59
  # Defines a new task, using the name +name+.
60
60
  def initialize(name = :reek)
61
61
  @name = name
62
- @libs = [File.expand_path(File.dirname(__FILE__) + '/../../lib')]
62
+ @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
63
63
  @source_files = nil
64
64
  @ruby_opts = []
65
65
  @reek_opts = ''
@@ -87,7 +87,7 @@ private
87
87
  end
88
88
 
89
89
  def self.reek_script
90
- File.expand_path(File.dirname(__FILE__) + '/../../bin/reek')
90
+ File.expand_path(File.dirname(__FILE__) + '/../../../bin/reek')
91
91
  end
92
92
 
93
93
  def self.ruby_exe
@@ -0,0 +1,91 @@
1
+ require 'set'
2
+ require 'reek/command_line' # SMELL: Global Variable
3
+
4
+ module Reek
5
+ class ReportSection
6
+
7
+ def initialize(sniffer) # :nodoc:
8
+ @masked_warnings = SortedSet.new
9
+ @warnings = SortedSet.new
10
+ @desc = sniffer.desc
11
+ sniffer.report_on(self)
12
+ end
13
+
14
+ def <<(smell) # :nodoc:
15
+ @warnings << smell
16
+ true
17
+ end
18
+
19
+ def record_masked_smell(smell)
20
+ @masked_warnings << smell
21
+ end
22
+
23
+ def num_masked_smells # SMELL: getter
24
+ @masked_warnings.length
25
+ end
26
+
27
+ # Creates a formatted report of all the +Smells::SmellWarning+ objects recorded in
28
+ # this report, with a heading.
29
+ def full_report
30
+ return quiet_report if Options[:quiet]
31
+ result = header
32
+ result += ":\n#{smell_list}" if should_report
33
+ result += "\n"
34
+ result
35
+ end
36
+
37
+ def quiet_report
38
+ return '' unless should_report
39
+ "#{header}:\n#{smell_list}\n"
40
+ end
41
+
42
+ def header
43
+ @all_warnings = SortedSet.new(@warnings) # SMELL: Temporary Field
44
+ @all_warnings.merge(@masked_warnings)
45
+ "#{@desc} -- #{visible_header}#{masked_header}"
46
+ end
47
+
48
+ # Creates a formatted report of all the +Smells::SmellWarning+ objects recorded in
49
+ # this report.
50
+ def smell_list
51
+ smells = Options[:show_all] ? @all_warnings : @warnings
52
+ smells.map {|smell| " #{smell.report}"}.join("\n")
53
+ end
54
+
55
+ private
56
+
57
+ def should_report
58
+ @warnings.length > 0 or (Options[:show_all] and @masked_warnings.length > 0)
59
+ end
60
+
61
+ def visible_header
62
+ num_smells = @warnings.length
63
+ result = "#{num_smells} warning"
64
+ result += 's' unless num_smells == 1
65
+ result
66
+ end
67
+
68
+ def masked_header
69
+ num_masked_warnings = @all_warnings.length - @warnings.length
70
+ num_masked_warnings == 0 ? '' : " (+#{num_masked_warnings} masked)"
71
+ end
72
+ end
73
+
74
+ class Report
75
+
76
+ def initialize(sniffers)
77
+ @partials = Array(sniffers).map {|sn| ReportSection.new(sn)}
78
+ end
79
+
80
+ # SMELL: Shotgun Surgery
81
+ # This method and the next will have to be repeated for every new
82
+ # kind of report.
83
+ def full_report
84
+ @partials.map { |rpt| rpt.full_report }.join
85
+ end
86
+
87
+ def quiet_report
88
+ @partials.map { |rpt| rpt.quiet_report }.join
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,53 @@
1
+ require 'reek/adapters/config_file'
2
+
3
+ module Reek
4
+
5
+ #
6
+ # A +Source+ object represents a chunk of Ruby source code.
7
+ #
8
+ class Source
9
+
10
+ attr_reader :desc
11
+
12
+ def initialize(code, desc)
13
+ @source = code
14
+ @desc = desc
15
+ end
16
+
17
+ def configure(sniffer) end
18
+
19
+ def syntax_tree
20
+ RubyParser.new.parse(@source, @desc) || s()
21
+ end
22
+ end
23
+
24
+ #
25
+ # Represents a file of Ruby source, whose contents will be examined
26
+ # for code smells.
27
+ #
28
+ class SourceFile < Source
29
+
30
+ def self.lines(file)
31
+ IO.readlines(file.path)
32
+ end
33
+
34
+ def initialize(file)
35
+ @file = file
36
+ super(SourceFile.lines(@file).join, @file.path)
37
+ end
38
+
39
+ def configure(sniffer)
40
+ path = File.expand_path(File.dirname(@file.path))
41
+ all_config_files(path).each { |cf| ConfigFile.new(cf).configure(sniffer) }
42
+ end
43
+
44
+ private
45
+
46
+ def all_config_files(path)
47
+ return [] unless File.exist?(path)
48
+ parent = File.dirname(path)
49
+ return [] if path == parent
50
+ all_config_files(parent) + Dir["#{path}/*.reek"]
51
+ end
52
+ end
53
+ end
@@ -1,4 +1,7 @@
1
- require 'reek/source'
1
+ require 'reek/sniffer'
2
+ require 'reek/adapters/core_extras'
3
+ require 'reek/adapters/object_source'
4
+ require 'reek/adapters/report'
2
5
 
3
6
  module Reek
4
7
 
@@ -34,75 +37,83 @@ module Reek
34
37
  # And a more complex example, making use of one of the factory methods for
35
38
  # +Source+ so that the code is parsed and analysed only once:
36
39
  #
37
- # ruby = 'def double_thing() @other.thing.foo + @other.thing.foo end'.to_source
40
+ # ruby = 'def double_thing() @other.thing.foo + @other.thing.foo end'.sniff
38
41
  # ruby.should reek_of(:Duplication, /@other.thing[^\.]/)
39
42
  # ruby.should reek_of(:Duplication, /@other.thing.foo/)
40
43
  # ruby.should_not reek_of(:FeatureEnvy)
41
44
  #
42
45
  module Spec
46
+ module ReekMatcher
47
+ def report
48
+ Report.new(@sniffer.sniffers).quiet_report
49
+ end
50
+ end
51
+
43
52
  class ShouldReek # :nodoc:
53
+ include ReekMatcher
54
+
44
55
  def matches?(actual)
45
- @source = actual.to_source
46
- @source.smelly?
56
+ @sniffer = actual.sniff
57
+ @sniffer.smelly?
47
58
  end
48
59
  def failure_message_for_should
49
- "Expected source to reek, but it didn't"
60
+ "Expected #{@sniffer.desc} to reek, but it didn't"
50
61
  end
51
62
  def failure_message_for_should_not
52
- "Expected no smells, but got:\n#{@source.report}"
63
+ "Expected no smells, but got:\n#{report}"
53
64
  end
54
65
  end
55
66
 
56
- #
57
- # Returns +true+ if and only if the target source code contains smells.
58
- #
59
- def reek
60
- ShouldReek.new
61
- end
62
-
63
67
  class ShouldReekOf # :nodoc:
68
+ include ReekMatcher
69
+
64
70
  def initialize(klass, patterns)
65
71
  @klass = klass
66
72
  @patterns = patterns
67
73
  end
68
74
  def matches?(actual)
69
- @source = actual.to_source
70
- @source.has_smell?(@klass, @patterns)
75
+ @sniffer = actual.sniff
76
+ @sniffer.has_smell?(@klass, @patterns)
71
77
  end
72
78
  def failure_message_for_should
73
- "Expected #{@source} to reek of #{@klass}, but it didn't"
79
+ "Expected #{@sniffer.desc} to reek of #{@klass}, but it didn't"
74
80
  end
75
81
  def failure_message_for_should_not
76
- "Expected #{@source} not to reek of #{@klass}, but got:\n#{@source.report}"
82
+ "Expected #{@sniffer.desc} not to reek of #{@klass}, but got:\n#{report}"
77
83
  end
78
84
  end
79
85
 
80
- #
81
- # Checks the target source code for instances of +smell_class+,
82
- # and returns +true+ only if one of them has a report string matching
83
- # all of the +patterns+.
84
- #
85
- def reek_of(smell_class, *patterns)
86
- ShouldReekOf.new(smell_class, patterns)
87
- end
86
+ class ShouldReekOnlyOf < ShouldReekOf # :nodoc:
87
+ include ReekMatcher
88
88
 
89
- class ShouldReekOnlyOf # :nodoc:
90
- def initialize(klass, patterns)
91
- @klass = klass
92
- @patterns = patterns
93
- end
94
89
  def matches?(actual)
95
- @source = actual.to_source
96
- @source.report.length == 1 and @source.has_smell?(@klass, @patterns)
90
+ @sniffer = actual.sniff
91
+ @sniffer.smells_only_of?(@klass, @patterns)
97
92
  end
98
93
  def failure_message_for_should
99
- "Expected source to reek only of #{@klass}, but got:\n#{@source.report}"
94
+ "Expected #{@sniffer.desc} to reek only of #{@klass}, but got:\n#{report}"
100
95
  end
101
96
  def failure_message_for_should_not
102
- "Expected source not to reek only of #{@klass}, but it did"
97
+ "Expected #{@sniffer.desc} not to reek only of #{@klass}, but it did"
103
98
  end
104
99
  end
105
100
 
101
+ #
102
+ # Returns +true+ if and only if the target source code contains smells.
103
+ #
104
+ def reek
105
+ ShouldReek.new
106
+ end
107
+
108
+ #
109
+ # Checks the target source code for instances of +smell_class+,
110
+ # and returns +true+ only if one of them has a report string matching
111
+ # all of the +patterns+.
112
+ #
113
+ def reek_of(smell_class, *patterns)
114
+ ShouldReekOf.new(smell_class, patterns)
115
+ end
116
+
106
117
  #
107
118
  # As for reek_of, but the matched smell warning must be the only warning of
108
119
  # any kind in the target source code's Reek report.
@@ -113,32 +124,6 @@ module Reek
113
124
  end
114
125
  end
115
126
 
116
- class File
117
- def to_source
118
- Reek::Source.from_f(self)
119
- end
120
- end
121
-
122
- class String
123
- def to_source
124
- Reek::Source.from_s(self)
125
- end
126
- end
127
-
128
- class Array
129
- def to_source
130
- Reek::Source.from_pathlist(self)
131
- end
132
- end
133
-
134
- module Reek
135
- class Source
136
- def to_source
137
- self
138
- end
139
- end
140
- end
141
-
142
127
  if Object.const_defined?(:Spec)
143
128
  Spec::Runner.configure do |config|
144
129
  config.include(Reek::Spec)