Completed
Push — master ( ac011d...56c397 )
by Mikołaj
03:19
created

AnswersModel::getAnswersByQuestions()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.439
cc 6
eloc 16
nc 17
nop 1
1
<?php
2
3
namespace Egzaminer\Model;
4
5
use PDO;
6
7
class AnswersModel extends AbstractModel
8
{
9
    /**
10
     * Get answers by questions (ID).
11
     *
12
     * @param array $anwers
0 ignored issues
show
Documentation introduced by
There is no parameter named $anwers. Did you maybe mean $answers?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
13
     *
14
     * @return array
15
     */
16
    public function getAnswersByQuestions(array $answers)
17
    {
18
        if (empty($answers)) {
19
            return;
20
        }
21
22
        $where = '';
23
        foreach ($answers as $key => $value) {
24
            $where .= ' OR question_id = :question_id_'.md5($value['id']);
25
        }
26
27
        $stmt = $this->db->prepare('SELECT * FROM answers WHERE '.trim($where, ' OR '));
28
        foreach ($answers as $key => $value) {
29
            $stmt->bindValue(':question_id_'.md5($value['id']), $value['id'], PDO::PARAM_INT);
30
        }
31
32
        $stmt->execute();
33
34
        $array = [];
35
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $key => $value) {
36
            $array[$value['question_id']][] = $value;
37
        }
38
39
        if (!isset($array)) {
40
            return;
41
        }
42
43
        return $array;
44
    }
45
46
    /**
47
     * Get answers by one question ID.
48
     *
49
     * @param int $questionID
50
     *
51
     * @return array
52
     */
53
    public function getAnswersByOneQuestionId($questionID)
54
    {
55
        $stmt = $this->db->prepare('SELECT * FROM answers WHERE question_id = :qid');
56
        $stmt->bindValue(':qid', $questionID, PDO::PARAM_INT);
57
        $stmt->execute();
58
59
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
60
    }
61
}
62