ShowOneService::getHTML()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 30
ccs 0
cts 23
cp 0
rs 8.5806
c 2
b 0
f 0
cc 4
eloc 21
nc 4
nop 0
crap 20
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 1
    public function setUrlCreator($route)
81
    {
82 1
        $url = $this->di->get("url");
83 1
        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 1
    public function getCanEdit(Comm $item, $isadmin, $del)
131
    {
132 1
        $canedit = "";
133 1
        $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 1
        if ($item->userid == $this->sess['id'] || $isadmin) {
135 1
            $canedit = '<br /><a href="' . $update . '/' . $item->id . '">Redigera</a>';
136 1
            $canedit .= ' | <a href="' . $del . '/' . $item->id . '">Ta bort inlägget</a></p>';
137 1
        } else {
138
            $canedit .= '</p>';
139
        }
140 1
        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
        $delete = "";
194
        if ($isadmin || $userid == $this->sess['id']) {
195
            $delete = ' | <a href="' .  $del . '/' . $commid . '">Ta bort inlägget</a></p>';
196
        }
197
        return $delete;
198
    }
199
200
201
    /**
202
     * Returns html for each item
203
     *
204
     * @param object $item
205
     * @param string $can
206
     *
207
     * @return string htmlcod
208
     */
209 1
    public function getValHtml(Comm $item, $can)
210
    {
211 1
        $gravatar = $this->getGravatar($item->email);
212 1
        $updated = isset($item->updated) ? '| Uppdaterades: ' . $item->updated : "";
213
        
214 1
        $text = $this->getDecode($item);
215 1
        $text .= '<p><span class="smaller">' . $item->email . '</span> ' . $gravatar . '<br />';
216 1
        $text .= 'Skrevs: ' . $item->created . ' ' . $updated;
217 1
        $text .= $can;
218 1
        $text .= '<hr />';
219 1
        return $text;
220
    }
221
222
223
    /**
224
     * Returns all text for the view
225
     *
226
     * @return string htmlcode
227
     */
228
    public function getHTML()
229
    {
230
        $isadmin = $this->sess['isadmin'] == 1 ? true : false;
231
232
        $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...
233
        $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...
234
        
235
        $htmlcomment = $this->getLoginurl();
236
        $edit = $this->getEdit($isadmin, $this->comment->userid, $this->comment->id, $htmlcomment);
237
        $delete = $this->getDelete($isadmin, $this->comment->userid, $del, $this->comment->id);
238
239
        $text = '<h3>Kommentarer</h3>';
240
241
        if ($this->comments) {
242
            foreach ($this->comments as $value) {
243
                $can = $this->getCanEdit($value, $isadmin, $del);
244
                $text .= $this->getValHtml($value, $can);
245
            }
246
        }
247
248
        $html = '<div class="col-sm-12 col-xs-12"><div class="col-lg-4 col-sm-7 col-xs-7 abookimg">';
249
        $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...
250
        $html .= '<br />' . $edit;
251
        $html .=  $delete;
252
        $html .=  '<p><a href="' . $commpage . '">Till Alla Inläggen</a></p>';
253
        $html .=  '</div><div class="col-sm-5 col-xs-5">';
254
        $html .= $text;
255
        $html .= '</div></div>';
256
        return $html;
257
    }
258
}
259