Completed
Pull Request — master (#18)
by
unknown
04:15
created

CommentsInSession   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 35.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 7
Bugs 2 Features 5
Metric Value
wmc 13
lcom 1
cbo 1
dl 37
loc 105
rs 10
c 7
b 2
f 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
B humanTiming() 23 23 5
A getGravatar() 14 14 3
A findAll() 0 16 3
A update() 0 6 1
A delete() 0 6 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
 * class for commenting
4
 */
5
namespace Loom\Comment;
6
7
class CommentsInSession extends \Phpmvc\Comment\CommentsInSession
0 ignored issues
show
Bug introduced by
There is one abstract method setDI in this class; you could implement it, or declare this class as abstract.
Loading history...
8
{
9
10 View Code Duplication
    private function humanTiming($time)
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...
11
    {
12
13
        $time = time() - $time; // to get the time since that moment
14
        $time = ($time<1)? 1 : $time;
15
        $tokens = array (
16
            31536000 => 'år',
17
            2592000 => 'månader',
18
            604800 => 'veckor',
19
            86400 => 'dagar',
20
            3600 => 'timmar',
21
            60 => 'minuter',
22
            1 => 'sekunder'
23
        );
24
25
        foreach ($tokens as $unit => $text) {
26
            if ($time < $unit) {
27
                continue;
28
            }
29
            $numberOfUnits = floor($time / $unit);
30
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?' ':'');
31
        }
32
    }
33
34
    /**
35
     * Get either a Gravatar URL or complete image tag for a specified email address.
36
     *
37
     * @param string $email The email address
38
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
39
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
40
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
41
     * @param boole $img True to return a complete IMG tag False for just the URL
42
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
43
     * @return String containing either just a URL or a complete image tag
44
     * @source http://gravatar.com/site/implement/images/php/
45
     */
46 View Code Duplication
    private function getGravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
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...
47
    {
48
        $url = 'http://www.gravatar.com/avatar/';
49
        $url .= md5(strtolower(trim($email)));
50
        $url .= "?s=$s&d=$d&r=$r";
51
        if ($img) {
52
            $url = '<img src="' . $url . '"';
53
            foreach ($atts as $key => $val) {
54
                $url .= ' ' . $key . '="' . $val . '"';
55
            }
56
            $url .= ' />';
57
        }
58
        return $url;
59
    }
60
61
    /**
62
     * Find and return all comments within the page comment flow.
63
     *
64
     * @return array with all comments.
65
     */
66
    public function findAll()
67
    {
68
        $result = array();
69
        // TODO: add flow tag through object instantiation instead?
70
        $flow = $this->request->getRoute();
0 ignored issues
show
Bug introduced by
The property request 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...
71
        $comments = $this->session->get('comments', []);
0 ignored issues
show
Bug introduced by
The property session 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...
72
        foreach ($comments as $id => $comment) {
73
            if ($comment['comment-flow']==$flow) {
74
                $comment['since-time'] = $this->humanTiming($comment['timestamp']);
75
                $comment['id'] = $id;
76
                $comment['gravatar'] = $this->getGravatar($comment['mail']);
77
                $result[] = $comment;
78
            }
79
        }
80
        return $result;
81
    }
82
83
    /**
84
     * Update a comment.
85
     *
86
     * @param array $comment with all details.
87
     * @param integer $id of comment to be updated
88
     *
89
     * @return void
90
     */
91
    public function update($comment, $id)
92
    {
93
        $comments = $this->session->get('comments', []);
94
        $comments[$id] = $comment;
95
        $this->session->set('comments', $comments);
96
    }
97
98
    /**
99
     * Delete a comment.
100
     *
101
     * @param $id of post to delete.
102
     *
103
     * @return void
104
     */
105
    public function delete($id)
106
    {
107
        $comments = $this->session->get('comments', []);
108
        unset($comments[$id]);
109
        $this->session->set('comments', $comments);
110
    }
111
}
112