Completed
Push — master ( 8ffaa6...f544e7 )
by Nicklas
02:28
created

Post   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 76
Duplicated Lines 47.37 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 36
loc 76
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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