Test Failed
Push — master ( 7e1229...8260e2 )
by Nicklas
02:49
created

Post::__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 Post extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "ramverk1_posts";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $user;
23
    public $questionId; # Connection to new Question()
24
25
    public $type; # question/answer
26
    public $text;
27
28
    public $created;
29
30
    public $di;
31
32
33
    /**
34
     * Constructor injects with DI container.
35
     *
36
     */
37
    public function __construct($di = null)
38
    {
39
        $this->di = $di;
40
    }
41
    /**
42
     * Returns post with markdown and gravatar
43
     * @param string $sql
44
     * @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...
45
     *
46
     * @return array
47
     */
48
    public function getAllPosts($sql, $params)
49
    {
50
        $posts = $this->findAllWhere("$sql", $params);
51
52
        return array_map(function ($post) {
53
54
            // Get e-mail for Post
55
            $user = new User();
56
            $user->setDb($this->di->get("db"));
57
            $user->find("name", $post->user);
58
            $post->img = $this->gravatar($user->email);
59
60
            // Get comments for Post
61
            $comment = new Comment();
62
            $comment->setDb($this->di->get("db"));
63
            $post->comments = $comment->getComments("parentId = ?", [$post->id]);
64
65
            // Get text
66
            $post->markdown = $this->getMD($post->text);
67
68
            return $post;
69
        }, $posts);
70
    }
71
72
    /**
73
     * return question/answer, three attributes are set, comments connected to them is an array.
74
     *
75
     * @return object
76
    */
77
    public function getPost($sql, $params)
78
    {
79
        $post = $this->findWhere("$sql", $params);
80
81
82
        // Get e-mail for question
83
        $user = new User();
84
        $user->setDb($this->di->get("db"));
85
        $user->find("name", $post->user);
86
        $post->img = $this->gravatar($user->email);
87
88
        // Start setting attributes
89
        $post->img = $this->gravatar($user->email);
90
        $post->markdown = $this->getMD($post->text);
91
        $post->comments = $comment->getComments("parentId = ?", [$post->id]);
0 ignored issues
show
Bug introduced by
The variable $comment does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
92
93
        return $post;
94
    }
95
96
    /**
97
     * Check if a post belongs to user
98
     *
99
     *
100
     * @return boolean
101
     */
102 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...
103
    {
104
        $user = new User();
105
        $user->setDb($this->di->get("db"));
106
        $user->find("name", $name);
107
108
        // IF AUTHORITY == admin, then continue
109
        if ($user->authority != "admin") {
110
            return ($user->name == $this->user);
111
        }
112
        return true;
113
    }
114
}
115