| Conditions | 10 |
| Total Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like easytests.api.v1.validators.QuiestionsValidator.validateQuestion(QuestionModelInterface) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package easytests.api.v1.validators; |
||
| 13 | public void validateQuestion(QuestionModelInterface questionModel) throws Exception { |
||
| 14 | final int rightAnswersCount = 0; |
||
| 15 | |||
| 16 | //for (AnswerModelInterface answerModel: questionModel.getAnswers()) { |
||
| 17 | // this.answersValidator.validateAnswer(answerModel); |
||
| 18 | // if (answerModel.getRight()) { |
||
| 19 | // ++rightAnswersCount; |
||
| 20 | // } |
||
| 21 | //} |
||
| 22 | switch (questionModel.getQuestionType().getId()) { |
||
| 23 | case 1: |
||
| 24 | if (rightAnswersCount == 0) { |
||
| 25 | throw new BadRequestException("Right Answer must be exist"); |
||
| 26 | } |
||
| 27 | if (rightAnswersCount > 1) { |
||
| 28 | throw new BadRequestException("This Question can have only one right Answer"); |
||
| 29 | } |
||
| 30 | break; |
||
| 31 | |||
| 32 | case 2: |
||
| 33 | if (rightAnswersCount == 0) { |
||
| 34 | throw new BadRequestException("Right Answers must be exist"); |
||
| 35 | } |
||
| 36 | if (rightAnswersCount == 1) { |
||
| 37 | throw new BadRequestException("This Question can't have only one right Answer"); |
||
| 38 | } |
||
| 39 | if (rightAnswersCount == questionModel.getAnswers().size()) { |
||
| 40 | throw new BadRequestException("This Question can't have all right Answers"); |
||
| 41 | } |
||
| 42 | break; |
||
| 43 | |||
| 44 | case 3: |
||
| 45 | break; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * TODO |
||
| 49 | * Add check for Numeric And Text Answers |
||
| 50 | */ |
||
| 51 | default: break; |
||
| 52 | } |
||
| 55 |