snoot 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +20 -0
  3. data/LICENSE +21 -0
  4. data/README.md +49 -0
  5. data/data/reek_docs/API.md +174 -0
  6. data/data/reek_docs/Attribute.md +39 -0
  7. data/data/reek_docs/Basic-Smell-Options.md +85 -0
  8. data/data/reek_docs/Boolean-Parameter.md +54 -0
  9. data/data/reek_docs/Class-Variable.md +40 -0
  10. data/data/reek_docs/Code-Smells.md +39 -0
  11. data/data/reek_docs/Command-Line-Options.md +119 -0
  12. data/data/reek_docs/Control-Couple.md +26 -0
  13. data/data/reek_docs/Control-Parameter.md +32 -0
  14. data/data/reek_docs/Data-Clump.md +46 -0
  15. data/data/reek_docs/Duplicate-Method-Call.md +264 -0
  16. data/data/reek_docs/Feature-Envy.md +93 -0
  17. data/data/reek_docs/How-To-Write-New-Detectors.md +144 -0
  18. data/data/reek_docs/How-reek-works-internally.md +114 -0
  19. data/data/reek_docs/Instance-Variable-Assumption.md +163 -0
  20. data/data/reek_docs/Irresponsible-Module.md +47 -0
  21. data/data/reek_docs/LICENSE +20 -0
  22. data/data/reek_docs/Large-Class.md +16 -0
  23. data/data/reek_docs/Long-Parameter-List.md +39 -0
  24. data/data/reek_docs/Long-Yield-List.md +37 -0
  25. data/data/reek_docs/Manual-Dispatch.md +30 -0
  26. data/data/reek_docs/Missing-Safe-Method.md +92 -0
  27. data/data/reek_docs/Module-Initialize.md +62 -0
  28. data/data/reek_docs/Nested-Iterators.md +59 -0
  29. data/data/reek_docs/Nil-Check.md +47 -0
  30. data/data/reek_docs/RSpec-matchers.md +129 -0
  31. data/data/reek_docs/Rake-Task.md +66 -0
  32. data/data/reek_docs/Reek-4-to-Reek-5-migration.md +188 -0
  33. data/data/reek_docs/Reek-Driven-Development.md +46 -0
  34. data/data/reek_docs/Repeated-Conditional.md +47 -0
  35. data/data/reek_docs/Simulated-Polymorphism.md +16 -0
  36. data/data/reek_docs/Smell-Suppression.md +96 -0
  37. data/data/reek_docs/Style-Guide.md +19 -0
  38. data/data/reek_docs/Subclassed-From-Core-Class.md +79 -0
  39. data/data/reek_docs/Too-Many-Constants.md +37 -0
  40. data/data/reek_docs/Too-Many-Instance-Variables.md +43 -0
  41. data/data/reek_docs/Too-Many-Methods.md +56 -0
  42. data/data/reek_docs/Too-Many-Statements.md +54 -0
  43. data/data/reek_docs/Uncommunicative-Method-Name.md +94 -0
  44. data/data/reek_docs/Uncommunicative-Module-Name.md +92 -0
  45. data/data/reek_docs/Uncommunicative-Name.md +18 -0
  46. data/data/reek_docs/Uncommunicative-Parameter-Name.md +90 -0
  47. data/data/reek_docs/Uncommunicative-Variable-Name.md +96 -0
  48. data/data/reek_docs/Unused-Parameters.md +28 -0
  49. data/data/reek_docs/Unused-Private-Method.md +101 -0
  50. data/data/reek_docs/Utility-Function.md +57 -0
  51. data/data/reek_docs/Versioning-Policy.md +7 -0
  52. data/data/reek_docs/YAML-Reports.md +93 -0
  53. data/exe/snoot +5 -0
  54. data/lib/snoot/analyse_run/decision.rb +62 -0
  55. data/lib/snoot/analyse_run/result.rb +12 -0
  56. data/lib/snoot/analyse_run.rb +70 -0
  57. data/lib/snoot/analyser_orchestration/default.rb +149 -0
  58. data/lib/snoot/analyser_orchestration/result_mapping.rb +52 -0
  59. data/lib/snoot/analyser_orchestration.rb +21 -0
  60. data/lib/snoot/analyser_result.rb +14 -0
  61. data/lib/snoot/cli/event.rb +13 -0
  62. data/lib/snoot/cli/pipeline.rb +14 -0
  63. data/lib/snoot/cli.rb +147 -0
  64. data/lib/snoot/findings.rb +23 -0
  65. data/lib/snoot/render_report.rb +82 -0
  66. data/lib/snoot/run.rb +35 -0
  67. data/lib/snoot/state_error.rb +9 -0
  68. data/lib/snoot/value_types.rb +20 -0
  69. data/lib/snoot/version.rb +5 -0
  70. data/lib/snoot.rb +21 -0
  71. data/snoot.allium +482 -0
  72. metadata +160 -0
@@ -0,0 +1,32 @@
1
+ # Control Parameter
2
+
3
+ ## Introduction
4
+
5
+ _Control Parameter_ is a case of [Control Couple](Control-Couple.md).
6
+
7
+ ## Example
8
+
9
+ A simple example would be the `quoted` parameter in the following method:
10
+
11
+ ```ruby
12
+ def write(quoted)
13
+ if quoted
14
+ write_quoted @value
15
+ else
16
+ write_unquoted @value
17
+ end
18
+ end
19
+ ```
20
+
21
+ Fixing those problems is out of the scope of this document but an easy solution
22
+ could be to remove the `write` method altogether and to move the calls to
23
+ `write_quoted` and `write_unquoted` to the caller of `write`.
24
+
25
+ ## Current Support in Reek
26
+
27
+ Reek warns about _Control Parameter_ when a method parameter or block parameter is
28
+ the tested value in a conditional statement.
29
+
30
+ ## Configuration
31
+
32
+ _Control Parameter_ supports the [Basic Smell Options](Basic-Smell-Options.md).
@@ -0,0 +1,46 @@
1
+ # Data Clump
2
+
3
+ ## Introduction
4
+
5
+ In general, a _Data Clump_ occurs when the same two or three items frequently
6
+ appear together in classes and parameter lists, or when a group of instance
7
+ variable names start or end with similar substrings.
8
+
9
+ The recurrence of the items often means there is duplicate code spread around to handle them. There may be an abstraction missing from the code, making the system harder to understand.
10
+
11
+ ## Example
12
+
13
+ Given
14
+
15
+ ```ruby
16
+ class Dummy
17
+ def x(y1,y2); end
18
+ def y(y1,y2); end
19
+ def z(y1,y2); end
20
+ end
21
+ ```
22
+
23
+ Reek would emit the following warning:
24
+
25
+ ```
26
+ test.rb -- 1 warning:
27
+ [2, 3, 4]:Dummy takes parameters [y1, y2] to 3 methods (DataClump)
28
+ ```
29
+
30
+ A possible way to fix this problem (quoting from [Martin Fowler](http://martinfowler.com/bliki/DataClump.html)):
31
+
32
+ > The first step is to replace data clumps with objects and use the objects whenever you see them. An immediate benefit is that you'll shrink some parameter lists. The interesting stuff happens as you begin to look for behavior to move into the new objects.
33
+
34
+ ## Current Support in Reek
35
+
36
+ Reek looks for a group of two or more parameters with the same names that are expected by three or more methods of a class.
37
+
38
+ ## Configuration
39
+
40
+ Reek's _Data Clump_ detector offers the [Basic Smell Options](Basic-Smell-Options.md), plus:
41
+
42
+ | Option | Value | Effect |
43
+ | -----------------|-------------|---------|
44
+ | `max_copies` | integer | The maximum number of methods that are permitted to take the same group of parameters. Defaults to 2. |
45
+ | `min_clump_size` | integer | The smallest number of parameters that can be reported as a clump. Defaults to 2. |
46
+
@@ -0,0 +1,264 @@
1
+ # Duplicate Method Call
2
+
3
+ ## Introduction
4
+
5
+ Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.
6
+
7
+ Let's look at an example that is quite common in the Rails world:
8
+
9
+ ```ruby
10
+ def not_production?
11
+ Rails.env.development? || Rails.env.test?
12
+ end
13
+ ```
14
+
15
+ While this duplicate usage of `Rails.env` might seem innocuous there are 2 problems with it:
16
+
17
+ 1.) Efficiency
18
+
19
+ ```ruby
20
+ Rails.env.development? || Rails.env.test?
21
+ ```
22
+
23
+ is not as efficient as it could be. If the call to `env` is not memoized your basically paying twice in terms of computation for something that you should only pay once.
24
+
25
+ Here
26
+
27
+ ```ruby
28
+ Rails.env.development? || Rails.env.test?
29
+ ```
30
+
31
+ you have 4 method calls while here:
32
+
33
+ ```ruby
34
+ env = Rails.env
35
+ env.development? || env.test?
36
+ ```
37
+
38
+ you have one assignment (which is very cheap in terms of computation) and 3 method calls.
39
+ The difference might not be much here but just imagine you're writing a high performance app or you doing some expensive database calls in each method call.
40
+
41
+ It doesn't really matter though if the efficiency difference is significant. This is a matter of principle - we believe that being efficient is one of the vital traits of good software.
42
+
43
+ 2.) Maintainability
44
+
45
+ The second point is a bit more subtle. This
46
+
47
+ ```ruby
48
+ env = Rails.env
49
+ env.development? || env.test?
50
+ ```
51
+
52
+ is a lot more intention revealing than
53
+
54
+ ```ruby
55
+ Rails.env.development? || Rails.env.test?
56
+ ```
57
+
58
+ Here
59
+
60
+ ```ruby
61
+ env = Rails.env
62
+ env.development? || env.test?
63
+ ```
64
+
65
+ I'm very clear on what I do: I get the environment and then I run some checks on it.
66
+
67
+ Here
68
+
69
+ ```ruby
70
+ Rails.env.development? || Rails.env.test?
71
+ ```
72
+
73
+ I'm not very clear on what I do and it requires quite more mental effort: Ok, so I'm talking to Rails, getting the environment and then running a check on it ...or .....oh, I get the same Rails constant again, get the same environment and run another check on it.
74
+
75
+ ## Example
76
+
77
+ Here's a very much simplified and contrived example. The following method will report a warning:
78
+
79
+ ```ruby
80
+ def double_thing
81
+ @other.thing + @other.thing
82
+ end
83
+ ```
84
+
85
+ One quick approach to silence Reek would be to refactor the code thus:
86
+
87
+ ```ruby
88
+ def double_thing
89
+ thing = @other.thing
90
+ thing + thing
91
+ end
92
+ ```
93
+
94
+ A slightly different approach would be to replace all calls in `double_thing` by calls to `thing`:
95
+
96
+ ```ruby
97
+ class Other
98
+ def double_thing
99
+ thing + thing
100
+ end
101
+
102
+ def thing
103
+ @other.thing
104
+ end
105
+ end
106
+ ```
107
+
108
+ The approach you take will depend on balancing other factors in your code.
109
+
110
+ ## Current support in Reek
111
+
112
+ Reek's Duplicate Method Call detector checks for repeated identical method calls within
113
+ any one method definition. This is intended to complement the checks performed by tools
114
+ such as [Flay](http://ruby.sadi.st/Flay.html) and [Simian](http://www.redhillconsulting.com.au/products/simian/).
115
+
116
+ ## Edge cases
117
+
118
+ Be aware that there are some edge cases like this code:
119
+
120
+ ```ruby
121
+ class Foo
122
+ def bar(switch)
123
+ case switch
124
+ when :a
125
+ ->(arg) { arg.call_me(:maybe); do_something }
126
+ when :b
127
+ ->(arg) { arg.call_me(:maybe); do_something_else }
128
+ when :c
129
+ ->(arg) { arg.call_me(:maybe); do_something_different }
130
+ end
131
+ end
132
+ end
133
+ ```
134
+
135
+ Reek cannot reliably detect that each call's receiver is a different arg and will report:
136
+
137
+ ```
138
+ [5, 7, 9]:DuplicateMethodCall: Foo#bar calls 'arg.call_me(:maybe)' 3 times
139
+ ```
140
+
141
+ If you're running into this problem you can disable this smell detector for this method either via
142
+ configuration:
143
+
144
+ ```yaml
145
+ ---
146
+ DuplicateMethodCall:
147
+ exclude:
148
+ - 'Foo#bar'
149
+ ```
150
+
151
+ or via source code comment:
152
+
153
+ ```ruby
154
+ class Foo
155
+ # :reek:DuplicateMethodCall
156
+ def bar(switch)
157
+ # ....
158
+ end
159
+ end
160
+ ```
161
+
162
+ ## Configuration
163
+
164
+ Reek's Duplicate Method Call detector currently offers the [Basic Smell Options](Basic-Smell-Options.md), plus:
165
+
166
+ Option | Value | Effect
167
+ -------|-------|-------
168
+ `max_calls` | integer | The maximum number of duplicate calls allowed within a method. Defaults to 1.
169
+ `allow_calls` | an array of strings or regular expressions | Ignores any context who matches it |
170
+
171
+ ## Example configuration
172
+
173
+ ### Adjusting `max_calls`
174
+
175
+ Imagine code like this:
176
+
177
+ ```ruby
178
+ class Alfa
179
+ def bravo
180
+ charlie.delta
181
+ charlie.delta
182
+ end
183
+ end
184
+ ```
185
+
186
+ This would report:
187
+
188
+ >>
189
+ src.rb -- 1 warning:
190
+ [4, 5]:DuplicateMethodCall: Alfa#bravo calls 'charlie.delta' 2 times
191
+
192
+ If you want to allow those double calls here you can disable it in 2 different ways:
193
+
194
+ 1.) Via source code comment:
195
+
196
+ ```ruby
197
+ class Alfa
198
+ # :reek:DuplicateMethodCall { max_calls: 2 }
199
+ def bravo
200
+ charlie.delta
201
+ charlie.delta
202
+ end
203
+ end
204
+ ```
205
+
206
+ 2.) Via configuration file:
207
+
208
+ ```yaml
209
+ DuplicateMethodCall:
210
+ max_calls: 2
211
+ ```
212
+
213
+ Note though that the latter way will set `max_calls` to 2 for all instances
214
+ of the smell detector which might not be what you want - in this case
215
+ you'll have to use source code comments.
216
+
217
+ ### Adjusting `allow_calls`
218
+
219
+ Imagine code like this:
220
+
221
+ ```ruby
222
+ class Alfa
223
+ def bravo
224
+ charlie.delta
225
+ charlie.delta
226
+ echo.foxtrot
227
+ echo.foxtrot
228
+ end
229
+ end
230
+ ```
231
+
232
+ This would report:
233
+
234
+ >>
235
+ src.rb -- 2 warnings:
236
+ [4, 5]:DuplicateMethodCall: Alfa#bravo calls charlie.delta 2 times
237
+ [6, 7]:DuplicateMethodCall: Alfa#bravo calls echo.foxtrot 2 times
238
+
239
+ So let's say you're ok with the `echo.foxtrot` calls you can stop reporting them like this:
240
+
241
+ 1.) Via source code comment:
242
+
243
+ ```ruby
244
+ class Alfa
245
+ # :reek:DuplicateMethodCall { allow_calls: ['echo.foxtrot'] }
246
+ def bravo
247
+ charlie.delta
248
+ charlie.delta
249
+ echo.foxtrot
250
+ echo.foxtrot
251
+ end
252
+ end
253
+ ```
254
+
255
+ 2.) Via configuration file:
256
+
257
+ ```yaml
258
+ DuplicateMethodCall:
259
+ allow_calls:
260
+ - 'echo.foxtrot'
261
+ ```
262
+
263
+ Note though that the latter way will allow those calls across your source code which might not be what you want.
264
+ In this case you'll have to use source code comments.
@@ -0,0 +1,93 @@
1
+ # Feature Envy
2
+
3
+ ## Introduction
4
+
5
+ _Feature Envy_ occurs when a code fragment references another object more often than it references itself, or when several clients do the same series of manipulations on a particular type of object.
6
+
7
+ _Feature Envy_ reduces the code's ability to communicate intent: code that "belongs" on one class but which is located in another can be hard to find, and may upset the "System of Names" in the host class.
8
+
9
+ _Feature Envy_ also affects the design's flexibility: A code fragment that is in the wrong class creates couplings that may not be natural within the application's domain, and creates a loss of cohesion in the unwilling host class.
10
+
11
+ _Feature Envy_ often arises because it must manipulate other objects (usually its arguments) to get them into a useful form, and one force preventing them (the arguments) doing this themselves is that the common knowledge lives outside the arguments, or the arguments are of too basic a type to justify extending that type. Therefore there must be something which 'knows' about the contents or purposes of the arguments. That thing would have to be more than just a basic type, because the basic types are either containers which don't know about their contents, or they are single objects which can't capture their relationship with their fellows of the same type. So, this thing with the extra knowledge should be reified into a class, and the utility method will most likely belong there.
12
+
13
+ ## Example
14
+
15
+ Running Reek on:
16
+
17
+ ```ruby
18
+ class Warehouse
19
+ def sale_price(item)
20
+ (item.price - item.rebate) * @vat
21
+ end
22
+ end
23
+ ```
24
+
25
+ would report:
26
+
27
+ ```bash
28
+ Warehouse#sale_price refers to item more than self (FeatureEnvy)
29
+ ```
30
+
31
+ since this:
32
+
33
+ ```ruby
34
+ (item.price - item.rebate)
35
+ ```
36
+
37
+ belongs to the Item class, not the Warehouse.
38
+
39
+ ## Current Support in Reek
40
+
41
+ _Feature Envy_ reports any method that refers to self less often than it refers to (ie. send messages to) some other object.
42
+
43
+ ## Edge cases
44
+
45
+ Be aware that there are some edge cases like this code:
46
+
47
+ ```ruby
48
+ class Foo
49
+ def initialize
50
+ @map = {
51
+ a: ->(arg) { arg.css('table') },
52
+ b: ->(arg) { arg.css('div') },
53
+ c: ->(arg) { arg.css('span') }
54
+ }
55
+ end
56
+ end
57
+ ```
58
+
59
+ Reek cannot reliably detect that each call's receiver is a different arg and will report:
60
+
61
+ ```
62
+ [4, 5, 6]:FeatureEnvy: Foo#initialize refers to 'arg' more than self (maybe move it to another class?)
63
+ ```
64
+
65
+ If you're running into this problem you can disable this smell detector for this method either via
66
+ configuration:
67
+
68
+ ```yaml
69
+ ---
70
+ FeatureEnvy:
71
+ exclude:
72
+ - 'Foo#bar'
73
+ ```
74
+
75
+ or via source code comment:
76
+
77
+ ```ruby
78
+ class Foo
79
+ # :reek:FeatureEnvy
80
+ def initialize
81
+ @map = {
82
+ # ....
83
+ end
84
+ end
85
+ ```
86
+
87
+ ## Differences to _Utility Function_
88
+
89
+ _Feature Envy_ is only triggered if there are some references to self and _[Utility Function](Utility-Function.md)_ is triggered if there are no references to self.
90
+
91
+ ## Configuration
92
+
93
+ _Feature Envy_ supports the [Basic Smell Options](Basic-Smell-Options.md).
@@ -0,0 +1,144 @@
1
+ ## How to write new detectors
2
+
3
+ ### Outline what you have in mind
4
+
5
+ Before starting to code you should discuss the overall idea for your new smell detector with
6
+ us in a corresponding github issue.
7
+ We all should have a solid understanding of what this detector actually reports, the edge cases
8
+ it covers and the overall rationale behind it.
9
+
10
+ ### Structure
11
+
12
+ All smell detectors reside in `lib/reek/smell_detectors` and have the following base structure:
13
+
14
+ ```ruby
15
+ require_relative 'base_detector'
16
+ require_relative 'smell_warning'
17
+
18
+ module Reek
19
+ module SmellDetectors
20
+ #
21
+ # Here goes your introduction for this detector.
22
+ #
23
+ # See {file:docs/Your-Detector.md} for details.
24
+ class YourDetector < BaseDetector
25
+ def self.contexts
26
+ [:class] # In case you're operating on class contexts only - just an example.
27
+ end
28
+
29
+ #
30
+ # Here you should document what you expect the detector's context to look
31
+ # like.
32
+ #
33
+ # @return [Array<SmellWarning>]
34
+ #
35
+ def sniff
36
+ # "found_smells" below is just an abstraction for
37
+ # "find the smells in question" and iterate over them.
38
+ # This can just be a method but it can also be a more sophisticated set up.
39
+ # Check out other smell detectors to get a feeling for what to do here.
40
+ found_smells.map do |smell|
41
+ # "smell_warning" is defined in BaseDetector and should be used by you
42
+ # to construct smell warnings
43
+ smell_warning(
44
+ lines: [], # lines on which the smell was detected
45
+ message: "...", # the message that is printed on STDOUT
46
+ # whatever you interpolate into the "message" should go into
47
+ # parameters below - if you do not interpolate anything you
48
+ # can omit this
49
+ parameters: { })
50
+ end
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ # Here goes everything you need for finding smells.
57
+ end
58
+ end
59
+ end
60
+ ```
61
+
62
+ For your detector to be properly loaded you need to require it in `lib/reek/smell_detectors.rb` as well.
63
+
64
+ ### defaults.reek.yml
65
+
66
+ After you ran
67
+
68
+ ```
69
+ bundle exec rake
70
+ ```
71
+
72
+ for the first time with your shiny new detector in place the `docs/defaults.reek.yml`
73
+ file should have been updated automatically. Make sure you don't forget to check
74
+ in those changes as well.
75
+
76
+ ### Documentation
77
+
78
+ * Above every `SmellDetector::sniff` method it should be documented what the expected AST is
79
+ * Every detector should have a separate documentation page in /docs. You can
80
+ take any arbitrary existing smell detector documentation page as template (since
81
+ they all have the same structure already)
82
+ * The detector should be listed under [Code Smells](docs/Code-Smells.md)
83
+ * Depending on what your detector does it might make sense to add it to other doc pages as
84
+ well e.g. [Simulated Polymorphism](docs/Simulated-Polymorphism.md)
85
+
86
+ ### Rspec examples
87
+
88
+ All smell detector specs start out with 2 generic examples like below - the second one
89
+ only if it makes sense.
90
+ Here's what it looks like for `UncommunicativeVariableName`:
91
+
92
+ ```ruby
93
+ it 'reports the right values' do
94
+ src = <<-RUBY
95
+ def alfa
96
+ bravo = 5
97
+ end
98
+ RUBY
99
+
100
+ expect(src).to reek_of(:UncommunicativeVariableName,
101
+ lines: [2],
102
+ context: 'alfa',
103
+ message: "has the variable name 'bravo'",
104
+ source: 'string',
105
+ name: 'bravo')
106
+ end
107
+
108
+ it 'does count all occurrences' do
109
+ src = <<-RUBY
110
+ def alfa
111
+ bravo = 3
112
+ charlie = 7
113
+ end
114
+ RUBY
115
+
116
+ expect(src).to reek_of(:UncommunicativeVariableName,
117
+ lines: [2],
118
+ name: 'bravo')
119
+ expect(src).to reek_of(:UncommunicativeVariableName,
120
+ lines: [3],
121
+ name: 'charlie')
122
+ end
123
+ ```
124
+
125
+ The following examples should then cover the detector specific features.
126
+
127
+ ### Schema validation
128
+
129
+ We make use of [dry-schema](https://github.com/dry-rb/dry-schema) to validate the
130
+ the reek configuration file (usually named `.reek.yml` in the root of a project).
131
+ The validation will warn reek users of missing or incorrectly defined configuration.
132
+
133
+ If you add or modify a detector you will need to make sure that you update the
134
+ [schema](lib/reek/configuration/schema.rb) file. For help with the schema syntax
135
+ you can refer to the [dry-schema documentation](https://dry-rb.org/gems/dry-schema).
136
+ You can also take a look at the existing schema rules for they all look fairly
137
+ similar.
138
+
139
+ ### Cucumber features
140
+
141
+ We are trying to write as few Cucumber features as possible.
142
+ Normally, there should be no need to write a new feature for a new smell detector.
143
+ If you feel like this is necessary in this case, please discuss this with us via
144
+ github issue or in your work-in-progress pull request before doing anything.
@@ -0,0 +1,114 @@
1
+ # How Reek works internally
2
+
3
+
4
+ ## The big picture
5
+
6
+ ```
7
+ ["class C; end" | reek] [reek lib/*.rb] [expect(files).not_to reek_of(:LargeClass)]
8
+ \ | |
9
+ \ | |
10
+ \ | |
11
+ \ creates a | |
12
+ \ | |
13
+ \ | |
14
+ \ | |
15
+ \ | |
16
+ \---------- Application (cli/application.rb) + |
17
+ Options (cli/options) |
18
+ | |
19
+ | |
20
+ | |
21
+ | |
22
+ creates a | |
23
+ | |
24
+ | |
25
+ | |
26
+ | |
27
+ ReekCommand (cli/reek_command) |
28
+ * uses a reporter (report/report) |
29
+ * uses a SourceLocator (source/source_locator) |
30
+ / | \ |
31
+ / | \ |
32
+ / | \ |
33
+ Source Source Source (source/source_code) |
34
+ | | | |
35
+ | | | |
36
+ | | | |
37
+ Examiner | Examiner |
38
+ | |
39
+ | |
40
+ Examiner (core/examiner) --------------------------------------
41
+ * generates the AST out of the given source
42
+ * adorns the generated AST via a TreeDresser (core/tree_dresser)
43
+ * initializes a DetectorRepository with all relevant smells (smells/detector_repository)
44
+ * builds a tree of Contexts using ContextBuilder
45
+ * tells the DetectorRepository above to run each of its smell detectors above on each of the contexts
46
+ / | \
47
+ / | \
48
+ / | \
49
+ UtilityFunction FeatureEnvy TooManyMethods
50
+ \ | /
51
+ \ | /
52
+ \ | /
53
+ DetectorRepository
54
+ |
55
+ |
56
+ |
57
+ Application output
58
+
59
+ ## A closer look at how an Examiner works
60
+
61
+ The core foundation of Reek and its API is the Examiner.
62
+ As you can see above, the Examiner is run for every source it gets passed and then runs the configured SmellDetectors.
63
+ The overall workflow is like this:
64
+
65
+ Examiner
66
+ |
67
+ |
68
+ |
69
+ Initialize DetectorRepository only with eligible SmellDetectors
70
+ |
71
+ |
72
+ |
73
+ Generate the AST out of the given source using SourceCode#syntax_tree, which works like this:
74
+
75
+ - We generate a "rough" AST using the "parser" gem
76
+ - We then obtain the comments from the source code separately
77
+ - We pass this unprocessed AST and the comment_map to TreeDresser#dress which
78
+ returns an instance of Reek::AST::SexpNode with type-dependent SexpExtensions mixed in.
79
+
80
+ An example should make this more palpable.
81
+ Given:
82
+
83
+ class C
84
+ def m
85
+ puts 'nada'
86
+ end
87
+ end
88
+
89
+ The AST generated by the parser gem (consisting of Parser::AST::Node) looks like this:
90
+
91
+ (class
92
+ (const nil :C)
93
+ nil
94
+ (def :m
95
+ (args)
96
+ (send nil :puts
97
+ (str "nada"))))
98
+
99
+ TreeDresser#dress would transform this into a very similar tree, but this time not consisting
100
+ of Parser::AST::Node but of Reek::AST::SexpNode and with node-dependent SexpExtensions
101
+ mixed in (noted in []):
102
+
103
+ (class [AST::SexpExtensions::ClassNode, AST::SexpExtensions::ModuleNode]
104
+ (const nil :C) [AST::SexpExtensions::ConstNode]
105
+ nil
106
+ (def :m [AST::SexpExtensions::DefNode, AST::SexpExtensions::MethodNodeBase]
107
+ (args) [AST::SexpExtensions::ArgsNode]
108
+ (send nil :puts [AST::SexpExtensions::SendNode]
109
+ (str "nada"))))
110
+ |
111
+ |
112
+ |
113
+ A ContextBuilder then traverses this now adorned tree again and
114
+ runs all SmellDetectors from the DetectorRepository above