Completed
Push — master ( d6d75c...5451f5 )
by Magnus
02:30
created

CreateCommentForm   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 67
Duplicated Lines 40.3 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 4
dl 27
loc 67
ccs 17
cts 34
cp 0.5
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 27 27 1
B callbackSubmit() 0 25 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 Radchasay\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\Comment;
8
use \Radchasay\User\User;
9
10
/**
11
 * Example of FormModel implementation.
12
 */
13
class CreateCommentForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container.
17
     *
18
     * @param Anax\DI\DIInterface $di a service container
19
     */
20 2 View Code Duplication
    public function __construct(DIInterface $di, $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...
21
    {
22 2
        parent::__construct($di);
23
24 2
        $this->form->create(
25
            [
26 2
                "id"     => __CLASS__,
27 2
                "legend" => "Create new comment",
28 2
            ],
29 1
            [
30
                    "text"    => [
31
                        "type"     => "textarea"
32 2
                    ],
33
34
                    "hidden" => [
35 2
                        "type"       => "hidden",
36 2
                        "value"      => $id,
37 2
                    ],
38
39
                    "submit" => [
40 2
                        "type"     => "submit",
41 2
                        "value"    => "Create comment",
42 2
                        "callback" => [$this, "callbackSubmit"],
43 2
                    ],
44
            ]
45 2
        );
46 2
    }
47
48
    /**
49
     * Callback for submit-button which should return true if it could
50
     * carry out its work and false if something failed.
51
     *
52
     * @return boolean true if okey, false if something went wrong.
53
     */
54
    public function callbackSubmit()
55
    {
56
        // Get values from the submitted form
57
58
        $comment = new Comment();
59
        $comment->setDB($this->di->get("db"));
60
        $data = $this->form->value("text");
61
        $text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]);
62
        $comment->commenttext = $text;
63
        $comment->idpost = $this->form->value("hidden");
64
        $comment->postuser = $this->di->get("session")->get("email");
65
66
        $user = new User();
67
        $user->setDb($this->di->get("db"));
68
        $user->getInformation($comment->postuser);
69
        $user->points += 1;
0 ignored issues
show
Bug introduced by
The property points does not seem to exist in Radchasay\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
71
72
        $comment->save();
73
        $user->save();
74
75
        $url = $this->di->get("url")->create("comment/retrieve/$comment->idpost");
76
        $this->di->get("response")->redirect($url);
77
        return true;
78
    }
79
}
80