Vote   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 22.95 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 28
loc 122
ccs 42
cts 44
cp 0.9545
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getVote() 0 14 1
B saveVote() 0 28 4
A like() 14 14 3
A dislike() 14 14 3

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 Vote extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "coffee_votes";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $user; # question/answer/comment
23
    public $parentId; # All posts have different ids
24
    public $parentType; # post or comment
25
    public $upVote;
26
    public $downVote;
27
28
29
    /**
30
     * Return a vote
31
     * @param string $sql
32
     * @param array $params
33
     *
34
     * @return object
35
     */
36 11
    public function getVote($sql, $params)
37
    {
38 11
        $likes = $this->findAllWhere("$sql", $params);
39
40
        // Return users of the ones who have upvoted.
41
        $score = array_reduce($likes, function ($carry, $item) {
42 10
            $carry += $item->upVote - $item->downVote;
43 10
            return $carry;
44 11
        });
45
46 11
        $this->likes = $likes;
0 ignored issues
show
Bug introduced by
The property likes 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...
47 11
        $this->score = $score;
0 ignored issues
show
Bug introduced by
The property score 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...
48 11
        return $this;
49
    }
50
51
    /**
52
     * Checks if vote already exists, then either create new or update
53
     * @param array $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. 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...
54
     *
55
     * @return bool
56
     */
57 2
    public function saveVote($user, $parentId, $parentType, $voteType = "like")
58
    {
59 2
        $vote = $this->findAllWhere("parentId = ? AND parentType = ? AND user = ?", [$parentId, $parentType, $user])[0];
60
61 2
        if ($vote != null) {
62 2
            $vote->setDb($this->db);
63 2
        }
64
65 2
        if ($vote == null) {
66
            $vote = $this;
67
        }
68
69 2
        $vote->user = $user;
70 2
        $vote->parentId = $parentId;
71 2
        $vote->parentType = $parentType;
72 2
        $vote->upVote = 1;
73 2
        $vote->downVote = null;
74
75 2
        if ($voteType == "downVote") {
76 1
            $vote->upVote = null;
77 1
            $vote->downVote = 1;
78 1
        }
79
80
81
82 2
        $vote->save();
83 2
        return true;
84
    }
85
86
    /**
87
     * Control if user has already liked or not
88
     * @param string $user
89
     * @param array $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. 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...
90
     *
91
     * @return bool
92
     */
93 1 View Code Duplication
    public function like($user, $parentId, $parentType)
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...
94
    {
95 1
        $likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]);
96
        // Return users of the ones who have upvoted.
97
        $users = array_map(function ($like) {
98 1
            return $like->upVote != null ? $like->user : "";
99 1
        }, $likes);
100
101 1
        if (in_array($user, $users)) {
102 1
            return false;
103
        }
104 1
        $this->saveVote($user, $parentId, $parentType);
105 1
        return true;
106
    }
107
108
    /**
109
     * Control if user has already liked or not
110
     * @param string $user
111
     * @param array $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. 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...
112
     *
113
     * @return bool
114
     */
115 1 View Code Duplication
    public function dislike($user, $parentId, $parentType)
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...
116
    {
117 1
        $likes = $this->findAllWhere("parentId = ? AND parentType = ?", [$parentId, $parentType]);
118
        // Return users of the ones who have downvoted.
119 1
        $users = array_map(function ($like) {
120 1
            return $like->downVote != null ? $like->user : "";
121 1
        }, $likes);
122
123 1
        if (in_array($user, $users)) {
124 1
            return false;
125
        }
126 1
        $this->saveVote($user, $parentId, $parentType, "downVote");
127 1
        return true;
128
    }
129
}
130