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

CommentsInDb   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 30.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 2 Features 3
Metric Value
wmc 13
lcom 1
cbo 1
dl 45
loc 146
rs 10
c 6
b 2
f 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 52 1
B humanTiming() 23 23 5
A getGravatar() 14 14 3
A findAll() 4 11 2
A findFlow() 4 12 2

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. Model for comments.
4
 */
5
namespace Anax\CommentDb;
6
7
class CommentsInDb extends \Anax\MVC\CDatabaseModel
8
{
9
10
    /**
11
     * Initialize model.
12
     *
13
     * @return array
14
     */
15
    public function init()
16
    {
17
        // $this->db->setVerbose();
18
        $this->db->dropTableIfExists('commentsindb')->execute();
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\CommentDb\CommentsInDb>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
19
20
        $this->db->createTable(
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\CommentDb\CommentsInDb>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
21
            'commentsindb',
22
            [
23
               'id' => ['integer', 'primary key', 'not null', 'auto_increment'],
24
               'flow' => ['varchar(80)'],
25
               'name' => ['varchar(80)'],
26
               'mail' => ['varchar(80)'],
27
               'web' => ['varchar(80)'],
28
               'content' => ['varchar(1024)'],
29
               'created' => ['datetime'],
30
            ]
31
        )->execute();
32
        $this->db->insert(
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\CommentDb\CommentsInDb>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
33
            'commentsindb',
34
            ['flow', 'name', 'mail', 'web', 'content', 'created']
35
        );
36
37
        $now = time();
38
39
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\CommentDb\CommentsInDb>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
            'commentadmin/lorem',
41
            'Fredrik Nilsson',
42
            '[email protected]',
43
            'www.dbwebb.se',
44
            'Lorem ipsum dolor sit amet, ad nam graeci dissentias, te verear utroque per. Doming intellegat mea id, mel ei dicta iudico. Dicunt fabulas usu ad. Per nemore possim commune ut, eu probo dicta has. ',
45
            $now
46
        ]);
47
48
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\CommentDb\CommentsInDb>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
            'commentadmin/lorem',
50
            'Joe Doe',
51
            '[email protected]',
52
            'test.dbwebb.se',
53
            'Accusam eleifend qui ex. Has duis iuvaret salutatus id, dico illud porro ea mei, id oblique tibique eos. Ne eam meis equidem admodum, eos nisl maluisset id. Ancillae lucilius persecuti no sed.',
54
            $now
55
        ]);
56
57
        $this->db->execute([
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Anax\CommentDb\CommentsInDb>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
            'calendar',
59
            'Jane Doe',
60
            '[email protected]',
61
            'jane.dbwebb.se',
62
            'Sea te vocibus dolores pertinax, quodsi insolens appellantur sit an, dicam definitionem sed ne.',
63
            $now
64
        ]);
65
66
    }
67 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...
68
    {
69
70
        $time = time() - $time; // to get the time since that moment
71
        $time = ($time<1)? 1 : $time;
72
        $tokens = array (
73
            31536000 => 'år',
74
            2592000 => 'månader',
75
            604800 => 'veckor',
76
            86400 => 'dagar',
77
            3600 => 'timmar',
78
            60 => 'minuter',
79
            1 => 'sekunder'
80
        );
81
82
        foreach ($tokens as $unit => $text) {
83
            if ($time < $unit) {
84
                continue;
85
            }
86
            $numberOfUnits = floor($time / $unit);
87
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?' ':'');
88
        }
89
    }
90
91
    /**
92
     * Get either a Gravatar URL or complete image tag for a specified email address.
93
     *
94
     * @param string $email The email address
95
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
96
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
97
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
98
     * @param boole $img True to return a complete IMG tag False for just the URL
99
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
100
     * @return String containing either just a URL or a complete image tag
101
     * @source http://gravatar.com/site/implement/images/php/
102
     */
103 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...
104
    {
105
        $url = 'http://www.gravatar.com/avatar/';
106
        $url .= md5(strtolower(trim($email)));
107
        $url .= "?s=$s&d=$d&r=$r";
108
        if ($img) {
109
            $url = '<img src="' . $url . '"';
110
            foreach ($atts as $key => $val) {
111
                $url .= ' ' . $key . '="' . $val . '"';
112
            }
113
            $url .= ' />';
114
        }
115
        return $url;
116
    }
117
118
    /**
119
     * Find and return all comments in complete database.
120
     *
121
     * @return array with all comments.
122
     */
123
    public function findAll()
124
    {
125
        $comments = parent::findAll();
126
127 View Code Duplication
        foreach ($comments as $id => $comment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
            $comment->since_time = $this->humanTiming($comment->created);
129
            $comment->gravatar = $this->getGravatar($comment->mail);
130
        }
131
        // echo __FILE__ . " : " . __LINE__ . "<br>";dump($comments);
132
        return $comments;
133
    }
134
135
    /**
136
     * Find and return all comments within the page comment flow.
137
     *
138
     * @return array with all comments.
139
     */
140
    public function findFlow($flow = null)
141
    {
142
        $comments = $this->query()
143
        ->where('flow = ' . "'$flow'")
144
        ->execute();
145
        // Add human timing and gravatars to each comment post.
146 View Code Duplication
        foreach ($comments as $id => $comment) {
0 ignored issues
show
Bug introduced by
The expression $comments of type object<Anax\CommentDb\CommentsInDb> is not traversable.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
147
            $comment->since_time = $this->humanTiming($comment->created);
148
            $comment->gravatar = $this->getGravatar($comment->mail);
149
        }
150
        return $comments;
151
    }
152
}
153