reek 1.2.6 → 1.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/.yardopts +10 -0
  2. data/History.txt +20 -0
  3. data/README.md +90 -0
  4. data/bin/reek +2 -2
  5. data/config/defaults.reek +34 -4
  6. data/features/masking_smells.feature +35 -15
  7. data/features/options.feature +2 -0
  8. data/features/rake_task.feature +11 -18
  9. data/features/reports.feature +13 -15
  10. data/features/samples.feature +90 -105
  11. data/features/stdin.feature +3 -6
  12. data/features/step_definitions/reek_steps.rb +8 -4
  13. data/features/support/env.rb +2 -3
  14. data/features/yaml.feature +124 -0
  15. data/lib/reek.rb +8 -4
  16. data/lib/reek/cli/application.rb +46 -0
  17. data/lib/reek/cli/command_line.rb +106 -0
  18. data/lib/reek/cli/help_command.rb +18 -0
  19. data/lib/reek/cli/reek_command.rb +37 -0
  20. data/lib/reek/cli/report.rb +91 -0
  21. data/lib/reek/cli/version_command.rb +19 -0
  22. data/lib/reek/cli/yaml_command.rb +32 -0
  23. data/lib/reek/core/block_context.rb +18 -0
  24. data/lib/reek/core/class_context.rb +23 -0
  25. data/lib/reek/core/code_context.rb +72 -0
  26. data/lib/reek/core/code_parser.rb +192 -0
  27. data/lib/reek/core/detector_stack.rb +29 -0
  28. data/lib/reek/core/masking_collection.rb +46 -0
  29. data/lib/reek/core/method_context.rb +132 -0
  30. data/lib/reek/core/module_context.rb +64 -0
  31. data/lib/reek/{object_refs.rb → core/object_refs.rb} +8 -6
  32. data/lib/reek/{singleton_method_context.rb → core/singleton_method_context.rb} +10 -5
  33. data/lib/reek/core/smell_configuration.rb +66 -0
  34. data/lib/reek/core/sniffer.rb +110 -0
  35. data/lib/reek/core/stop_context.rb +26 -0
  36. data/lib/reek/examiner.rb +88 -0
  37. data/lib/reek/rake/task.rb +124 -0
  38. data/lib/reek/smell_warning.rb +69 -13
  39. data/lib/reek/smells.rb +29 -0
  40. data/lib/reek/smells/attribute.rb +13 -14
  41. data/lib/reek/smells/boolean_parameter.rb +33 -0
  42. data/lib/reek/smells/class_variable.rb +8 -6
  43. data/lib/reek/smells/control_couple.rb +33 -17
  44. data/lib/reek/smells/data_clump.rb +10 -6
  45. data/lib/reek/smells/duplication.rb +24 -14
  46. data/lib/reek/smells/feature_envy.rb +11 -6
  47. data/lib/reek/smells/irresponsible_module.rb +28 -0
  48. data/lib/reek/smells/large_class.rb +9 -7
  49. data/lib/reek/smells/long_method.rb +6 -5
  50. data/lib/reek/smells/long_parameter_list.rb +11 -9
  51. data/lib/reek/smells/long_yield_list.rb +37 -7
  52. data/lib/reek/smells/nested_iterators.rb +34 -9
  53. data/lib/reek/smells/simulated_polymorphism.rb +15 -11
  54. data/lib/reek/smells/smell_detector.rb +24 -12
  55. data/lib/reek/smells/uncommunicative_method_name.rb +76 -0
  56. data/lib/reek/smells/uncommunicative_module_name.rb +76 -0
  57. data/lib/reek/smells/{uncommunicative_name.rb → uncommunicative_parameter_name.rb} +14 -26
  58. data/lib/reek/smells/uncommunicative_variable_name.rb +90 -0
  59. data/lib/reek/smells/utility_function.rb +33 -9
  60. data/lib/reek/source.rb +18 -0
  61. data/lib/reek/source/code_comment.rb +19 -0
  62. data/lib/reek/source/config_file.rb +72 -0
  63. data/lib/reek/source/core_extras.rb +46 -0
  64. data/lib/reek/source/sexp_formatter.rb +16 -0
  65. data/lib/reek/source/source_code.rb +44 -0
  66. data/lib/reek/source/source_file.rb +32 -0
  67. data/lib/reek/source/source_locator.rb +36 -0
  68. data/lib/reek/source/tree_dresser.rb +128 -0
  69. data/lib/reek/spec.rb +51 -0
  70. data/lib/reek/spec/should_reek.rb +34 -0
  71. data/lib/reek/spec/should_reek_of.rb +37 -0
  72. data/lib/reek/spec/should_reek_only_of.rb +36 -0
  73. data/reek.gemspec +5 -5
  74. data/spec/reek/{help_command_spec.rb → cli/help_command_spec.rb} +3 -4
  75. data/spec/reek/{reek_command_spec.rb → cli/reek_command_spec.rb} +8 -7
  76. data/spec/reek/cli/report_spec.rb +26 -0
  77. data/spec/reek/{version_command_spec.rb → cli/version_command_spec.rb} +3 -3
  78. data/spec/reek/cli/yaml_command_spec.rb +47 -0
  79. data/spec/reek/core/block_context_spec.rb +26 -0
  80. data/spec/reek/core/class_context_spec.rb +53 -0
  81. data/spec/reek/{code_context_spec.rb → core/code_context_spec.rb} +15 -37
  82. data/spec/reek/{code_parser_spec.rb → core/code_parser_spec.rb} +5 -5
  83. data/spec/reek/{config_spec.rb → core/config_spec.rb} +2 -6
  84. data/spec/reek/{masking_collection_spec.rb → core/masking_collection_spec.rb} +3 -4
  85. data/spec/reek/{method_context_spec.rb → core/method_context_spec.rb} +6 -7
  86. data/spec/reek/core/module_context_spec.rb +42 -0
  87. data/spec/reek/{object_refs_spec.rb → core/object_refs_spec.rb} +5 -6
  88. data/spec/reek/core/singleton_method_context_spec.rb +15 -0
  89. data/spec/reek/core/smell_configuration_spec.rb +11 -0
  90. data/spec/reek/core/stop_context_spec.rb +17 -0
  91. data/spec/reek/examiner_spec.rb +42 -0
  92. data/spec/reek/smell_warning_spec.rb +82 -33
  93. data/spec/reek/smells/attribute_spec.rb +33 -7
  94. data/spec/reek/smells/boolean_parameter_spec.rb +76 -0
  95. data/spec/reek/smells/class_variable_spec.rb +15 -6
  96. data/spec/reek/smells/control_couple_spec.rb +40 -29
  97. data/spec/reek/smells/data_clump_spec.rb +28 -7
  98. data/spec/reek/smells/duplication_spec.rb +47 -41
  99. data/spec/reek/smells/feature_envy_spec.rb +76 -18
  100. data/spec/reek/smells/irresponsible_module_spec.rb +37 -0
  101. data/spec/reek/smells/large_class_spec.rb +91 -56
  102. data/spec/reek/smells/long_method_spec.rb +32 -7
  103. data/spec/reek/smells/long_parameter_list_spec.rb +42 -13
  104. data/spec/reek/smells/long_yield_list_spec.rb +65 -0
  105. data/spec/reek/smells/nested_iterators_spec.rb +94 -3
  106. data/spec/reek/smells/simulated_polymorphism_spec.rb +48 -20
  107. data/spec/reek/smells/smell_detector_shared.rb +28 -0
  108. data/spec/reek/smells/uncommunicative_method_name_spec.rb +57 -0
  109. data/spec/reek/smells/uncommunicative_module_name_spec.rb +67 -0
  110. data/spec/reek/smells/uncommunicative_parameter_name_spec.rb +61 -0
  111. data/spec/reek/smells/uncommunicative_variable_name_spec.rb +124 -0
  112. data/spec/reek/smells/utility_function_spec.rb +45 -3
  113. data/spec/reek/source/code_comment_spec.rb +24 -0
  114. data/spec/reek/source/object_source_spec.rb +20 -0
  115. data/spec/reek/{adapters/source_spec.rb → source/source_code_spec.rb} +7 -8
  116. data/spec/reek/source/tree_dresser_spec.rb +165 -0
  117. data/spec/reek/spec/should_reek_of_spec.rb +76 -0
  118. data/spec/reek/spec/should_reek_only_of_spec.rb +89 -0
  119. data/spec/reek/{adapters → spec}/should_reek_spec.rb +8 -32
  120. data/spec/samples/all_but_one_masked/clean_one.rb +1 -0
  121. data/spec/samples/all_but_one_masked/dirty.rb +1 -0
  122. data/spec/samples/all_but_one_masked/masked.reek +5 -1
  123. data/spec/samples/clean_due_to_masking/clean_one.rb +1 -0
  124. data/spec/samples/clean_due_to_masking/clean_three.rb +1 -0
  125. data/spec/samples/clean_due_to_masking/clean_two.rb +1 -0
  126. data/spec/samples/clean_due_to_masking/dirty_one.rb +1 -1
  127. data/spec/samples/clean_due_to_masking/dirty_two.rb +1 -1
  128. data/spec/samples/clean_due_to_masking/masked.reek +5 -1
  129. data/spec/samples/corrupt_config_file/dirty.rb +1 -1
  130. data/spec/samples/empty_config_file/dirty.rb +2 -1
  131. data/spec/samples/exceptions.reek +1 -1
  132. data/spec/samples/masked/dirty.rb +2 -1
  133. data/spec/samples/masked/masked.reek +3 -1
  134. data/spec/samples/mixed_results/clean_one.rb +1 -0
  135. data/spec/samples/mixed_results/clean_three.rb +1 -0
  136. data/spec/samples/mixed_results/clean_two.rb +1 -0
  137. data/spec/samples/mixed_results/dirty_one.rb +1 -0
  138. data/spec/samples/mixed_results/dirty_two.rb +1 -0
  139. data/spec/samples/not_quite_masked/dirty.rb +2 -1
  140. data/spec/samples/not_quite_masked/masked.reek +1 -1
  141. data/spec/samples/overrides/masked/dirty.rb +2 -1
  142. data/spec/samples/overrides/masked/lower.reek +3 -1
  143. data/spec/samples/three_clean_files/clean_one.rb +1 -0
  144. data/spec/samples/three_clean_files/clean_three.rb +1 -0
  145. data/spec/samples/three_clean_files/clean_two.rb +1 -0
  146. data/spec/samples/two_smelly_files/dirty_one.rb +2 -1
  147. data/spec/samples/two_smelly_files/dirty_two.rb +2 -1
  148. data/spec/spec_helper.rb +1 -2
  149. data/tasks/reek.rake +2 -2
  150. data/tasks/test.rake +12 -3
  151. metadata +81 -62
  152. data/README.rdoc +0 -84
  153. data/lib/reek/adapters/application.rb +0 -46
  154. data/lib/reek/adapters/command_line.rb +0 -77
  155. data/lib/reek/adapters/config_file.rb +0 -31
  156. data/lib/reek/adapters/core_extras.rb +0 -64
  157. data/lib/reek/adapters/rake_task.rb +0 -121
  158. data/lib/reek/adapters/report.rb +0 -86
  159. data/lib/reek/adapters/source.rb +0 -72
  160. data/lib/reek/adapters/spec.rb +0 -133
  161. data/lib/reek/block_context.rb +0 -62
  162. data/lib/reek/class_context.rb +0 -41
  163. data/lib/reek/code_context.rb +0 -68
  164. data/lib/reek/code_parser.rb +0 -203
  165. data/lib/reek/configuration.rb +0 -57
  166. data/lib/reek/detector_stack.rb +0 -37
  167. data/lib/reek/help_command.rb +0 -14
  168. data/lib/reek/if_context.rb +0 -18
  169. data/lib/reek/masking_collection.rb +0 -33
  170. data/lib/reek/method_context.rb +0 -138
  171. data/lib/reek/module_context.rb +0 -49
  172. data/lib/reek/name.rb +0 -57
  173. data/lib/reek/reek_command.rb +0 -28
  174. data/lib/reek/sexp_formatter.rb +0 -10
  175. data/lib/reek/sniffer.rb +0 -177
  176. data/lib/reek/stop_context.rb +0 -35
  177. data/lib/reek/tree_dresser.rb +0 -82
  178. data/lib/reek/version_command.rb +0 -14
  179. data/lib/reek/yield_call_context.rb +0 -12
  180. data/spec/reek/adapters/report_spec.rb +0 -31
  181. data/spec/reek/adapters/should_reek_of_spec.rb +0 -138
  182. data/spec/reek/adapters/should_reek_only_of_spec.rb +0 -87
  183. data/spec/reek/block_context_spec.rb +0 -65
  184. data/spec/reek/class_context_spec.rb +0 -161
  185. data/spec/reek/configuration_spec.rb +0 -12
  186. data/spec/reek/if_context_spec.rb +0 -17
  187. data/spec/reek/module_context_spec.rb +0 -46
  188. data/spec/reek/name_spec.rb +0 -37
  189. data/spec/reek/object_source_spec.rb +0 -23
  190. data/spec/reek/singleton_method_context_spec.rb +0 -16
  191. data/spec/reek/smells/smell_detector_spec.rb +0 -36
  192. data/spec/reek/smells/uncommunicative_name_spec.rb +0 -146
  193. data/spec/reek/sniffer_spec.rb +0 -11
  194. data/spec/reek/stop_context_spec.rb +0 -33
  195. data/spec/reek/tree_dresser_spec.rb +0 -20
@@ -1,19 +1,66 @@
1
+
1
2
  module Reek
2
3
 
3
4
  #
4
5
  # Reports a warning that a smell has been found.
5
6
  #
6
7
  class SmellWarning
8
+
7
9
  include Comparable
8
10
 
9
- def initialize(detector_class, context, line, message, masked)
10
- @smell = detector_class.class.name.split(/::/)[-1]
11
- @context = context
12
- @line = line
13
- @message = message
14
- @is_masked = masked
11
+ MESSAGE_KEY = 'message'
12
+ SUBCLASS_KEY = 'subclass'
13
+ CLASS_KEY = 'class'
14
+
15
+ CONTEXT_KEY = 'context'
16
+ LINES_KEY = 'lines'
17
+ SOURCE_KEY = 'source'
18
+
19
+ ACTIVE_KEY = 'is_active'
20
+
21
+ def initialize(class_name, context, lines, message, masked,
22
+ source = '', subclass_name = '', parameters = {})
23
+ @smell = {
24
+ CLASS_KEY => class_name,
25
+ SUBCLASS_KEY => subclass_name,
26
+ MESSAGE_KEY => message,
27
+ }
28
+ @smell.merge!(parameters)
29
+ @status = {
30
+ ACTIVE_KEY => !masked
31
+ }
32
+ @location = {
33
+ CONTEXT_KEY => context,
34
+ LINES_KEY => lines,
35
+ SOURCE_KEY => source
36
+ }
15
37
  end
16
38
 
39
+ #
40
+ # Details of the smell found, including its class ({CLASS_KEY}),
41
+ # subclass ({SUBCLASS_KEY}) and summary message ({MESSAGE_KEY})
42
+ #
43
+ # @return [Hash{String => String}]
44
+ #
45
+ attr_reader :smell
46
+
47
+ #
48
+ # Details of the smell's location, including its context ({CONTEXT_KEY}),
49
+ # the line numbers on which it occurs ({LINES_KEY}) and the source
50
+ # file ({SOURCE_KEY})
51
+ #
52
+ # @return [Hash{String => String, Array<Number>}]
53
+ #
54
+ attr_reader :location
55
+
56
+ #
57
+ # Details of the smell's status, including whether it is active ({ACTIVE_KEY})
58
+ # (as opposed to being masked by a config file)
59
+ #
60
+ # @return [Hash{String => Boolean}]
61
+ #
62
+ attr_reader :status
63
+
17
64
  def hash # :nodoc:
18
65
  sort_key.hash
19
66
  end
@@ -28,29 +75,38 @@ module Reek
28
75
 
29
76
  def contains_all?(patterns)
30
77
  rpt = sort_key.to_s
31
- return patterns.all? {|exp| exp === rpt}
78
+ return patterns.all? {|patt| patt === rpt}
79
+ end
80
+
81
+ def matches?(klass, patterns)
82
+ @smell.values.include?(klass.to_s) and contains_all?(patterns)
32
83
  end
33
84
 
34
85
  def sort_key
35
- [@context, @message, smell_name]
86
+ [@location[CONTEXT_KEY], @smell[MESSAGE_KEY], smell_name]
36
87
  end
37
88
 
38
89
  protected :sort_key
39
90
 
40
91
  def report(format)
41
- format.gsub(/\%s/, smell_name).gsub(/\%c/, @context).gsub(/\%w/, @message).gsub(/\%m/, @is_masked ? '(masked) ' : '')
92
+ format.gsub(/\%s/, smell_name).
93
+ gsub(/\%c/, @location[CONTEXT_KEY]).
94
+ gsub(/\%w/, @smell[MESSAGE_KEY]).
95
+ gsub(/\%m/, @status[ACTIVE_KEY] ? '' : '(masked) ')
42
96
  end
43
97
 
44
98
  def report_on(report)
45
- if @is_masked
46
- report.found_masked_smell(self)
47
- else
99
+ if @status[ACTIVE_KEY]
48
100
  report.found_smell(self)
101
+ else
102
+ report.found_masked_smell(self)
49
103
  end
50
104
  end
51
105
 
106
+ private
107
+
52
108
  def smell_name
53
- @smell.gsub(/([a-z])([A-Z])/) { |sub| "#{$1} #{$2}"}.split.join(' ')
109
+ @smell[CLASS_KEY].gsub(/([a-z])([A-Z])/) { |sub| "#{$1} #{$2}"}.split.join(' ')
54
110
  end
55
111
  end
56
112
  end
@@ -0,0 +1,29 @@
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'attribute')
2
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'boolean_parameter')
3
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'class_variable')
4
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'control_couple')
5
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'data_clump')
6
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'duplication')
7
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'feature_envy')
8
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'irresponsible_module')
9
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'large_class')
10
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'long_method')
11
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'long_parameter_list')
12
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'long_yield_list')
13
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'nested_iterators')
14
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'simulated_polymorphism')
15
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'uncommunicative_method_name')
16
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'uncommunicative_module_name')
17
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'uncommunicative_parameter_name')
18
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'uncommunicative_variable_name')
19
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smells', 'utility_function')
20
+ # SMELL: Duplication -- all these should be found automagically
21
+
22
+ module Reek
23
+
24
+ #
25
+ # This module contains the various smell detectors.
26
+ #
27
+ module Smells
28
+ end
29
+ end
@@ -1,4 +1,6 @@
1
- require 'reek/smells/smell_detector'
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
2
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
3
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'core', 'smell_configuration')
2
4
 
3
5
  module Reek
4
6
  module Smells
@@ -25,24 +27,21 @@ module Reek
25
27
  end
26
28
 
27
29
  def self.default_config
28
- super.adopt(SmellConfiguration::ENABLED_KEY => false)
30
+ super.adopt(Core::SmellConfiguration::ENABLED_KEY => false)
29
31
  end
30
32
 
31
- def initialize(config = Attribute.default_config)
32
- super(config)
33
+ def initialize(source, config = Attribute.default_config)
34
+ super(source, config)
33
35
  end
34
36
 
35
37
  #
36
38
  # Checks whether the given class declares any attributes.
37
39
  # Remembers any smells found.
38
40
  #
39
- def examine_context(mod)
40
- # SMELL: Duplication
41
- # MethodContext, ClassContext and ModuleContext all know which
42
- # calls constitute attribute declarations. Need a method on
43
- # ModuleContext: each_public_call.select [names] {...}
44
- attributes_in(mod).each do |attr|
45
- found(mod, "declares the attribute #{attr}")
41
+ def examine_context(module_ctx)
42
+ attributes_in(module_ctx).each do |attr, line|
43
+ found(module_ctx, "declares the attribute #{attr}", '',
44
+ {'attribute' => attr.to_s}, [line])
46
45
  end
47
46
  end
48
47
 
@@ -50,11 +49,11 @@ module Reek
50
49
  # Collects the names of the class variables declared and/or used
51
50
  # in the given module.
52
51
  #
53
- def attributes_in(mod)
52
+ def attributes_in(module_ctx)
54
53
  result = Set.new
55
- mod.local_nodes(:call) do |call_node|
54
+ module_ctx.local_nodes(:call) do |call_node|
56
55
  if ATTRIBUTE_METHODS.include?(call_node.method_name)
57
- call_node.arg_names.each {|arg| result << arg }
56
+ call_node.arg_names.each {|arg| result << [arg, call_node.line] }
58
57
  end
59
58
  end
60
59
  result
@@ -0,0 +1,33 @@
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
2
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
3
+
4
+ module Reek
5
+ module Smells
6
+
7
+ #
8
+ # A Boolean parameter effectively permits a method's caller
9
+ # to decide which execution path to take. The
10
+ # offending parameter is a kind of Control Couple.
11
+ #
12
+ # Currently Reek can only detect a Boolean parameter when it has a
13
+ # default initializer.
14
+ #
15
+ class BooleanParameter < SmellDetector
16
+
17
+ #
18
+ # Checks whether the given method has a Boolean parameter.
19
+ # Remembers any smells found.
20
+ #
21
+ def examine_context(method_ctx)
22
+ method_ctx.parameters.default_assignments.each do |param, value|
23
+ next unless [:true, :false].include?(value[0])
24
+ smell = SmellWarning.new('ControlCouple', method_ctx.full_name, [method_ctx.exp.line],
25
+ "has boolean parameter '#{param.to_s}'", @masked,
26
+ @source, 'BooleanParameter', {'parameter' => param.to_s})
27
+ @smells_found << smell
28
+ #SMELL: serious duplication
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,4 +1,6 @@
1
- require 'reek/smells/smell_detector'
1
+ require 'set'
2
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
3
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
2
4
 
3
5
  module Reek
4
6
  module Smells
@@ -21,9 +23,9 @@ module Reek
21
23
  # Checks whether the given class or module declares any class variables.
22
24
  # Remembers any smells found.
23
25
  #
24
- def examine_context(mod)
25
- class_variables_in(mod).each do |cvar_name|
26
- found(mod, "declares the class variable #{cvar_name}")
26
+ def examine_context(ctx)
27
+ class_variables_in(ctx).each do |cvar_name|
28
+ found(ctx, "declares the class variable #{cvar_name}", '', {'variable' => cvar_name.to_s})
27
29
  end
28
30
  end
29
31
 
@@ -31,11 +33,11 @@ module Reek
31
33
  # Collects the names of the class variables declared and/or used
32
34
  # in the given module.
33
35
  #
34
- def class_variables_in(mod)
36
+ def class_variables_in(ctx)
35
37
  result = Set.new
36
38
  collector = proc { |cvar_node| result << cvar_node.name }
37
39
  [:cvar, :cvasgn, :cvdecl].each do |stmt_type|
38
- mod.local_nodes(stmt_type, &collector)
40
+ ctx.local_nodes(stmt_type, &collector)
39
41
  end
40
42
  result
41
43
  end
@@ -1,6 +1,6 @@
1
- require 'reek/smells/smell_detector'
2
- require 'reek/smell_warning'
3
- require 'reek/sexp_formatter'
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
2
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
3
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'source')
4
4
 
5
5
  module Reek
6
6
  module Smells
@@ -33,27 +33,43 @@ module Reek
33
33
  # method probably has more than one responsibility,
34
34
  # because it includes at least two different code paths.
35
35
  #
36
+ # One possible solution is to use the Strategy Pattern
37
+ # to pass into the callee what must be done. This is
38
+ # not considered to be control coupling because the
39
+ # callee will do the same thing with the strategy,
40
+ # whatever it happens to be. Sometimes in Ruby the
41
+ # strategy may actually just be a block passed in, and
42
+ # that remains next to where the caller invokes it in
43
+ # the source code.
44
+ #
36
45
  class ControlCouple < SmellDetector
37
46
 
38
- def self.contexts # :nodoc:
39
- [:if, :defn, :defs]
40
- end
41
-
42
47
  #
43
- # Checks whether the given conditional statement relies on a control couple.
48
+ # Checks whether the given method chooses its execution path
49
+ # by testing the value of one of its parameters.
44
50
  # Remembers any smells found.
45
51
  #
46
- def examine_context(ctx)
47
- case ctx
48
- when IfContext
49
- return unless ctx.tests_a_parameter?
50
- found(ctx, "is controlled by argument #{SexpFormatter.format(ctx.if_expr)}")
51
- else
52
- ctx.parameters.default_assignments.each do |param, value|
53
- next unless [:true, :false].include?(value[0])
54
- found(ctx, "is controlled by argument #{param.to_s}")
52
+ def examine_context(method_ctx)
53
+ control_parameters(method_ctx).each do |cond, occurs|
54
+ param = Source::SexpFormatter.format(cond)
55
+ lines = occurs.map {|exp| exp.line}
56
+ found(method_ctx, "is controlled by argument #{param}",
57
+ 'ControlParameter', {'parameter' => param}, lines)
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def control_parameters(method_ctx)
64
+ params = method_ctx.exp.parameter_names
65
+ result = Hash.new {|hash,key| hash[key] = []}
66
+ method_ctx.local_nodes(:if) do |if_node|
67
+ cond = if_node[1]
68
+ if cond[0] == :lvar and params.include?(cond[1])
69
+ result[cond].push(cond)
55
70
  end
56
71
  end
72
+ result
57
73
  end
58
74
  end
59
75
  end
@@ -1,6 +1,6 @@
1
- require 'reek/smells/smell_detector'
2
- require 'reek/smell_warning'
3
- require 'reek/sexp_formatter'
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
2
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
3
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'source')
4
4
 
5
5
  module Reek
6
6
  module Smells
@@ -39,8 +39,8 @@ module Reek
39
39
  )
40
40
  end
41
41
 
42
- def initialize(config = DataClump.default_config)
43
- super(config)
42
+ def initialize(source, config = DataClump.default_config)
43
+ super(source, config)
44
44
  end
45
45
 
46
46
  #
@@ -51,7 +51,10 @@ module Reek
51
51
  max_copies = value(MAX_COPIES_KEY, ctx, DEFAULT_MAX_COPIES)
52
52
  min_clump_size = value(MIN_CLUMP_SIZE_KEY, ctx, DEFAULT_MIN_CLUMP_SIZE)
53
53
  MethodGroup.new(ctx, min_clump_size, max_copies).clumps.each do |clump, occurs|
54
- found(ctx, "takes parameters #{DataClump.print_clump(clump)} to #{occurs} methods")
54
+ found(ctx, "takes parameters #{DataClump.print_clump(clump)} to #{occurs} methods",
55
+ 'DataClump', {'parameters' => clump.map {|name| name.to_s}, 'occurrences' => occurs})
56
+ # SMELL: name.to_s is becoming a nuisance
57
+ # TODO: record the methods in [lines] and in the hash
55
58
  end
56
59
  end
57
60
 
@@ -62,6 +65,7 @@ module Reek
62
65
  end
63
66
 
64
67
  # Represents a group of methods
68
+ # @private
65
69
  class MethodGroup # :nodoc:
66
70
 
67
71
  def self.intersection_of_parameters_of(methods)
@@ -1,6 +1,6 @@
1
- require 'reek/smells/smell_detector'
2
- require 'reek/smell_warning'
3
- require 'reek/sexp_formatter'
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
2
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
3
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'source')
4
4
 
5
5
  module Reek
6
6
  module Smells
@@ -30,22 +30,32 @@ module Reek
30
30
  super.adopt(MAX_ALLOWED_CALLS_KEY => DEFAULT_MAX_CALLS)
31
31
  end
32
32
 
33
- def initialize(config = Duplication.default_config)
34
- super(config)
33
+ def initialize(source, config = Duplication.default_config)
34
+ super(source, config)
35
35
  end
36
36
 
37
- def examine_context(method)
38
- smelly_calls(method).each do |call_data|
39
- num = call_data[1]
40
- multiple = num == 2 ? 'twice' : "#{num} times"
41
- found(method, "calls #{SexpFormatter.format(call_data[0])} #{multiple}")
37
+ def examine_context(method_ctx)
38
+ calls(method_ctx).each do |call_exp, copies|
39
+ occurs = copies.length
40
+ next if occurs <= value(MAX_ALLOWED_CALLS_KEY, method_ctx, DEFAULT_MAX_CALLS)
41
+ call = Source::SexpFormatter.format(call_exp)
42
+ multiple = occurs == 2 ? 'twice' : "#{occurs} times"
43
+ found(method_ctx, "calls #{call} #{multiple}",
44
+ 'DuplicateMethodCall', {'call' => call, 'occurrences' => occurs},
45
+ copies.map {|exp| exp.line})
42
46
  end
43
47
  end
44
-
45
- def smelly_calls(method) # :nodoc:
46
- method.calls.select do |key,val|
47
- val > value(MAX_ALLOWED_CALLS_KEY, method, DEFAULT_MAX_CALLS) and key[2] != :new
48
+
49
+ def calls(method_ctx)
50
+ result = Hash.new {|hash,key| hash[key] = []}
51
+ method_ctx.local_nodes(:call) do |call_node|
52
+ next if call_node.method_name == :new
53
+ result[call_node].push(call_node)
54
+ end
55
+ method_ctx.local_nodes(:attrasgn) do |asgn_node|
56
+ result[asgn_node].push(asgn_node)
48
57
  end
58
+ result
49
59
  end
50
60
  end
51
61
  end
@@ -1,6 +1,6 @@
1
- require 'reek/smells/smell_detector'
2
- require 'reek/smell_warning'
3
- require 'reek/sexp_formatter'
1
+ require File.join( File.dirname( File.expand_path(__FILE__)), 'smell_detector')
2
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'smell_warning')
3
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'source')
4
4
 
5
5
  module Reek
6
6
  module Smells
@@ -40,9 +40,14 @@ module Reek
40
40
  # might "belong" on another class.
41
41
  # Remembers any smells found.
42
42
  #
43
- def examine_context(context)
44
- context.envious_receivers.each do |ref|
45
- found(context, "refers to #{SexpFormatter.format(ref)} more than self")
43
+ def examine_context(method_ctx)
44
+ method_ctx.envious_receivers.each do |ref, occurs|
45
+ target = Source::SexpFormatter.format(ref)
46
+ smell = SmellWarning.new('LowCohesion', method_ctx.full_name, [method_ctx.exp.line],
47
+ "refers to #{target} more than self", @masked,
48
+ @source, 'FeatureEnvy', {'receiver' => target, 'references' => occurs})
49
+ @smells_found << smell
50
+ #SMELL: serious duplication
46
51
  end
47
52
  end
48
53
  end