Completed
Push — master ( a87104...0a436d )
by Nicklas
02:30
created

User   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 170
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllUsers() 0 22 3
A getUser() 0 16 1
A userExists() 0 5 2
A setGravatar() 0 4 1
A getGravatar() 0 5 1
A setPassword() 0 4 1
A verifyPassword() 0 5 1
A verifyQuestion() 0 5 1
A controlAuthority() 0 9 2
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
    /**
30
     * Returns post with markdown and gravatar
31
     * @param string $sql
32
     * @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...
33
     *
34
     * @return array
35
     */
36 1
    public function getAllUsers($sql = null, $params = null)
37
    {
38 1
        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...
39 1
            $users = $this->findAll();
40 1
        }
41 1
        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...
42 1
            $users = $this->findAllWhere($sql, $params);
43 1
        }
44
45 1
        $question   = new Question($this->db);
46 1
        $post       = new Post($this->db);
47 1
        $comment    = new Comment($this->db);
48
49 1
        return array_map(function ($user) use ($question, $post, $comment) {
50 1
            $user->questions    = $question->getQuestions("user = ?", $user->name);
51 1
            $user->posts        = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]);
52 1
            $user->comments     = $comment->getComments("user = ?", $user->name);
53 1
            $user->img          = $this->getGravatar($user->name);
54
            
55 1
            return $user;
56 1
        }, $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...
57
    }
58
59
    /**
60
     * return question/answer, three attributes are set, comments connected to them is an array.
61
     *
62
     * @return object
63
    */
64 1
    public function getUser($name)
65
    {
66 1
        $user = $this->find("name", $name);
67
68 1
        $question   = new Question($this->db);
69 1
        $post       = new Post($this->db);
70 1
        $comment    = new Comment($this->db);
71
72
        // Get all the different posts user made.
73 1
        $user->questions    = $question->getQuestions("user = ?", $user->name);
74 1
        $user->posts        = $post->getAllPosts("user = ? AND type = ?", [$user->name, "answer"]);
75 1
        $user->comments     = $comment->getComments("user = ?", $user->name);
76 1
        $user->img          = $this->getGravatar($name);
77
78 1
        return $user;
79
    }
80
81
82
    /**
83
     * Check if user exists
84
     *
85
     * @param string $name
86
     *
87
     * @return boolean true if user exists in database else false
88
     */
89 1
    public function userExists($name)
90
    {
91 1
        $user = $this->find("name", $name);
92 1
        return !$user ? false : true;
93
    }
94
    /**
95
     * Returns gravatar link
96
     *
97
     * @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...
98
     *
99
     * @return string as gravatar link
100
     */
101 5
    public function setGravatar()
102
    {
103 5
        $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...
104 5
    }
105
106
    /**
107
     * Returns gravatar link
108
     *
109
     * @param string $name
110
     *
111
     * @return string as gravatar link
112
     */
113 6
    public function getGravatar($name)
114
    {
115 6
        $this->find("name", $name);
116 6
        return $this->gravatar($this->email);
117
    }
118
119
    /**
120
     * Set the pass.
121
     *
122
     * @param string $pass the pass to use.
123
     *
124
     * @return void
125
     */
126 5
    public function setPassword($pass)
127
    {
128 5
        $this->pass = password_hash($pass, PASSWORD_DEFAULT);
129 5
    }
130
131
    /**
132
     * Verify the name and the pass, if successful the object contains
133
     * all details from the database row.
134
     *
135
     * @param string $name  name to check.
136
     * @param string $pass the pass to use.
137
     *
138
     * @return boolean true if name and pass matches, else false.
139
     */
140 3
    public function verifyPassword($name, $pass)
141
    {
142 3
        $this->find("name", $name);
143 3
        return password_verify($pass, $this->pass);
144
    }
145
146
    /**
147
     * Verify the name and the anaswer, if successful the object contains
148
     * all details from the database row.
149
     *
150
     * @param string $name  name to check.
151
     * @param string $answer the answer.
152
     *
153
     * @return boolean true if name and pass matches, else false.
154
     */
155 1
    public function verifyQuestion($name, $answer)
156
    {
157 1
        $this->find("name", $name);
158 1
        return ($this->question == $answer);
159
    }
160
161
162
    /**
163
    * Check if a post belongs to user
164
    *
165
    *
166
    * @return boolean
167
    */
168 1
    public function controlAuthority($loggedUser, $name)
169
    {
170 1
        $this->find("name", $loggedUser);
171
        // IF AUTHORITY == admin, then continue
172 1
        if ($this->authority != "admin") {
173 1
            return ($this->name == $name);
174
        }
175 1
        return true;
176
    }
177
}
178