Completed
Push — master ( 0a436d...ec1a89 )
by Nicklas
02:25
created

User::setupUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nicklas\Comment\Modules;
4
5
/**
6
 * A database driven model.
7
 */
8
class User extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "ramverk1_users";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $name;
23
    public $email;
24
    public $pass;
25
    public $authority = "user";
26
    public $question;
27
28
29 7
    public function setupUser($user)
30
    {
31 7
        $question = new Question($this->db);
32 7
        $post     = new Post($this->db);
33 7
        $comment  = new Comment($this->db);
34
35 7
        $user->questions  = $question->getQuestions("user = ?", $user->name);
36 7
        $user->posts      = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]);
37 7
        $user->comments   = $comment->getComments("user = ?", $user->name);
38 7
        $user->img        = $this->getGravatar($user->name);
39 7
        $user->postAmount = count($user->questions) + count($user->posts) + count($user->comments);
40
41
        // Get all the questions connected to answers
42 7
        $user->answeredQuestions = array_map(function ($answer) use ($question) {
43 5
            return $question->getQuestion($answer->questionId);
44 7
        }, $user->posts);
45
46 7
        return $user;
47
    }
48
49
    /**
50
     * Returns post with markdown and gravatar
51
     * @param string $sql
52
     * @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...
53
     *
54
     * @return array
55
     */
56 3
    public function getAllUsers($sql = null, $params = null)
57
    {
58 3
        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...
59 3
            $users = $this->findAll();
60 3
        }
61 3
        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...
62 1
            $users = $this->findAllWhere($sql, $params);
63 1
        }
64
65 3
        return array_map(array($this, 'setupUser'), $users);
0 ignored issues
show
Bug introduced by
The variable $users 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...
66
    }
67
68
    /**
69
     * return question/answer, three attributes are set, comments connected to them is an array.
70
     *
71
     * @return object
72
    */
73 4
    public function getUser($name)
74
    {
75 4
        $user = $this->find("name", $name);
76 4
        $user = $this->setupUser($user);
77 4
        return $user;
78
    }
79
80
81
    /**
82
     * Check if user exists
83
     *
84
     * @param string $name
85
     *
86
     * @return boolean true if user exists in database else false
87
     */
88 1
    public function userExists($name)
89
    {
90 1
        $user = $this->find("name", $name);
91 1
        return !$user ? false : true;
92
    }
93
    /**
94
     * Returns gravatar link
95
     *
96
     * @param string $email
0 ignored issues
show
Bug introduced by
There is no parameter named $email. 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...
97
     *
98
     * @return string as gravatar link
99
     */
100 1
    public function setGravatar()
101
    {
102 1
        $this->img = $this->gravatar($this->email);
0 ignored issues
show
Bug introduced by
The property img does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103 1
    }
104
105
    /**
106
     * Returns gravatar link
107
     *
108
     * @param string $name
109
     *
110
     * @return string as gravatar link
111
     */
112 10
    public function getGravatar($name)
113
    {
114 10
        $this->find("name", $name);
115 10
        return $this->gravatar($this->email);
116
    }
117
118
    /**
119
     * Set the pass.
120
     *
121
     * @param string $pass the pass to use.
122
     *
123
     * @return void
124
     */
125 4
    public function setPassword($pass)
126
    {
127 4
        $this->pass = password_hash($pass, PASSWORD_DEFAULT);
128 4
    }
129
130
    /**
131
     * Verify the name and the pass, if successful the object contains
132
     * all details from the database row.
133
     *
134
     * @param string $name  name to check.
135
     * @param string $pass the pass to use.
136
     *
137
     * @return boolean true if name and pass matches, else false.
138
     */
139 3
    public function verifyPassword($name, $pass)
140
    {
141 3
        $this->find("name", $name);
142 3
        return password_verify($pass, $this->pass);
143
    }
144
145
    /**
146
     * Verify the name and the anaswer, if successful the object contains
147
     * all details from the database row.
148
     *
149
     * @param string $name  name to check.
150
     * @param string $answer the answer.
151
     *
152
     * @return boolean true if name and pass matches, else false.
153
     */
154 1
    public function verifyQuestion($name, $answer)
155
    {
156 1
        $this->find("name", $name);
157 1
        return ($this->question == $answer);
158
    }
159
160
161
    /**
162
    * Check if a post belongs to user
163
    *
164
    *
165
    * @return boolean
166
    */
167 1
    public function controlAuthority($loggedUser, $name)
168
    {
169 1
        $this->find("name", $loggedUser);
170
        // IF AUTHORITY == admin, then continue
171 1
        if ($this->authority != "admin") {
172 1
            return ($this->name == $name);
173
        }
174 1
        return true;
175
    }
176
}
177