public RuleResult CheckMethod (MethodDefinition method) { //does not apply if method has no parameter, is a property, or a p/invoke if (!method.HasParameters || method.IsProperty () || method.IsPInvokeImpl) return RuleResult.DoesNotApply; //if this is a constructor or override, the method name is dependent if (method.IsConstructor || method.IsOverride ()) return RuleResult.DoesNotApply; ParameterDefinition p0 = method.Parameters [0]; string name = p0.ParameterType.Name; //param is out/ref, it is already not obvious (there is a rule for that) if (p0.IsOut || p0.IsRef ()) return RuleResult.DoesNotApply; string method_name = method.Name; if (name.Length == 1 || method_name.Length <= name.Length) return RuleResult.DoesNotApply; if ((method_name.Length - name.Length) < 4 && IsVaguePrefix (method_name)) //suggestion would be too vague anyway (Get/Set/Is) return RuleResult.DoesNotApply; if (!char.IsUpper (name [0])) //non-compliant naming, cannot go further (PascalWords needed) return RuleResult.DoesNotApply; //if the method return the parameter type it is most likely clearer to have it in the name if (method.ReturnType == p0.ParameterType) return RuleResult.Success; //if starting with name it is most likely on purpose if (method_name.StartsWith (name, StringComparison.Ordinal)) return RuleResult.Success; int pos = method_name.LastIndexOf (name); if (-1 == pos) return RuleResult.Success; Confidence confidence = Confidence.Normal; if (pos >= method_name.Length - name.Length) //suffix, most common and most verbose case confidence = Confidence.High; else if (!char.IsUpper (method_name [pos + name.Length])) //not the end of a 'PascalWord' return RuleResult.Success; //if IgnoreAlienNamespaces is True, then check if parameter type is from one of the analyzed namespaces if (IgnoreAlienNamespaces && IsTypeFromAlienNamespace (p0.ParameterType)) return RuleResult.Success; //ignored/out-of-reach, so this is a success //main goal is to keep the API as simple as possible so this is more severe for visible methods Severity severity = method.IsVisible () ? Severity.Medium : Severity.Low; string suggestion = GetSuggestionMethodName (method, name, pos); string msg; if (method.IsStatic) { //we already have a rule that checks if the method should be static string memberKind = GetSuggestionMemberKind (method); msg = String.Format ("Consider renaming method to '{2}', or extracting method to type '{0}' as {1} '{2}', or making an extension method of that type.", p0.ParameterType, memberKind, suggestion); } else { msg = String.Format ("Consider renaming method to '{0}'.", suggestion); } Runner.Report (method, severity, confidence, msg); return RuleResult.Failure; }