| Conditions | 14 |
| Paths | 144 |
| Total Lines | 51 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 16 | public function setAnswer() |
||
| 17 | { |
||
| 18 | $questionId = 0; |
||
| 19 | |||
| 20 | if (method_exists($this, 'getIid')) { |
||
| 21 | $questionId = (int) $this->getIid(); |
||
| 22 | } elseif (property_exists($this, 'iid')) { |
||
| 23 | $questionId = (int) $this->iid; |
||
| 24 | } elseif (property_exists($this, 'id')) { |
||
| 25 | $questionId = (int) $this->id; |
||
| 26 | } |
||
| 27 | |||
| 28 | // Avoid null IDs: use 0 as safe fallback (some flows set the ID later). |
||
| 29 | if ($questionId < 0) { |
||
| 30 | $questionId = 0; |
||
| 31 | } |
||
| 32 | |||
| 33 | switch ($this->type) { |
||
| 34 | case TF: |
||
| 35 | case MCUA: |
||
| 36 | case MCMA: |
||
| 37 | $answer = new ImsAnswerMultipleChoice($questionId); |
||
| 38 | break; |
||
| 39 | |||
| 40 | case FIB: |
||
| 41 | $answer = new ImsAnswerFillInBlanks($questionId); |
||
| 42 | break; |
||
| 43 | |||
| 44 | case MATCHING: |
||
| 45 | case MATCHING_DRAGGABLE: |
||
| 46 | $answer = new ImsAnswerMatching($questionId); |
||
| 47 | break; |
||
| 48 | |||
| 49 | case FREE_ANSWER: |
||
| 50 | $answer = new ImsAnswerFree($questionId); |
||
| 51 | break; |
||
| 52 | |||
| 53 | case HOT_SPOT: |
||
| 54 | $answer = new ImsAnswerHotspot($questionId); |
||
| 55 | break; |
||
| 56 | |||
| 57 | default: |
||
| 58 | $answer = null; |
||
| 59 | } |
||
| 60 | |||
| 61 | // If the parent Question class expects an internal property, keep it. |
||
| 62 | if (property_exists($this, 'answer')) { |
||
| 63 | $this->answer = $answer; |
||
| 64 | } |
||
| 65 | |||
| 66 | return $answer; |
||
| 67 | } |
||
| 79 |