private static SolutionAnalysis Analyze(LeapSolution leapSolution) { if (leapSolution.UsesTooManyChecks()) { return(leapSolution.DisapproveWithComment(LeapComments.UseMinimumNumberOfChecks)); } if (leapSolution.ReturnsMinimumNumberOfChecksInSingleExpression()) { return(leapSolution.UsesExpressionBody() ? leapSolution.ApproveAsOptimal() : leapSolution.ApproveWithComment(SharedComments.UseExpressionBodiedMember)); } return(leapSolution.ReferToMentor()); }
private static bool BinaryExpressionUsesYearParameter(this LeapSolution leapSolution, BinaryExpressionSyntax binaryExpression) => binaryExpression.Left.IsEquivalentWhenNormalized( LeapParameterIdentifierName(leapSolution)) || binaryExpression.Right.IsEquivalentWhenNormalized( LeapParameterIdentifierName(leapSolution));
public static bool UsesTooManyChecks(this LeapSolution leapSolution) => leapSolution.IsLeapYearMethod .DescendantNodes() .OfType <BinaryExpressionSyntax>() .Count(leapSolution.BinaryExpressionUsesYearParameter) > MinimalNumberOfChecks;
public static bool UsesExpressionBody(this LeapSolution leapSolution) => leapSolution.IsLeapYearMethod.IsExpressionBody();
public static bool ReturnsMinimumNumberOfChecksInSingleExpression(this LeapSolution leapSolution) => leapSolution.Returns(LeapMinimumNumberOfChecksWithoutParenthesesBinaryExpression(leapSolution)) || leapSolution.Returns(LeapMinimumNumberOfChecksWithParenthesesBinaryExpression(leapSolution));
private static BinaryExpressionSyntax LeapModuloExpression(LeapSolution leapSolution, int number) => ModuloExpression( LeapParameterIdentifierName(leapSolution), NumericLiteralExpression(number));
public static IdentifierNameSyntax LeapParameterIdentifierName(LeapSolution leapSolution) => IdentifierName(leapSolution.YearParameter.Identifier);
public static BinaryExpressionSyntax LeapMinimumNumberOfChecksWithParenthesesBinaryExpression(LeapSolution leapSolution) => LogicalAndExpression( EqualsExpression( LeapModuloExpression(leapSolution, 4), NumericLiteralExpression(0)), ParenthesizedExpression( LogicalOrExpression( NotEqualsExpression( LeapModuloExpression(leapSolution, 100), NumericLiteralExpression(0)), EqualsExpression( LeapModuloExpression(leapSolution, 400), NumericLiteralExpression(0)))));
private static BinaryExpressionSyntax LeapYearModuloExpression(LeapSolution solution, int year) => ModuloExpression( LeapParameterIdentifierName(solution), NumericLiteralExpression(year));
private static BinaryExpressionSyntax DivisibleByFourHundred(LeapSolution solution) => EqualsExpression( LeapYearModuloExpression(solution, 400), NumericLiteralExpression(0));
public static BinaryExpressionSyntax LeapMinimumNumberOfChecksWithParenthesesBinaryExpression(LeapSolution solution) => LogicalAndExpression( DivisibleByFour(solution), ParenthesizedExpression( LogicalOrExpression( NotDivisibleByHundred(solution), DivisibleByFourHundred(solution))));
public static IdentifierNameSyntax LeapParameterIdentifierName(LeapSolution solution) => IdentifierName(solution.YearParameterName);