Skip to content

Commit

Permalink
fix(#357): add two more puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 18, 2024
1 parent 910c225 commit 4508334
Showing 1 changed file with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithBody;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.Statement;
import java.util.ArrayList;
Expand Down Expand Up @@ -99,7 +97,11 @@ Stream<MethodCallExpr> statements() {
* we simply unroll all these statements and return them as a flat stream.
* @param stmts Statements to unroll.
* @return Stream of statements.
* @todo #357:90min Refactor 'flatStatements' method to make it more readable.
* The method 'flatStatements' is too complex and hard to read. It should be refactored
* to make it more readable and maintainable.
*/
@SuppressWarnings("PMD.CognitiveComplexity")
private static Stream<Statement> flatStatements(final Stream<? extends Statement> stmts) {
return stmts.flatMap(
statement -> {
Expand All @@ -122,7 +124,8 @@ private static Stream<Statement> flatStatements(final Stream<? extends Statement
ifstmt.getElseStmt()
.orElseThrow(
() -> new IllegalStateException(
"Else block is absent. It's impossible")
"Else block is absent. It's impossible"
)
)
);
}
Expand All @@ -146,32 +149,42 @@ private static Stream<Statement> flatStatements(final Stream<? extends Statement
);
}

static Stream<Statement> flatStatements(final Expression expression) {
List<Statement> statements = new ArrayList<>(0);
List<Expression> expressions = new ArrayList<>(0);
List<VariableDeclarator> declarators = new ArrayList<>(0);
/**
* This method extracts statements from Expression.
* @param expression Expression to extract statements from.
* @return Stream of statements.
* @todo #357:90min Refactor 'flatStatements' method to make it more readable.
* The method 'flatStatements' is too complex and hard to read. It should be refactored
* to make it more readable and maintainable. The method should be split into smaller
* methods with clear responsibilities.
*/
@SuppressWarnings("PMD.CognitiveComplexity")
private static Stream<Statement> flatStatements(final Expression expression) {
final Stream<Statement> result;
if (expression.isLambdaExpr()) {
return JavaParserMethod.flatStatements(Stream.of(expression.asLambdaExpr().getBody()));
}
for (final Node node : expression.getChildNodes()) {
if (node instanceof Statement) {
statements.add((Statement) node);
} else if (node instanceof VariableDeclarator) {
declarators.add((VariableDeclarator) node);
result = JavaParserMethod.flatStatements(
Stream.of(expression.asLambdaExpr().getBody())
);
} else {
final List<Statement> statements = new ArrayList<>(0);
final List<VariableDeclarator> declarators = new ArrayList<>(0);
for (final Node node : expression.getChildNodes()) {
if (node instanceof Statement) {
statements.add((Statement) node);
} else if (node instanceof VariableDeclarator) {
declarators.add((VariableDeclarator) node);
}
}
}
final Stream<Statement> a = declarators.stream()
.map(VariableDeclarator::getInitializer)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(JavaParserMethod::flatStatements);
return Stream.concat(
Stream.concat(
a,
result = Stream.concat(
declarators.stream()
.map(VariableDeclarator::getInitializer)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(JavaParserMethod::flatStatements),
statements.stream()
),
expressions.stream().flatMap(JavaParserMethod::flatStatements)
);
);
}
return result;
}

/**
Expand Down

2 comments on commit 4508334

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 4508334 Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 357-a463583f discovered in src/main/java/com/github/lombrozo/testnames/javaparser/JavaParserMethod.java) and submitted as #359. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 4508334 Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 357-4ec3ee26 discovered in src/main/java/com/github/lombrozo/testnames/javaparser/JavaParserMethod.java) and submitted as #360. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.