Test Failed
Push — master ( 086d7c...48da91 )
by Nicklas
03:17
created

Comment   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 108
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPosts() 0 15 1
A getPost() 0 16 1
A controlAuthority() 0 12 2
1
<?php
2
3
namespace Nicklas\Comment;
4
5
/**
6
 * A database driven model.
7
 */
8
class Comment extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "ramverk1_comments";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $di;
23
24
25
    public $text;
26
27
    public $parentId; # All posts have different ids
28
    public $type; # question/answer/comment
29
30
    public $created;
31
    public $status; # default is active
32
33
34
35
36
    /**
37
     * HA EN FOREACH I En FOREACH FÖR ATT HA COMMENTS CONNECTED
38
     *
39
40
        */
41
42
    /**
43
     * Constructor injects with DI container.
44
     *
45
     */
46
    public function __construct($di = null)
47
    {
48
        $this->di = $di;
49
    }
50
    /**
51
     * Returns post with markdown and gravatar
52
     * @param string $sql
53
     * @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...
54
     *
55
     * @return array
56
     */
57
    public function getPosts($sql, $params)
58
    {
59
        $posts = $this->findAllWhere("$sql", $params);
60
61
        return array_map(function ($post) {
62
            $user = new User();
63
            $user->setDb($this->di->get("db"));
64
            $user->find("name", $post->user);
65
66
            $post->img = $this->gravatar($user->email);
67
            $post->markdown = $this->getMD($post->text);
68
69
            return $post;
70
        }, $posts);
71
    }
72
73
    /**
74
     * return question/answer, three attributes are set, comments connected to them is an array.
75
     *
76
     * @return object
77
    */
78
        public function getPost($id)
79
        {
80
            $post = $this->find("id", $id);
81
82
            // Get user who posted
83
            $user = new User();
84
            $user->setDb($this->di->get("db"));
85
            $user->find("name", $post->user);
86
87
            // Start setting attributes
88
            $post->comments = $this->getPosts("parentId = ? AND type = ?", [$id, "comment"]);
89
            $post->img = $this->gravatar($user->email);
90
            $post->markdown = $this->getMD($post->text);
91
92
            return $post;
93
        }
94
95
96
97
    /**
98
     * Check if a post belongs to user
99
     *
100
     *
101
     * @return boolean
102
     */
103
    public function controlAuthority($name)
104
    {
105
        $user = new User();
106
        $user->setDb($this->di->get("db"));
107
        $user->find("name", $name);
108
109
        // IF AUTHORITY == admin, then continue
110
        if ($user->authority != "admin") {
111
            return ($user->name == $this->user);
0 ignored issues
show
Bug introduced by
The property user 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...
112
        }
113
        return true;
114
    }
115
}
116