Skip to content

Commit e21a5ad

Browse files
committed
Implemented as abstract factory pattern per comments
1 parent 69ddd7f commit e21a5ad

12 files changed

+108
-105
lines changed

RetailCoder.VBE/Inspections/AssignedByValParameterInspection.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
using Rubberduck.Parsing.Grammar;
77
using Rubberduck.Parsing.Symbols;
88
using Rubberduck.Parsing.VBA;
9+
using Rubberduck.UI.Refactorings;
910

1011
namespace Rubberduck.Inspections
1112
{
1213
public sealed class AssignedByValParameterInspection : InspectionBase
1314
{
14-
public AssignedByValParameterInspection(RubberduckParserState state)
15+
private IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
16+
public AssignedByValParameterInspection(RubberduckParserState state, IAssignedByValParameterQuickFixDialogFactory dialogFactory)
1517
: base(state)
1618
{
1719
Severity = DefaultSeverity;
20+
_dialogFactory = dialogFactory;
21+
1822
}
1923

2024
public override string Meta { get { return InspectionsUI.AssignedByValParameterInspectionMeta; } }
@@ -31,7 +35,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
3135
.ToList();
3236

3337
return parameters
34-
.Select(param => new AssignedByValParameterInspectionResult(this, param))
38+
.Select(param => new AssignedByValParameterInspectionResult(this, param, _dialogFactory))
3539
.ToList();
3640
}
3741
}

RetailCoder.VBE/Inspections/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFix.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ namespace Rubberduck.Inspections.QuickFixes
1717
public class AssignedByValParameterMakeLocalCopyQuickFix : QuickFixBase
1818
{
1919
private readonly Declaration _target;
20+
private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
2021
private string _localCopyVariableName;
2122
private string[] _variableNamesAccessibleToProcedureContext;
22-
private IAssignedByValParameterQuickFixDialogFactory _factory;
2323

24-
public AssignedByValParameterMakeLocalCopyQuickFix(Declaration target, QualifiedSelection selection, IAssignedByValParameterQuickFixDialogFactory factory)
24+
public AssignedByValParameterMakeLocalCopyQuickFix(Declaration target, QualifiedSelection selection, IAssignedByValParameterQuickFixDialogFactory dialogFactory)
2525
: base(target.Context, selection, InspectionsUI.AssignedByValParameterMakeLocalCopyQuickFix)
2626
{
2727
_target = target;
28-
_factory = factory;
28+
_dialogFactory = dialogFactory;
2929
_localCopyVariableName = "x" + _target.IdentifierName.CapitalizeFirstLetter();
3030
_variableNamesAccessibleToProcedureContext = GetVariableNamesAccessibleToProcedureContext(_target.Context.Parent.Parent);
3131
}
@@ -49,7 +49,7 @@ public override void Fix()
4949

5050
private void RequestLocalCopyVariableName()
5151
{
52-
using( var view = _factory.Create(_target.IdentifierName, _target.DeclarationType.ToString()))
52+
using( var view = _dialogFactory.Create(_target.IdentifierName, _target.DeclarationType.ToString()))
5353
{
5454
view.NewName = _localCopyVariableName;
5555
view.IdentifierNamesAlreadyDeclared = _variableNamesAccessibleToProcedureContext;

RetailCoder.VBE/Inspections/Results/AssignedByValParameterInspectionResult.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ namespace Rubberduck.Inspections.Results
1111
public class AssignedByValParameterInspectionResult : InspectionResultBase
1212
{
1313
private IEnumerable<QuickFixBase> _quickFixes;
14+
private IAssignedByValParameterQuickFixDialogFactory _dialogFactory;
1415

15-
public AssignedByValParameterInspectionResult(IInspection inspection, Declaration target) : base(inspection, target) { }
16+
public AssignedByValParameterInspectionResult(IInspection inspection, Declaration target, IAssignedByValParameterQuickFixDialogFactory dialogFactory)
17+
: base(inspection, target)
18+
{
19+
_dialogFactory = dialogFactory;
20+
}
1621

1722
public override string Description
1823
{
@@ -26,10 +31,9 @@ public override IEnumerable<QuickFixBase> QuickFixes
2631
{
2732
get
2833
{
29-
IAssignedByValParameterQuickFixDialogFactory factory = new AssignedByValParameterQuickFixDialogFactory();
3034
return _quickFixes ?? (_quickFixes = new QuickFixBase[]
3135
{
32-
new AssignedByValParameterMakeLocalCopyQuickFix(Target, QualifiedSelection, factory),
36+
new AssignedByValParameterMakeLocalCopyQuickFix(Target, QualifiedSelection, _dialogFactory),
3337
new PassParameterByReferenceQuickFix(Target, QualifiedSelection),
3438
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName)
3539
});

RetailCoder.VBE/Root/RubberduckModule.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
3737
using Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract;
3838
using ReparseCommandMenuItem = Rubberduck.UI.Command.MenuItems.CommandBars.ReparseCommandMenuItem;
39+
using Rubberduck.UI.Refactorings;
40+
using Rubberduck.Inspections;
3941

4042
namespace Rubberduck.Root
4143
{
@@ -69,6 +71,7 @@ public override void Load()
6971
Bind<IOperatingSystem>().To<WindowsOperatingSystem>().InSingletonScope();
7072

7173
Bind<CommandBase>().To<VersionCheckCommand>().WhenInjectedExactlyInto<App>();
74+
7275
BindCodeInspectionTypes();
7376

7477
var assemblies = new[]
@@ -146,6 +149,7 @@ public override void Load()
146149
ConfigureProjectExplorerContextMenu();
147150

148151
BindWindowsHooks();
152+
149153
}
150154

151155
private void BindDockableToolwindows()
@@ -425,6 +429,10 @@ private IEnumerable<ICommandMenuItem> GetRubberduckCommandBarItems()
425429

426430
private IEnumerable<IMenuItem> GetRubberduckMenuItems()
427431
{
432+
//This bind needs to occur before the the new array is built by the function calls
433+
Bind<IAssignedByValParameterQuickFixDialogFactory>().To<AssignedByValParameterQuickFixDialogFactory>()
434+
.WhenInjectedInto<AssignedByValParameterInspection>();
435+
428436
return new[]
429437
{
430438
KernelInstance.Get<AboutCommandMenuItem>(),

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,10 @@
471471
<Compile Include="UI\CodeExplorer\Commands\AddTestModuleCommand.cs" />
472472
<Compile Include="UI\ModernFolderBrowser.cs" />
473473
<Compile Include="UI\Refactorings\AssignedByValParameterQuickFixDialogFactory.cs" />
474+
<Compile Include="UI\Refactorings\AssignedByValParameterQuickFixMockDialogFactory.cs" />
474475
<Compile Include="UI\Refactorings\IAssignedByValParameterQuickFixDialog.cs" />
475476
<Compile Include="UI\Refactorings\IAssignedByValParameterQuickFixDialogFactory.cs" />
477+
<Compile Include="UI\Refactorings\AssignedByValParameterQuickFixMockDialog.cs" />
476478
<Compile Include="VersionCheck\IVersionCheck.cs" />
477479
<Compile Include="UI\Command\MenuItems\CommandBars\AppCommandBarBase.cs" />
478480
<Compile Include="UI\Command\MenuItems\CommandBars\ContextSelectionLabelMenuItem.cs" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Windows.Forms;
2+
3+
namespace Rubberduck.UI.Refactorings
4+
{
5+
public class AssignedByValParameterQuickFixMockDialog : IAssignedByValParameterQuickFixDialog
6+
{
7+
private string _testLocalVariableName;
8+
internal AssignedByValParameterQuickFixMockDialog(string testLocalVariableName = "")
9+
{
10+
_testLocalVariableName = testLocalVariableName;
11+
}
12+
public DialogResult ShowDialog() { return DialogResult.OK; }
13+
14+
public void Dispose()
15+
{
16+
}
17+
public DialogResult DialogResult { set; get; }
18+
private string _newName;
19+
public string NewName
20+
{
21+
get
22+
{
23+
if (_testLocalVariableName.Length > 0)
24+
{
25+
return _testLocalVariableName;
26+
}
27+
else
28+
{
29+
return _newName;
30+
}
31+
}
32+
set { _newName = value; }
33+
}
34+
public string[] IdentifierNamesAlreadyDeclared { get; set; }
35+
}
36+
}

RetailCoder.VBE/UI/Refactorings/AssignedByValParameterQuickFixDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class AssignedByValParameterQuickFixDialog : Form, IAssignedByVal
1010
private string[] _identifierNamesAlreadyDeclared;
1111
private string _identifierName;
1212

13-
public AssignedByValParameterQuickFixDialog(string identifierName, string declarationType)
13+
internal AssignedByValParameterQuickFixDialog(string identifierName, string declarationType)
1414
{
1515
InitializeComponent();
1616
InitializeCaptions(identifierName, declarationType);

RetailCoder.VBE/UI/Refactorings/AssignedByValParameterQuickFixDialogFactory.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Rubberduck.Parsing.Symbols;
7-
using Rubberduck.VBEditor;
8-
1+

92
namespace Rubberduck.UI.Refactorings
103
{
114
public class AssignedByValParameterQuickFixDialogFactory : IAssignedByValParameterQuickFixDialogFactory
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
namespace Rubberduck.UI.Refactorings
3+
{
4+
public class AssignedByValParameterQuickFixMockDialogFactory : IAssignedByValParameterQuickFixDialogFactory
5+
{
6+
private string _userEnteredVariableName;
7+
public AssignedByValParameterQuickFixMockDialogFactory()
8+
{
9+
_userEnteredVariableName = string.Empty;
10+
}
11+
public AssignedByValParameterQuickFixMockDialogFactory(string userEnteredVariableName)
12+
{
13+
_userEnteredVariableName = userEnteredVariableName;
14+
}
15+
IAssignedByValParameterQuickFixDialog IAssignedByValParameterQuickFixDialogFactory.Create(string identifier, string identifierType)
16+
{
17+
return new AssignedByValParameterQuickFixMockDialog(_userEnteredVariableName);
18+
}
19+
}
20+
}

RubberduckTests/Inspections/AssignedByValParameterInspectionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Public Sub Foo(ByVal arg1 As String)
145145
[TestCategory("Inspections")]
146146
public void InspectionType()
147147
{
148-
var inspection = new AssignedByValParameterInspection(null);
148+
var inspection = new AssignedByValParameterInspection(null,null);
149149
Assert.AreEqual(CodeInspectionType.CodeQualityIssues, inspection.InspectionType);
150150
}
151151

@@ -154,7 +154,7 @@ public void InspectionType()
154154
public void InspectionName()
155155
{
156156
const string inspectionName = "AssignedByValParameterInspection";
157-
var inspection = new AssignedByValParameterInspection(null);
157+
var inspection = new AssignedByValParameterInspection(null,null);
158158

159159
Assert.AreEqual(inspectionName, inspection.Name);
160160
}
@@ -194,7 +194,7 @@ private string GetModuleContent(Mock<IVBE> vbe)
194194
parser.Parse(new CancellationTokenSource());
195195
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
196196

197-
var inspection = new AssignedByValParameterInspection(parser.State);
197+
var inspection = new AssignedByValParameterInspection(parser.State,null);
198198
return inspection.GetInspectionResults();
199199
}
200200

0 commit comments

Comments
 (0)