Passed
Push — master ( c28549...310450 )
by Nicklas
02:13
created

Post   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 88
Duplicated Lines 53.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 47
loc 88
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getAllPosts() 25 25 1
A getPost() 22 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public $accepted; # default "no"
25
26
    public $type; # question/answer
27
    public $text;
28
29
    public $created;
30
31
    public $di;
32
33
34
35
    /**
36
     * Returns post with markdown and gravatar
37
     * @param string $sql
38
     * @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...
39
     *
40
     * @return array
41
     */
42 9 View Code Duplication
    public function getAllPosts($sql, $params)
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...
43
    {
44 9
        $posts = $this->findAllWhere("$sql", $params);
45
46 9
        return array_map(function ($post) {
47
48
            // Get e-mail for Post
49 7
            $user = new User($this->db);
50 7
            $post->img = $user->getGravatar($post->user);
51
52
            // Get comments for Post
53 7
            $comment = new Comment($this->db);
54 7
            $post->comments = $comment->getComments("parentId = ?", [$post->id]);
55
56
57
            // Get votes for Post
58 7
            $vote = new Vote($this->db);
59 7
            $post->vote = $vote->getVote("parentId = ? AND parentType = ?", [$post->id, "post"]);
60
61
            // Get text
62 7
            $post->markdown = $this->getMD($post->text);
63
64 7
            return $post;
65 9
        }, $posts);
66
    }
67
68
    /**
69
     * return question/answer, three attributes are set, comments connected to them is an array.
70
     *
71
     * @return object
72
    */
73 7 View Code Duplication
    public function getPost($sql, $params)
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...
74
    {
75 7
        $post = $this->findWhere("$sql", $params);
76
77
        // Get e-mail for question
78 7
        $user = new User($this->db);
79 7
        $post->img = $user->getGravatar($post->user);
80
81
        // Get comments for Post
82 7
        $comment = new Comment($this->db);
83 7
        $post->comments = $comment->getComments("parentId = ?", [$post->id]);
84
85
        // Get votes for Post
86 7
        $vote = new Vote($this->db);
87 7
        $post->vote = $vote->getVote("parentId = ? AND parentType = ?", [$post->id, "post"]);
88
        
89
        // Start setting attributes
90 7
        $post->markdown = $this->getMD($post->text);
91
92
93 7
        return $post;
94
    }
95
}
96