-
Notifications
You must be signed in to change notification settings - Fork 17
/
nitriq.nq
500 lines (448 loc) · 21.8 KB
/
nitriq.nq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?xml version="1.0"?>
<RuleSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RuleCategories>
<RuleCategory Name="Nitriq">
<RuleCategories>
<RuleCategory Name="Design Problems">
<RuleCategories />
<Rules>
<Rule Name="Methods to Refactor" Active="true">
<Code>var results =
from method in Methods
where (method.Cyclomatic > 25 || method.PhysicalLineCount > 200 ||
method.TypesUsed.Count > 30 || method.ParameterCount > 7) && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name, method.Cyclomatic,
method.PhysicalLineCount, OutTypes = method.TypesUsed.Count, method.ParameterCount };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Types To Refactor" Active="true">
<Code>var results =
from type in Types
where (type.Methods.Count > 30 || (type.Fields.Count > 15 && !type.IsEnum)) &&
type.IsInCoreAssembly
select new { type.TypeId, type.Name, type.Methods.Count };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Fields that are never set" Active="true">
<Code>var results =
from field in Fields
where field.SetByMethods.Count == 0 && field.IsPrivate && field.Type.IsInCoreAssembly
select new { field.FieldId, field.Name, field.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Methods that could be static" Active="true">
<Code>var results =
from m in Methods
where m.Type.IsInCoreAssembly == true &&
m.IsConstructor == false &&
m.IsEventAdder == false &&
m.IsEventRemover == false &&
m.IsVirtual == false &&
m.IsStatic == false &&
m.Calls.Where(methodCall => methodCall.IsStatic == false).Count() == 0 &&
m.FieldGets.Where(fieldGet => fieldGet.IsStatic == false).Count() == 0 &&
m.FieldSets.Where(fieldSet => fieldSet.IsStatic == false).Count() == 0
select new { m.MethodId, m.Name, m.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Methods that may violate SRP" Active="true">
<Code>//if a method name contains a conjunction like "And", "Or", or "Then",
//then it may be doing too many things and is violating the
//Single Responsibility Principle (SRP)
var results =
from m in Methods
where !m.IsPropertyGetter && !m.IsPropertySetter && m.IsInCoreAssembly &&
m.Name.Like(".*And[A-Z].*|.*Then[A-Z].*|.*Or[A-Z].*", false) //false => case sensitive
select new { m.MethodId, m.Name, m.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Henderson Sellers Lack of Cohesion" Active="true">
<Code>//LOC-HS as defined at:
//http://eclipse-metrics.sourceforge.net/descriptions/LackOfCohesionInMethods.html
var results =
from type in Types
let methodCount = type.Methods.Count
let instanceFields = type.Fields.Where(f => !f.IsStatic)
let fieldAccesses = instanceFields.Select(f => f.GotByMethods.Union(f.SetByMethods).Distinct()
.Where(m => m.Type == type).Count())
let accessAverage = fieldAccesses.Count() == 0 ? 0 : fieldAccesses.Average().Round(2)
let lcomHS = ((accessAverage - methodCount) / (1 - methodCount)).Round(2)
where lcomHS > .9 && instanceFields.Count() > 0 && type.IsInCoreAssembly
orderby lcomHS descending
select new { type.TypeId, type.Name, lcomHS, methodCount, fieldCount = instanceFields.Count(), accessAverage, type.FullName };
Warn(results, 0);</Code>
</Rule>
</Rules>
</RuleCategory>
<RuleCategory Name="Informational">
<RuleCategories />
<Rules>
<Rule Name="Recursive Methods" Active="true">
<Code>var results =
from m in Methods
where m.Calls.Contains(m)
select new { m.MethodId, m.Name, m.FullName };
//Recursive methods aren't necessarily a bad thing, so there is no warning
//its just a nice thing to be aware of how many and where these methods are</Code>
</Rule>
<Rule Name="Methods that take or return System.Object" Active="true">
<Code>var objectType = Types.Where(t => t.FullName == "System.Object").Single();
var results =
from m in Methods
let TakeObjectParam = m.ParameterTypes.Contains(objectType)
let ReturnsObject = m.ReturnType == objectType
where m.IsInCoreAssembly && (TakeObjectParam || ReturnsObject)
select new { m.MethodId, m.Name, m.FullName, TakeObjectParam, ReturnsObject };
//There are a lot of methods that force you to accept a parameter of type object
//particularly event handlers, so no warning. But if you see a lot of these
//in your non-ui layers, you may want to check it out</Code>
</Rule>
<Rule Name="Static methods that instantiate objects" Active="true">
<Code>var results =
from m in Methods
let ConstructorCalls = m.Calls.Where(callMethod => callMethod.IsConstructor).Count()
where m.IsStatic && !m.IsConstructor && ConstructorCalls > 0
select new { m.MethodId, m.Name, m.FullName, ConstructorCalls };
//Your design could be more loosely coupled if classes being instantiated were
//instead passed in as a parameter</Code>
</Rule>
<Rule Name="Assembly Breakout Info" Active="true">
<Code>var results =
from a in Assemblies
orderby a.IsCoreAssembly descending, a.Name, a.Version
select new { a.AssemblyId, IsCore = a.IsCoreAssembly, a.Name, a.Version,
Types = Types.Where(t => t.Assembly == a).Count(),
Methods = Methods.Where(m => m.Type.Assembly == a).Count(),
};
//the code tree won't display assembly results unless the "Top Level" is
//set to "Assembly", for best results, view the results in the "grid" tab.</Code>
</Rule>
<Rule Name="All Properties" Active="true">
<Code>var results =
from m in Methods
where m.IsPropertyGetter || m.IsPropertySetter
select new { m.MethodId, m.Name, m.FullName };</Code>
</Rule>
</Rules>
</RuleCategory>
</RuleCategories>
<Rules />
</RuleCategory>
<RuleCategory Name="NX Cop">
<RuleCategories>
<RuleCategory Name="Design">
<RuleCategories />
<Rules>
<Rule Name="Abstract types should not have constructors" Active="true">
<Code>var results =
from type in Types
let ConstructorCount = type.Methods.Where(m => m.IsConstructor && (m.IsPublic || m.IsInternal)).Count()
where type.IsAbstract && ConstructorCount > 0 && type.IsInCoreAssembly
select new { type.TypeId, type.Name, ConstructorCount } ;
Warn(results, 0);</Code>
</Rule>
<Rule Name="Avoid empty interfaces" Active="true">
<Code>var results =
from t in Types
let MethodCount = t.Methods.Count()
let EventCount = t.Events.Count()
let FieldCount = t.Fields.Count()
let InterfacesCount = t.Interfaces.Count()
where t.IsInCoreAssembly && t.IsInterface
&& MethodCount == 0 && EventCount == 0 && FieldCount == 0 && InterfacesCount < 2
select new { t.TypeId, t.Name, t.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Do not declare protected members in sealed types" Active="true">
<Code>var results =
from type in Types
let protectedMethodCount = type.Methods.Where(m => m.IsProtected).Count()
let protectedFieldCount = type.Fields.Where(f => f.IsProtected).Count()
let protectedEventCount = type.Events.Where(e => e.IsProtected).Count()
where type.IsSealed && (protectedMethodCount > 0 || protectedFieldCount > 0
|| protectedEventCount > 0) && type.IsInCoreAssembly
select new { type.TypeId, type.Name, type.FullName, protectedMethodCount, protectedFieldCount, protectedEventCount };
Warn(results, 0);
</Code>
</Rule>
<Rule Name="Do not declare static members on generic types" Active="true">
<Code>var results =
from type in Types
from method in type.Methods
from field in type.Fields
from ev in type.Events
where type.GenericParameterCount > 0 && (method.IsStatic || field.IsStatic || ev.IsStatic)
&& type.IsInCoreAssembly
select new {
type.TypeId,
type.Name,
type.FullName,
MethodName = method.Name,
FieldName = field.Name,
EventName = ev.Name
};
Warn(results, 0);</Code>
</Rule>
<Rule Name="Do not declare virtual members in sealed types" Active="true">
<Code>var results =
from type in Types
from method in type.Methods
where type.IsSealed && method.IsVirtual && type.IsInCoreAssembly
select new { method.MethodId, MethodName = method.Name, TypeName = type.Name,
TypeFullName = type.FullName };
Warn(results, 0);
</Code>
</Rule>
<Rule Name="Do not declare visible instance fields" Active="true">
<Code>var results =
from field in Fields
where !field.IsStatic && (field.IsPublic || field.IsProtectedOrInternal || field.IsProtected)
&& field.Type.IsInCoreAssembly
select new { field.FieldId, field.Name, Type = field.Type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Do not expose generic lists" Active="true">
<Code>var listOfT = Types.FullNameIs("System.Collections.Generic.List`1").FirstOrDefault();
var results =
from type in Types
from method in type.Methods
where (method.ReturnType == listOfT || method.ParameterTypes.Any(t => t == listOfT)) &&
!method.IsPrivate && !method.IsProtected && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name, Type = method.Type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Enum storage should be Int32" Active="true">
<Code>var int32Type = Types.FullNameIs("System.Int32").First();
var results =
from type in Types
let valueType = type.Fields.NameIs("value__").FirstOrDefault()
where type.IsEnum && valueType != null && valueType.FieldType != int32Type && type.IsInCoreAssembly
select new { type.TypeId, type.Name, type.FullName, BaseType = valueType.FieldType.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Exceptions should be public" Active="true">
<Code>var results =
from exceptableBaseException in Types
from type in exceptableBaseException.DerivedTypes
where (exceptableBaseException.FullName == "System.Exception" ||
exceptableBaseException.FullName == "System.SystemException" ||
exceptableBaseException.FullName == "System.ApplicationException")
&& !type.IsPublic && type.IsInCoreAssembly
select new { type.TypeId, type.Name, type.FullName,
type.IsInternal, type.IsProtected,
type.IsProtectedAndInternal, type.IsPrivate };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Indexers should not be multidimensional" Active="true">
<Code>var results =
from method in Methods
where ((method.IsIndexGetter && method.ParameterCount > 1) ||
(method.IsIndexSetter && method.ParameterCount > 2)) && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name, Type = method.Type.FullName, method.ParameterCount};
Warn(results, 0);
//the last parameter on an index setter is the value, we don't want to count
//that for whether or not it is multi dimensional
</Code>
</Rule>
<Rule Name="Nested types should not be visible" Active="true">
<Code>var iEnumerator = Types.FullNameIs("System.Collections.IEnumerator").FirstOrDefault();
var results =
from type in Types
where type.IsNested && type.IsInCoreAssembly &&
type.IsPublic && type.Interfaces.Contains(iEnumerator)
select new { type.TypeId, type.Name, type.FullName };
Warn(results, 0);
//enumerators are exempt from this rule</Code>
</Rule>
<Rule Name="Properties should not be write only" Active="true">
<Code>var results =
from type in Types
from method in type.Methods
where method.IsPropertySetter &&
type.Methods.NameIs("get_" + method.Name.Substring(4)).FirstOrDefault() == null &&
method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name, method.Type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Static holder types should not have constructors" Active="true">
<Code>var results =
from type in Types
where !type.IsSealed &&
type.Methods.Where(method => method.IsConstructor && method.ParameterCount != 0).Count() > 0 &&
type.Methods.Where(method => !method.IsStatic).Count() == 0 &&
type.Fields.Where(field => !field.IsStatic).Count() == 0 &&
type.Events.Where(ev => !ev.IsStatic).Count() == 0 &&
type.IsInCoreAssembly
select new { type.TypeId, type.Name, type.FullName } ;
Warn(results, 0);
</Code>
</Rule>
<Rule Name="Types that own disposable fields should be disposable" Active="true">
<Code>var iDisposable = Types.FullNameIs("System.IDisposable").FirstOrDefault();
var results =
from type in Types
from field in type.Fields
where !type.Interfaces.Contains(iDisposable) && type.IsInCoreAssembly &&
field.FieldType != null && field.FieldType.Interfaces.Contains(iDisposable)
select new { field.FieldId, field.Name, Type = type.FullName } ;
Warn(results.Count() > 0, "Types that own disposable fields should be disposable");</Code>
</Rule>
<Rule Name="URI properties should not be strings" Active="true">
<Code>var stringType = Types.FullNameIs("System.String").FirstOrDefault();
var results =
from method in Methods
where (method.IsPropertyGetter || method.IsPropertySetter) &&
(method.Name.Like("uri") || method.Name.Like("urn") || method.Name.Like("url")) &&
method.ReturnType == stringType && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name };
Warn(results, 0);</Code>
</Rule>
<Rule Name="URI return values should not be string" Active="true">
<Code>var stringType = Types.FullNameIs("System.String").FirstOrDefault();
var results =
from method in Methods
where (!method.IsPropertyGetter && !method.IsPropertySetter) &&
(method.Name.Like("uri") || method.Name.Like("urn") || method.Name.Like("url")) &&
method.ReturnType == stringType && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Use events where appropriate" Active="true">
<Code>var results =
from method in Methods
where (method.IsPublic || method.IsProtected || method.IsPrivate) &&
(method.Name.StartsWith("AddOn") || method.Name.StartsWith("RemoveOn") ||
method.Name.StartsWith("Fire") || method.Name.StartsWith("Raise")) && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name };
Warn(results, 0);</Code>
</Rule>
</Rules>
</RuleCategory>
<RuleCategory Name="Maintainability">
<RuleCategories />
<Rules>
<Rule Name="Avoid excessive class coupling" Active="true">
<Code>//We're not exactly sure at what point the actual FxCop engine cries foul
//about this rule, so we guessed 75.
var results =
from type in Types
where type.TypesUsed.Count > 75 && type.IsInCoreAssembly
select new { type.TypeId, type.Name, In = type.TypesUsing.Count, Out = type.TypesUsed.Count };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Avoid excessive complexity" Active="true">
<Code>var results =
from method in Methods
where method.Cyclomatic > 25 && method.Type.IsInCoreAssembly
select new { method.MethodId, method.Name, Type = method.Type.Name, method.Cyclomatic };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Avoid excessive inheritance" Active="true">
<Code>var results =
from type in Types
where type.InheritanceDepth > 5
select new { type.TypeId, type.Name, type.InheritanceDepth };</Code>
</Rule>
<Rule Name="Review misleading field names" Active="true">
<Code>var results =
from field in Fields
where ((field.Name.StartsWith("s_") && !field.IsStatic) ||
(field.Name.StartsWith("m_") && field.IsStatic)) && field.Type.IsInCoreAssembly
select new { field.FieldId, field.Name, Type = field.Type.FullName };
Warn(results, 0);
</Code>
</Rule>
</Rules>
</RuleCategory>
<RuleCategory Name="Naming">
<RuleCategories />
<Rules>
<Rule Name="Do not name enum values 'Reserved'" Active="true">
<Code>var results =
from field in Fields
where field.Type.IsEnum && (field.Name == "Reserved" || field.Name == "reserved")
&& field.Type.IsInCoreAssembly
select new { field.FieldId, field.Name, Type = field.Type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Do not prefix enum values with type name" Active="true">
<Code>var results =
from field in Fields
where field.Type.IsEnum && (field.Name.StartsWith(field.Type.Name)) && field.Type.IsInCoreAssembly
select new { field.FieldId, field.Name, Type = field.Type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Events should not have before or after prefix" Active="true">
<Code>var results =
from myEvent in Events
where (myEvent.Name.StartsWith("Before") || myEvent.Name.StartsWith("After")) && myEvent.Type.IsInCoreAssembly
select new { myEvent.EventId, myEvent.Name, Type = myEvent.Type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Identifiers should have correct prefix" Active="true">
<Code>//The full version of this FxCop rule requires Type Parameters
//on Generic classes to begin with "T", but Nitriq doesn't currently
//support querying Type Parameters
var results =
from type in Types
where type.IsInterface && !type.Name.StartsWith("I") && type.IsInCoreAssembly
select new { type.TypeId, type.Name, type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Identifiers should have correct suffix" Active="true">
<Code>// use FirstOrDefault because if these types are never references, the Types collection
// won't have a record for them.
var attribute = Types.FullNameIs("System.Attribute").FirstOrDefault();
var eventArgs = Types.FullNameIs("System.EventArgs").FirstOrDefault();
var exception = Types.FullNameIs("System.Exception").FirstOrDefault();
var iCollection = Types.FullNameIs("System.Collections.ICollection").FirstOrDefault();
var iDictionary = Types.FullNameIs("System.Collections.IDictionary").FirstOrDefault();
var iEnumerable = Types.FullNameIs("System.Collections.IEnumerable").FirstOrDefault();
var queue = Types.FullNameIs("System.Collections.Queue").FirstOrDefault();
var stack = Types.FullNameIs("System.Collections.Stack").FirstOrDefault();
var iCollectionT = Types.FullNameIs("System.Collections.ICollection`1").FirstOrDefault();
var iDictionaryKV = Types.FullNameIs("System.Collections.IDictionary`2").FirstOrDefault();
var dataSet = Types.FullNameIs("System.Data.DataSet").FirstOrDefault();
var dataTable = Types.FullNameIs("System.Data.DataTable").FirstOrDefault();
var stream = Types.FullNameIs("System.IO.Stream").FirstOrDefault();
var permission = Types.FullNameIs("System.Security.Permission").FirstOrDefault();
var membershipCondition = Types.FullNameIs("System.Security.Policy.IMembershipCondition").FirstOrDefault();
var results =
from type in Types
let typeName = System.Text.RegularExpressions.Regex.Replace(type.Name, @"`\d+", "")
where
type.IsInCoreAssembly &&
type.BaseType != null && (
(type.BaseType == attribute && !typeName.EndsWith("Attribute")) ||
(type.BaseType == eventArgs && !typeName.EndsWith("EventArgs")) ||
(type.BaseType == exception && !typeName.EndsWith("Exception")) ||
(type.BaseType == iCollection && !typeName.EndsWith("Collection")) ||
(type.BaseType == iDictionary && !typeName.EndsWith("Dictionary")) ||
(type.BaseType == iEnumerable && !typeName.EndsWith("Collection")) ||
(type.BaseType == queue && !typeName.EndsWith("Collection") && !typeName.EndsWith("Queue")) ||
(type.BaseType == stack && !typeName.EndsWith("Collection") && !typeName.EndsWith("Stack")) ||
(type.BaseType == iCollectionT && !typeName.EndsWith("Collection")) ||
(type.BaseType == iDictionaryKV && !typeName.EndsWith("Dictionary")) ||
(type.BaseType == dataSet && !typeName.EndsWith("DataSet")) ||
(type.BaseType == dataTable && !typeName.EndsWith("Collection") && !typeName.EndsWith("DataTable")) ||
(type.BaseType == stream && !typeName.EndsWith("Stream")) ||
(type.BaseType == permission && !typeName.EndsWith("Permission")) ||
(type.BaseType == membershipCondition && !typeName.EndsWith("Condition"))
)
select new { type.TypeId, type.Name, typeName, type.FullName };
Warn(results, 0);</Code>
</Rule>
<Rule Name="Identifiers should not contain underscores (Namespaces)" Active="true">
<Code>var results =
from myNamespace in Namespaces
where myNamespace.FullName.Contains("_")
select new { myNamespace.NamespaceId, myNamespace.FullName };
Warn(results, 0);</Code>
</Rule>
</Rules>
</RuleCategory>
</RuleCategories>
<Rules />
</RuleCategory>
</RuleCategories>
</RuleSet>