GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

QuizQuestionnaireParser::parseBody()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 7.2944

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 27
cts 33
cp 0.8182
rs 7.983
c 0
b 0
f 0
cc 7
nc 4
nop 1
crap 7.2944

How to fix   Long Method   

Long Method

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:

1
<?php
2
/*
3
 * This file is part of the trefoil application.
4
 *
5
 * (c) Miguel Angel Gabriel <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Trefoil\Helpers;
12
13
use Symfony\Component\DomCrawler\Crawler;
14
use Trefoil\Util\CrawlerTools;
15
16
/**
17
 * Parse an HTML representation of a questionnaire into a QuizQuestionnaire object.
18
 *
19
 * Example questionnaire:
20
 *
21
 * <div class="questions" data-id="1-1">
22
 *     <h5>Optional heading</h5>
23
 *     <h6>Optional subheading</h6>
24
 *     <p>free text</p>
25
 *     <ul><li>can have unordered lists</li></ul>
26
 *     <p>more free text</p>
27
 *
28
 *     [questionnaire questions]
29
 * </div>
30
 *
31
 * Where [questionnaire questions]:
32
 *
33
 *  <ol>
34
 *    <li><p>Text of question 1</p>
35
 *        ... whatever text and markup ...
36
 *      <h6>Solution heading (optional)</h6>
37
 *      <p>Solution text (optional)</p>
38
 *        ... whatever text and markup ...
39
 *    </li>
40
 *    <li><p>Text of question 2</p>
41
 *        ...
42
 *    </li>
43
 *  </ol>
44
 */
45
class QuizQuestionnaireParser extends QuizItemParser
46
{
47
    /**
48
     * @var QuizQuestionnaire
49
     */
50
    protected $quizQuestionnaire;
51
52 1
    public function __construct($text)
53
    {
54 1
        $this->quizQuestionnaire = new QuizQuestionnaire();
55 1
        parent::__construct($text, $this->quizQuestionnaire);
56 1
    }
57
58
    /**
59
     * Example body:
60
     *
61
     *  <ol>
62
     *    <li><p>Text of question 1</p>
63
     *        ... whatever text and markup ...
64
     *      <h6>Solution heading (optional)</h6>
65
     *      <p>Solution text (optional)</p>
66
     *        ... whatever text and markup ...
67
     *    </li>
68
     *    <li><p>Text of question 2</p>
69
     *        ...
70
     *    </li>
71
     *  </ol>
72
     *
73
     * @param Crawler $crawler pointing to "div" node of the quiz item
74
     *
75
     * @throws \RuntimeException
76
     * @return array             with questionnaire values
77
     */
78 1
    protected function parseBody(Crawler $crawler)
79
    {
80
        // 'ol' node contains all the questions
81 1
        $olNode = $crawler->filter('div>ol');
82
83 1
        if (0 == $olNode->count()) {
84
            throw new \RuntimeException(
85
                sprintf(
86
                    'No questions found for questionnaire id "%s"' . "\n"
87
                    . $this->quizQuestionnaire->getId()
88
                ));
89
        }
90
91
        // collect questions
92 1
        $questionsList = array();
93
94 1
        $qnodes = $olNode->children();
95
96
        // all the 1st level "li" nodes are questions
97 1
        foreach ($qnodes as $qDomNode) {
98 1
            $qnode = new Crawler($qDomNode);
99
100 1
            $question = new QuizQuestionnaireQuestion();
101
102 1
            $qnodeNodes = $qnode->children();
103
104 1
            if (0 == $qnodeNodes->count()) {
105
                // this "li" question node doesn't have anything in it except some text
106
                $question->setText('<p>' . $qnode->text() . '</p>');
107
            } else {
108
                // a normal question
109 1
                foreach ($qnodeNodes as $qnodeDomNode) {
110 1
                    $qnodeNode = new Crawler($qnodeDomNode);
111
112 1
                    $nodeName = CrawlerTools::getNodeName($qnodeNode);
113 1
                    if ('h6' == $nodeName) {
114
                        // solution heading
115 1
                        $question->setSolution('');
116 1
                        $question->setHeading($qnodeNode->text());
117 1
                    } else {
118
                        // piece of question text or solution text
119 1
                        $piece = CrawlerTools::getNodeHtml($qnodeNode);
120 1
                        if ($question->getHeading()) {
121
                            // solution
122 1
                            $question->setSolution($question->getSolution() . $piece);
123 1
                        } else {
124
                            // question text
125 1
                            $question->setText($question->getText() . $piece);
126
                        }
127
                    }
128 1
                }
129
            }
130
131 1
            $questionsList[] = $question;
132 1
        }
133
134 1
        $this->quizQuestionnaire->setQuestions($questionsList);
135 1
    }
136
137
}
138