@@ -115,7 +115,7 @@ namespace Rubberduck.Inspections.Concrete.UnreachableCaseInspection
115
115
/// </example>
116
116
public sealed class UnreachableCaseInspection : InspectionBase , IParseTreeInspection
117
117
{
118
- private readonly IUnreachableCaseInspectorFactory _unreachableCaseInspectorFactory ;
118
+ private readonly IUnreachableCaseInspector _inspector ;
119
119
private readonly IParseTreeValueVisitor _parseTreeValueVisitor ;
120
120
private readonly IInspectionListener < VBAParser . SelectCaseStmtContext > _listener ;
121
121
@@ -128,10 +128,13 @@ public enum CaseInspectionResultType
128
128
CaseElse
129
129
}
130
130
131
- public UnreachableCaseInspection ( IDeclarationFinderProvider declarationFinderProvider , IUnreachableCaseInspectionFactoryProvider factoryProvider , IParseTreeValueVisitor parseTreeValueVisitor )
131
+ public UnreachableCaseInspection (
132
+ IDeclarationFinderProvider declarationFinderProvider ,
133
+ IUnreachableCaseInspector inspector ,
134
+ IParseTreeValueVisitor parseTreeValueVisitor )
132
135
: base ( declarationFinderProvider )
133
136
{
134
- _unreachableCaseInspectorFactory = factoryProvider . CreateIUnreachableInspectorFactory ( ) ;
137
+ _inspector = inspector ;
135
138
_parseTreeValueVisitor = parseTreeValueVisitor ;
136
139
_listener = new UnreachableCaseInspectionListener ( ) ;
137
140
}
@@ -142,38 +145,30 @@ public UnreachableCaseInspection(IDeclarationFinderProvider declarationFinderPro
142
145
143
146
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( DeclarationFinder finder )
144
147
{
145
- var selectCaseInspector = _unreachableCaseInspectorFactory . Create ( GetVariableTypeNameFunction ( finder ) ) ;
146
-
147
148
return finder . UserDeclarations ( DeclarationType . Module )
148
149
. Where ( module => module != null )
149
- . SelectMany ( module => DoGetInspectionResults ( module . QualifiedModuleName , finder , selectCaseInspector ) )
150
+ . SelectMany ( module => DoGetInspectionResults ( module . QualifiedModuleName , finder ) )
150
151
. ToList ( ) ;
151
152
}
152
153
153
154
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module , DeclarationFinder finder )
154
- {
155
- var selectCaseInspector = _unreachableCaseInspectorFactory . Create ( GetVariableTypeNameFunction ( finder ) ) ;
156
- return DoGetInspectionResults ( module , finder , selectCaseInspector ) ;
157
- }
158
-
159
- private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module , DeclarationFinder finder , IUnreachableCaseInspector selectCaseInspector )
160
155
{
161
156
var qualifiedSelectCaseStmts = _listener . Contexts ( module )
162
157
// ignore filtering here to make the search space smaller
163
158
. Where ( result => ! result . IsIgnoringInspectionResultFor ( finder , AnnotationName ) ) ;
164
159
165
160
return qualifiedSelectCaseStmts
166
- . SelectMany ( context => ResultsForContext ( context , selectCaseInspector , finder ) )
161
+ . SelectMany ( context => ResultsForContext ( context , finder ) )
167
162
. ToList ( ) ;
168
163
}
169
164
170
- private IEnumerable < IInspectionResult > ResultsForContext ( QualifiedContext < VBAParser . SelectCaseStmtContext > qualifiedSelectCaseStmt , IUnreachableCaseInspector selectCaseInspector , DeclarationFinder finder )
165
+ private IEnumerable < IInspectionResult > ResultsForContext ( QualifiedContext < VBAParser . SelectCaseStmtContext > qualifiedSelectCaseStmt , DeclarationFinder finder )
171
166
{
172
167
var module = qualifiedSelectCaseStmt . ModuleName ;
173
168
var selectStmt = qualifiedSelectCaseStmt . Context ;
174
169
var contextValues = _parseTreeValueVisitor . VisitChildren ( module , selectStmt , finder ) ;
175
170
176
- var results = selectCaseInspector . InspectForUnreachableCases ( module , selectStmt , contextValues ) ;
171
+ var results = _inspector . InspectForUnreachableCases ( module , selectStmt , contextValues , finder ) ;
177
172
178
173
return results
179
174
. Select ( resultTpl => CreateInspectionResult ( qualifiedSelectCaseStmt , resultTpl . context , resultTpl . resultType ) )
@@ -212,74 +207,6 @@ private IInspectionResult CreateInspectionResult(QualifiedContext<VBAParser.Sele
212
207
new QualifiedContext < ParserRuleContext > ( selectStmt . ModuleName , unreachableBlock ) ) ;
213
208
}
214
209
215
- private Func < QualifiedModuleName , ParserRuleContext , ( bool success , IdentifierReference reference ) > GetIdentifierReferenceForContextFunction ( DeclarationFinder finder )
216
- {
217
- return ( module , context ) => GetIdentifierReferenceForContext ( module , context , finder ) ;
218
- }
219
-
220
- //public static to support tests
221
- //FIXME There should not be additional public methods just for tests. This class seems to want to be split or at least reorganized.
222
- public static ( bool success , IdentifierReference idRef ) GetIdentifierReferenceForContext ( QualifiedModuleName module , ParserRuleContext context , DeclarationFinder finder )
223
- {
224
- if ( context == null )
225
- {
226
- return ( false , null ) ;
227
- }
228
-
229
- var qualifiedSelection = new QualifiedSelection ( module , context . GetSelection ( ) ) ;
230
-
231
- var identifierReferences =
232
- finder
233
- . IdentifierReferences ( qualifiedSelection )
234
- . Where ( reference => reference . Context == context )
235
- . ToList ( ) ;
236
-
237
- return identifierReferences . Count == 1
238
- ? ( true , identifierReferences . First ( ) )
239
- : ( false , null ) ;
240
- }
241
-
242
- private Func < string , QualifiedModuleName , ParserRuleContext , string > GetVariableTypeNameFunction ( DeclarationFinder finder )
243
- {
244
- var referenceRetriever = GetIdentifierReferenceForContextFunction ( finder ) ;
245
- return ( variableName , module , ancestor ) => GetVariableTypeName ( module , variableName , ancestor , referenceRetriever ) ;
246
- }
247
-
248
- private string GetVariableTypeName ( QualifiedModuleName module , string variableName , ParserRuleContext ancestor , Func < QualifiedModuleName , ParserRuleContext , ( bool success , IdentifierReference reference ) > referenceRetriever )
249
- {
250
- if ( ancestor == null )
251
- {
252
- return string . Empty ;
253
- }
254
-
255
- var descendents = ancestor . GetDescendents < VBAParser . SimpleNameExprContext > ( )
256
- . Where ( desc => desc . GetText ( ) . Equals ( variableName ) )
257
- . ToList ( ) ;
258
- if ( ! descendents . Any ( ) )
259
- {
260
- return string . Empty ;
261
- }
262
-
263
- var firstDescendent = descendents . First ( ) ;
264
- var ( success , reference ) = referenceRetriever ( module , firstDescendent ) ;
265
- return success ?
266
- GetBaseTypeForDeclaration ( reference . Declaration )
267
- : string . Empty ;
268
- }
269
-
270
- private string GetBaseTypeForDeclaration ( Declaration declaration )
271
- {
272
- var localDeclaration = declaration ;
273
- var iterationGuard = 0 ;
274
- while ( ! ( localDeclaration is null )
275
- && ! localDeclaration . AsTypeIsBaseType
276
- && iterationGuard ++ < 5 )
277
- {
278
- localDeclaration = localDeclaration . AsTypeDeclaration ;
279
- }
280
- return localDeclaration is null ? declaration . AsTypeName : localDeclaration . AsTypeName ;
281
- }
282
-
283
210
private class UnreachableCaseInspectionListener : InspectionListenerBase < VBAParser . SelectCaseStmtContext >
284
211
{
285
212
public override void EnterSelectCaseStmt ( [ NotNull ] VBAParser . SelectCaseStmtContext context )
0 commit comments