Question::setupQuestion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nicklas\Comment\Modules;
4
5
/**
6
 * A database driven model.
7
 */
8
class Question extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "coffee_questions";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $user;
23
24
    public $title;
25
    public $tags;
26
27
    public $created;
28
    public $status; # default is active
29
30
    /**
31
     * Set ups the question
32
     * @param object $question
33
     *
34
     * @return object
35
     */
36 8
    public function setupQuestion($question)
37
    {
38 8
        $user = new User($this->db);
39 8
        $post = new Post($this->db);
40
41 8
        $question->img = $user->getGravatar($question->user);
42 8
        $question->title = $this->getMD($question->title);
43 8
        $question->tags = explode(',', $question->tags);
44
45 8
        $question->question = $post->getPost("questionId = ? AND type = ?", [$question->id, "question"]);
46 8
        $question->answers = $post->getAllPosts("questionId = ? AND type = ?", [$question->id, "answer"]);
47 8
        $question->answerCount = count($question->answers);
48
49
50 8
        return $question;
51
    }
52
    /**
53
     * Returns post with markdown and gravatar
54
     * @param string $sql
55
     * @param array $param
0 ignored issues
show
Documentation introduced by
There is no parameter named $param. Did you maybe mean $params?

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...
56
     *
57
     * @return array
58
     */
59 8 View Code Duplication
    public function getQuestions($sql = null, $params = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61 8
        $questions = [];
62
63 8
        if ($sql == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $sql of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
64 2
            $questions = $this->findAll();
65 2
        }
66 8
        if ($sql != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $sql of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
67 8
            $questions = $this->findAllWhere($sql, $params);
68 8
        }
69
        // array_reverse so latest order question gets returned
70 8
        return array_reverse(array_map(array($this, 'setupQuestion'), $questions));
71
    }
72
73
    /**
74
    * Returns one question with it's own question text and other answers
75
    * @param int $id
76
    *
77
     * @return object
78
    */
79 7
    public function getQuestion($id)
80
    {
81 7
        $question = $this->find("id", $id);
82 7
        return $this->setupQuestion($question);
0 ignored issues
show
Documentation introduced by
$question is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
    }
84
85
    /**
86
     * Returns array of tags, keys are name, value is the integer of how many.
87
     *
88
     * @return array
89
    */
90 1
    public function getPopularTags()
91
    {
92 1
        $question = $this->findAll();
93
94 1
        $tagsMultiArray = array_map(function ($question) {
95 1
            return explode(',', $question->tags);
96 1
        }, $question);
97
98
        // Merge multi array, count values, sort high to low
99 1
        $tagArr = array_count_values(call_user_func_array("array_merge", $tagsMultiArray));
100 1
        arsort($tagArr);
101 1
        return $tagArr;
102
    }
103
}
104