File SwiftParser.java
has 647 lines of code (exceeds 250 allowed). Consider refactoring. Open
/*
* Copyright 2006-2023 Prowide
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Method readUntilBlockEnds
has a Cognitive Complexity of 44 (exceeds 8 allowed). Consider refactoring. Open
protected String readUntilBlockEnds() throws IOException {
final int start = buffer == null ? 0 : buffer.length();
int len = 0;
int c;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method consumeBlock4
has a Cognitive Complexity of 42 (exceeds 8 allowed). Consider refactoring. Open
protected SwiftBlock4 consumeBlock4(final SwiftBlock4 b, final String s) {
/*
* Note that if the block4 is a text block last character is -, which is part of the EOB
* since the parser removes the last }
*/
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
SwiftParser
has 38 methods (exceeds 20 allowed). Consider refactoring. Open
public class SwiftParser {
private static final transient java.util.logging.Logger log =
java.util.logging.Logger.getLogger(SwiftParser.class.getName());
/**
Method findEndOfTagByLineFeed
has a Cognitive Complexity of 28 (exceeds 8 allowed). Consider refactoring. Open
protected int findEndOfTagByLineFeed(final String s, int start, final boolean isTextBlock) {
int i = start;
// start scanning for tag end
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method consumeBlock
has a Cognitive Complexity of 24 (exceeds 8 allowed). Consider refactoring. Open
protected SwiftBlock consumeBlock(final UnparsedTextList unparsedReceiver) throws IOException {
// search for block start
final String unparsed = findBlockStart();
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method consumeBlock4
has 70 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected SwiftBlock4 consumeBlock4(final SwiftBlock4 b, final String s) {
/*
* Note that if the block4 is a text block last character is -, which is part of the EOB
* since the parser removes the last }
*/
Method consumeTagListBlock
has a Cognitive Complexity of 22 (exceeds 8 allowed). Consider refactoring. Open
protected SwiftTagListBlock consumeTagListBlock(final SwiftTagListBlock b, final String s) {
// start processing the block data
final int start = s.indexOf(':');
if (start >= 0 && start + 1 < s.length()) {
final String data = s.substring(start + 1);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method consumeBlock
has 53 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected SwiftBlock consumeBlock(final UnparsedTextList unparsedReceiver) throws IOException {
// search for block start
final String unparsed = findBlockStart();
Method readUntilBlockEnds
has 53 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected String readUntilBlockEnds() throws IOException {
final int start = buffer == null ? 0 : buffer.length();
int len = 0;
int c;
Method createBlock
has 38 lines of code (exceeds 25 allowed). Consider refactoring. Open
private SwiftBlock createBlock(final char blockId, final String s) {
SwiftBlock b;
// create the block object
switch (blockId) {
Method findEndOfTagByLineFeed
has 34 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected int findEndOfTagByLineFeed(final String s, int start, final boolean isTextBlock) {
int i = start;
// start scanning for tag end
Method createBlock
has a Cognitive Complexity of 14 (exceeds 8 allowed). Consider refactoring. Open
private SwiftBlock createBlock(final char blockId, final String s) {
SwiftBlock b;
// create the block object
switch (blockId) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method processUnparsedText
has a Cognitive Complexity of 13 (exceeds 8 allowed). Consider refactoring. Open
private UnparsedTextList processUnparsedText(final String unparsedText) {
// prepare to process
UnparsedTextList list = null;
// we start a new unparsed text at every "{1:"
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method isTextBlock
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
private boolean isTextBlock(final String s) {
// hack to report as block4 only text blocks 4 , check data in buffer
if (s.length() < 3) {
return false;
}
Method consumeTagListBlock
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected SwiftTagListBlock consumeTagListBlock(final SwiftTagListBlock b, final String s) {
// start processing the block data
final int start = s.indexOf(':');
if (start >= 0 && start + 1 < s.length()) {
final String data = s.substring(start + 1);
Method isTextBlock
has a Cognitive Complexity of 11 (exceeds 8 allowed). Consider refactoring. Open
private boolean isTextBlock(final String s) {
// hack to report as block4 only text blocks 4 , check data in buffer
if (s.length() < 3) {
return false;
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method identifyBlock
has a Cognitive Complexity of 11 (exceeds 8 allowed). Consider refactoring. Open
protected char identifyBlock(final String s) {
if (s != null && s.length() > 1) {
final char c = s.charAt(0);
if ('0' <= c && c <= '9') {
return c;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method createBlock2
has a Cognitive Complexity of 10 (exceeds 8 allowed). Consider refactoring. Open
private SwiftBlock2 createBlock2(final String s) {
Character block2Type = extractBlock2Type(s);
if (Character.valueOf('I').equals(block2Type)) {
return enrichBlockType(createBlock2Input(s), "I");
} else if (Character.valueOf('O').equals(block2Type)) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid too many return
statements within this method. Open
return new SwiftBlock2Output();
Avoid too many return
statements within this method. Open
return false;
Refactor this method to reduce its Cognitive Complexity from 24 to the 15 allowed. Open
protected SwiftBlock consumeBlock(final UnparsedTextList unparsedReceiver) throws IOException {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed. Open
protected SwiftTagListBlock consumeTagListBlock(final SwiftTagListBlock b, final String s) {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Refactor this method to reduce its Cognitive Complexity from 42 to the 15 allowed. Open
protected SwiftBlock4 consumeBlock4(final SwiftBlock4 b, final String s) {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed. Open
protected int findEndOfTagByLineFeed(final String s, int start, final boolean isTextBlock) {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Refactor this method to reduce its Cognitive Complexity from 44 to the 15 allowed. Open
protected String readUntilBlockEnds() throws IOException {
- Read upRead up
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
Add a default case to this switch. Open
switch (c) {
- Read upRead up
- Exclude checks
The requirement for a final default
clause is defensive programming. The clause should either take appropriate action, or contain a
suitable comment as to why no action is taken.
Noncompliant Code Example
switch (param) { //missing default clause case 0: doSomething(); break; case 1: doSomethingElse(); break; } switch (param) { default: // default clause should be the last one error(); break; case 0: doSomething(); break; case 1: doSomethingElse(); break; }
Compliant Solution
switch (param) { case 0: doSomething(); break; case 1: doSomethingElse(); break; default: error(); break; }
Exceptions
If the switch
parameter is an Enum
and if all the constants of this enum are used in the case
statements,
then no default
clause is expected.
Example:
public enum Day { SUNDAY, MONDAY } ... switch(day) { case SUNDAY: doSomething(); break; case MONDAY: doSomethingElse(); break; }
See
- MITRE, CWE-478 - Missing Default Case in Switch Statement
- CERT, MSC01-C. - Strive for logical completeness
End this switch case with an unconditional break, return or throw statement. Open
case '}':
- Read upRead up
- Exclude checks
When the execution is not explicitly terminated at the end of a switch case, it continues to execute the statements of the following case. While this is sometimes intentional, it often is a mistake which leads to unexpected behavior.
Noncompliant Code Example
switch (myVariable) { case 1: foo(); break; case 2: // Both 'doSomething()' and 'doSomethingElse()' will be executed. Is it on purpose ? doSomething(); default: doSomethingElse(); break; }
Compliant Solution
switch (myVariable) { case 1: foo(); break; case 2: doSomething(); break; default: doSomethingElse(); break; }
Exceptions
This rule is relaxed in the following cases:
switch (myVariable) { case 0: // Empty case used to specify the same behavior for a group of cases. case 1: doSomething(); break; case 2: // Use of return statement return; case 3: // Use of throw statement throw new IllegalStateException(); case 4: // Use of continue statement continue; default: // For the last case, use of break statement is optional doSomethingElse(); }
See
- MITRE, CWE-484 - Omitted Break Statement in Switch
- CERT, MSC17-C. - Finish every set of statements associated with a case label with a break statement
- CERT, MSC52-J. - Finish every set of statements associated with a case label with a break statement
Similar blocks of code found in 3 locations. Consider refactoring. Open
private SwiftBlock2Input createBlock2Input(final String s) {
try {
return new SwiftBlock2Input(s, false);
} catch (IllegalArgumentException e) {
if (this.configuration.isLenient()) {
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 56.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 3 locations. Consider refactoring. Open
private SwiftBlock1 createBlock1(final String s) {
try {
return new SwiftBlock1(s, false);
} catch (IllegalArgumentException e) {
if (this.configuration.isLenient()) {
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 56.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 3 locations. Consider refactoring. Open
private SwiftBlock2Output createBlock2Output(final String s) {
try {
return new SwiftBlock2Output(s, false);
} catch (IllegalArgumentException e) {
if (this.configuration.isLenient()) {
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 56.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Identical blocks of code found in 2 locations. Consider refactoring. Open
if (unparsed.length() > 0) {
if (unparsedReceiver == null) {
log.warning("Unparsed text '" + unparsed + "' can not be reported since unparsedReceiver is null");
} else {
unparsedReceiver.addText(unparsed);
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 48.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Identical blocks of code found in 2 locations. Consider refactoring. Open
if (unparsed.length() > 0) {
if (unparsedReceiver == null) {
log.warning("Unparsed text '" + unparsed + "' can not be reported since unparsedReceiver is null");
} else {
unparsedReceiver.addText(unparsed);
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 48.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76