Completed
Push — master ( 8260e2...8ffaa6 )
by Nicklas
02:17
created

Question::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 = "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
    /**
32
     * Returns post with markdown and gravatar
33
     * @param string $sql
34
     * @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...
35
     *
36
     * @return array
37
     */
38 2
    public function getQuestions($sql = null, $params = null)
39
    {
40 2
        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...
41 2
            $questions = $this->findAll();
42 2
        }
43 2
        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...
44
            $questions = $this->findAllWhere("$sql", $params);
45
        }
46
47
        return array_map(function ($question) {
48
49
            // Find users email
50 2
            $user = new User($this->db);
51 2
            $user->find("name", $question->user);
52
53
            // Start setting attributes
54 2
            $question->img = $this->gravatar($user->email);
55 2
            $question->title = $this->getMD($question->title);
56 2
            $question->tags = explode(',', $question->tags);
57
58 2
            return $question;
59 2
        }, $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...
60
    }
61
62
    /**
63
    * Returns one question with it's own question text and other answers
64
    * @param int $id
65
    *
66
     * @return object
67
    */
68 1
    public function getQuestion($id)
69
    {
70 1
        $question = $this->find("id", $id);
71
72
        // Add attributes from Post class
73 1
        $post = new Post($this->db);
74
75 1
        $question->question = $post->getPost("questionId = ? AND type = ?", [$question->id, "question"]);
76 1
        $question->answers = $post->getAllPosts("questionId = ? AND type = ?", [$question->id, "answer"]);
77 1
        $question->answersCount = count($question->answers);
78
79
        // Start setting up own attributes
80 1
        $question->title = $this->getMD($question->title);
81 1
        $question->tags = explode(',', $question->tags);
82
83
84 1
        return $question;
85
    }
86
87
    /**
88
     * Returns array of tags, keys are name, value is the integer of how many.
89
     *
90
     * @return array
91
    */
92
    public function getPopularTags()
93
    {
94
        $question = $this->findAll();
95
96
        $tagsMultiArray = array_map(function ($question) {
97
            return explode(',', $question->tags);
98
        }, $question);
99
100
        // Merge multi array, count values, sort high to low
101
        $tagArr = array_count_values(call_user_func_array("array_merge", $tagsMultiArray));
102
        arsort($tagArr);
103
104
        if (array_key_exists('', $tagArr)) {
105
            unset($tagArr[""]);
106
        }
107
        return $tagArr;
108
    }
109
110
111
112
113
    /**
114
     * Check if a post belongs to user
115
     *
116
     *
117
     * @return boolean
118
     */
119 View Code Duplication
    public function controlAuthority($name)
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...
120
    {
121
        $user = new User($this->db);
122
        $user->find("name", $name);
123
124
        // IF AUTHORITY == admin, then continue
125
        if ($user->authority != "admin") {
126
            return ($user->name == $this->user);
127
        }
128
        return true;
129
    }
130
}
131