Test Failed
Push — master ( a21b5d...f04a94 )
by Gunvor
01:38
created

ShowOneService   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 247
Duplicated Lines 2.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 30.93%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 26
c 3
b 0
f 0
lcom 1
cbo 2
dl 7
loc 247
ccs 30
cts 97
cp 0.3093
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrlCreator() 0 5 1
A getLoginurl() 0 12 3
A getCanEdit() 0 12 3
A getEdit() 0 11 3
A getDelete() 0 7 3
A getValHtml() 0 12 2
B getHTML() 0 30 4
A __construct() 0 12 1
A getItemDetails() 7 7 1
A getParentDetails() 0 6 1
A getDecode() 0 14 3
A getGravatar() 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
namespace Anax\Comments;
4
5
use \Anax\DI\DIInterface;
6
use \Anax\Comments\Comm;
7
8
/**
9
 * Form to update an item.
10
 */
11
class ShowOneService
12
{
13
    /**
14
    * @var array $commentitem, the chosen comment.
15
    */
16
    protected $comment;
17
    protected $comments;
18
    protected $sess;
19
20
21
    /**
22
     * Constructor injects with DI container and the id to update.
23
     *
24
     * @param Anax\DI\DIInterface $di a service container
25
     * @param integer             $id to show
26
     */
27 1
    public function __construct(DIInterface $di, $id)
28
    {
29 1
        $this->di = $di;
0 ignored issues
show
Bug introduced by
The property di 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...
30 1
        $this->comment = $this->getItemDetails($id);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getItemDetails($id) of type object<Anax\Comments\Comm> is incompatible with the declared type array of property $comment.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
32 1
        $where = "parentid = ?";
33 1
        $params = [$id];
34 1
        $this->comments = $this->getParentDetails($where, $params);
35
36 1
        $session = $this->di->get("session");
37 1
        $this->sess = $session->get("user");
38 1
    }
39
40
41
    /**
42
     * Get details on item to load form with.
43
     *
44
     * @param integer $id get details on item with id.
45
     *
46
     * @return Comm
47
     */
48 1 View Code Duplication
    public function getItemDetails($id)
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...
49
    {
50 1
        $comm = new Comm();
51 1
        $comm->setDb($this->di->get("db"));
52 1
        $comm->find("id", $id);
53 1
        return $comm;
54
    }
55
56
57
        /**
58
     * Get details on item to load form with.
59
     *
60
     * @param string $where
61
     * @param array $params get details on item with id parentid.
62
     *
63
     * @return Comm
64
     */
65 1
    public function getParentDetails($where, $params)
66
    {
67 1
        $comm = new Comm();
68 1
        $comm->setDb($this->di->get("db"));
69 1
        return $comm->findAllWhere($where, $params);
70
    }
71
72
73
    /**
74
     * Sets the callable to use for creating routes.
75
     *
76
     * @param callable $urlCreate to create framework urls.
0 ignored issues
show
Bug introduced by
There is no parameter named $urlCreate. 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...
77
     *
78
     * @return void
79
     */
80
    public function setUrlCreator($route)
81
    {
82
        $url = $this->di->get("url");
83
        return call_user_func([$url, "create"], $route);
84
    }
85
86
87
    /**
88
     * Returns link for gravatar img
89
     *
90
     * @param object $item
91
     * @return string htmlcode
92
     */
93 1
    public function getGravatar($item)
94
    {
95 1
        $comm = new Comm();
96 1
        $gravatar = $comm->getGravatar($item);
97 1
        return '<img src="' . $gravatar . '" alt=""/>';
98
    }
99
100
101
    /**
102
     * Returns json_decoded title and text
103
     * If lead text, headline is larger font
104
     * @param object $item
105
     * @return string htmlcode
106
     */
107 1
    public function getDecode(Comm $item, $lead = null)
108
    {
109 1
        $comt = json_decode($item->comment);
110 1
        if ($comt->frontmatter->title) {
111 1
            $til = $comt->frontmatter->title;
112 1
        } else {
113
            $til = $item->title;
114
        }
115 1
        $comt = $comt->text;
116 1
        if ($lead) {
117
            return '<h3>' . $til . '</h3><p>' . $comt . '</p>';
118
        }
119 1
        return '<h4>' . $til . '</h4><p>' . $comt . '</p>';
120
    }
121
122
123
    /**
124
     * If param met, returns string with edit-links
125
     * @param boolean $isadmin
126
     * @param object $item
127
     * @param string $del
128
     * @return string htmlcode
129
     */
130
    public function getCanEdit(Comm $item, $isadmin, $del)
131
    {
132
        $canedit = "";
133
        $update = $this->setUrlCreator("comm/update");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $update is correct as $this->setUrlCreator('comm/update') (which targets Anax\Comments\ShowOneService::setUrlCreator()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
134
        if ($item->userid == $this->sess['id'] || $isadmin) {
135
            $canedit = '<br /><a href="' . $update . '/' . $item->id . '">Redigera</a>';
136
            $canedit .= ' | <a href="' . $del . '/' . $item->id . '">Ta bort inlägget</a></p>';
137
        } else {
138
            $canedit .= '</p>';
139
        }
140
        return $canedit;
141
    }
142
143
144
    /**
145
     * If session contains correct id, returns string with edit-links
146
     *
147
     * @return string htmlcode
148
     */
149
    public function getLoginurl()
150
    {
151
        $loginurl = $this->setUrlCreator("user/login");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $loginurl is correct as $this->setUrlCreator('user/login') (which targets Anax\Comments\ShowOneService::setUrlCreator()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
152
        $create = $this->setUrlCreator("comm/create");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $create is correct as $this->setUrlCreator('comm/create') (which targets Anax\Comments\ShowOneService::setUrlCreator()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
153
154
        $htmlcomment = '<a href="' . $loginurl . '">Logga in om du vill kommentera</a></p>';
155
156
        if ($this->sess && $this->sess['id']) {
157
            $htmlcomment = '<a href="' . $create . '/' . $this->comment->id . '">Kommentera</a>';
158
        }
159
        return $htmlcomment;
160
    }
161
162
163
    /**
164
     * If loggedin allowed to edit
165
     *
166
     * @param boolean $isadmin
167
     * @param string $userid
168
     * @param string $commid
169
     * @param string $htmlcomment, link
0 ignored issues
show
Documentation introduced by
There is no parameter named $htmlcomment,. Did you maybe mean $htmlcomment?

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...
170
     *
171
     * @return string htmlcode
172
     */
173
    public function getEdit($isadmin, $userid, $commid, $htmlcomment)
174
    {
175
        $update = $this->setUrlCreator("comm/update");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $update is correct as $this->setUrlCreator('comm/update') (which targets Anax\Comments\ShowOneService::setUrlCreator()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
176
        if ($isadmin || $userid == $this->sess['id']) {
177
            $edit = '<p><a href="' . $update . '/' . $commid . '">Redigera</a> | ';
178
            $edit .= $htmlcomment;
179
        } else {
180
            $edit = "<p>" . $htmlcomment . "</p>";
181
        }
182
        return $edit;
183
    }
184
185
186
    /**
187
     * If session contains correct id, returns string with edit-links
188
     *
189
     * @return string htmlcode
190
     */
191
    public function getDelete($isadmin, $userid, $del, $commid)
192
    {
193
        if ($isadmin || $userid == $this->sess['id']) {
194
            $delete = ' | <a href="' .  $del . '/' . $commid . '">Ta bort inlägget</a></p>';
195
        }
196
        return $delete;
0 ignored issues
show
Bug introduced by
The variable $delete does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
197
    }
198
199
200
    /**
201
     * Returns html for each item
202
     *
203
     * @param object $item
204
     * @param string $can
205
     *
206
     * @return string htmlcod
207
     */
208
    public function getValHtml(Comm $item, $can)
209
    {
210
        $gravatar = $this->getGravatar($item->email);
211
        $updated = isset($item->updated) ? '| Uppdaterades: ' . $item->updated : "";
212
        
213
        $text = $this->getDecode($item);
214
        $text .= '<p><span class="smaller">' . $item->email . '</span> ' . $gravatar . '<br />';
215
        $text .= 'Skrevs: ' . $item->created . ' ' . $updated;
216
        $text .= $can;
217
        $text .= '<hr />';
218
        return $text;
219
    }
220
221
222
    /**
223
     * Returns all text for the view
224
     *
225
     * @return string htmlcode
226
     */
227
    public function getHTML()
228
    {
229
        $isadmin = $this->sess['isadmin'] == 1 ? true : false;
230
231
        $del = $this->setUrlCreator("comm/delete");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $del is correct as $this->setUrlCreator('comm/delete') (which targets Anax\Comments\ShowOneService::setUrlCreator()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
232
        $commpage = $this->setUrlCreator("comm");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $commpage is correct as $this->setUrlCreator('comm') (which targets Anax\Comments\ShowOneService::setUrlCreator()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
233
        
234
        $htmlcomment = $this->getLoginurl();
235
        $edit = $this->getEdit($isadmin, $this->comment->userid, $this->comment->id, $htmlcomment);
236
        $delete = $this->getDelete($isadmin, $this->comment->userid, $del, $this->comment->id);
237
238
        $text = '<h3>Kommentarer</h3>';
239
240
        if ($this->comments) {
241
            foreach ($this->comments as $value) {
242
                $can = $this->getCanEdit($value, $isadmin, $del);
243
                $text .= $this->getValHtml($value, $can);
244
            }
245
        }
246
247
        $html = '<div class="col-sm-12 col-xs-12"><div class="col-lg-4 col-sm-7 col-xs-7 abookimg">';
248
        $html .= $this->getDecode($this->comment, true);
0 ignored issues
show
Documentation introduced by
$this->comment is of type array, but the function expects a object<Anax\Comments\Comm>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
        $html .= '<br />' . $edit;
250
        $html .=  $delete;
251
        $html .=  '<p><a href="' . $commpage . '">Till Alla Inläggen</a></p>';
252
        $html .=  '</div><div class="col-sm-5 col-xs-5">';
253
        $html .= $text;
254
        $html .= '</div></div>';
255
        return $html;
256
    }
257
}
258