Completed
Push — master ( 310450...75af5b )
by Nicklas
02:16
created

Question::getQuestions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3
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 = "ramverk1_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
     * @param array $param
0 ignored issues
show
Bug introduced by
There is no parameter named $param. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
34
     *
35
     * @return object
36
     */
37 8
    public function setupQuestion($question)
38
    {
39 8
        $user = new User($this->db);
40 8
        $post = new Post($this->db);
41
42 8
        $question->img = $user->getGravatar($question->user);
43 8
        $question->title = $this->getMD($question->title);
44 8
        $question->tags = explode(',', $question->tags);
45
46 8
        $question->question = $post->getPost("questionId = ? AND type = ?", [$question->id, "question"]);
47 8
        $question->answers = $post->getAllPosts("questionId = ? AND type = ?", [$question->id, "answer"]);
48 8
        $question->answerCount = count($question->answers);
49
50
51 8
        return $question;
52
    }
53
    /**
54
     * Returns post with markdown and gravatar
55
     * @param string $sql
56
     * @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...
57
     *
58
     * @return array
59
     */
60 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...
61
    {
62 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...
63 2
            $questions = $this->findAll();
64 2
        }
65 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...
66 8
            $questions = $this->findAllWhere($sql, $params);
67 8
        }
68
        // array_reverse so latest order question gets returned
69 8
        return array_map(array($this, 'setupQuestion'), $questions);
0 ignored issues
show
Bug introduced by
The variable $questions does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
    }
71
72
    /**
73
    * Returns one question with it's own question text and other answers
74
    * @param int $id
75
    *
76
     * @return object
77
    */
78 7
    public function getQuestion($id)
79
    {
80 7
        $question = $this->find("id", $id);
81 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...
82
    }
83
84
    /**
85
     * Returns array of tags, keys are name, value is the integer of how many.
86
     *
87
     * @return array
88
    */
89 1
    public function getPopularTags()
90
    {
91 1
        $question = $this->findAll();
92
93 1
        $tagsMultiArray = array_map(function ($question) {
94 1
            return explode(',', $question->tags);
95 1
        }, $question);
96
97
        // Merge multi array, count values, sort high to low
98 1
        $tagArr = array_count_values(call_user_func_array("array_merge", $tagsMultiArray));
99 1
        arsort($tagArr);
100 1
        return $tagArr;
101
    }
102
}
103