UpdateCommForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 111
Duplicated Lines 7.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 55.77%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 3
dl 8
loc 111
ccs 29
cts 52
cp 0.5577
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A decode() 0 8 1
A aForm() 0 14 1
A getCommDetails() 7 7 1
B callbackSubmit() 0 33 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
namespace Anax\Comments\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\Comments\Comm;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class UpdateCommForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20 2
    public function __construct(DIInterface $di, $id, $sessid)
21
    {
22 2
        parent::__construct($di);
23 2
        $comm = $this->getCommDetails($id);
24
25 2
        $comt = $this->decode($comm->comment);
26 2
        $this->aForm($id, $sessid, $comm, $comt);
27 2
    }
28
29
30
    /**
31
    * Converts json-string back to variables
32
    *
33
    * @param string $fromjson the jsoncode
34
    * @return the extracted comment-text
35
    */
36 2
    public function decode($fromjson)
37
    {
38 2
        $textfilter = $this->di->get("textfilter");
39 2
        $toparse = json_decode($fromjson);
40 2
        $comt = $textfilter->parse($toparse->text, ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"]);
41 2
        $comt = strip_tags($comt->text);
42 2
        return $comt;
43
    }
44
45
    /**
46
     * Create the form.
47
     *
48
     */
49 2
    public function aForm($id, $sessid, $comm, $comt)
50
    {
51 2
        $this->form->create(
52 2
            ["id" => __CLASS__, "legend" => "Uppdatera ditt konto"],
53 2
            [   "sessid" => ["type"  => "hidden", "value" => $sessid],
54 2
                "id" => ["type"  => "hidden", "value" => $id],
55 2
                "parentid" => ["type"  => "hidden", "value" => $comm->parentid],
56 2
                "title" => ["type" => "text", "validation" => ["not_empty"], "value" => $comm->title],
57 2
                "comment" => ["type" => "textarea", "validation" => ["not_empty"], "value" => $comt],
58 2
                "submit" => ["type" => "submit", "value" => "Spara", "callback" => [$this, "callbackSubmit"]],
59 2
                "reset" => ["type"      => "reset"],
60
            ]
61 2
        );
62 2
    }
63
64
65
66
    /**
67
     * Get details on item to load form with.
68
     *
69
     * @param integer $id get details on item with id.
70
     *
71
     * @return Comm
72
     */
73 2 View Code Duplication
    public function getCommDetails($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...
74
    {
75 2
        $comm = new Comm();
76 2
        $comm->setDb($this->di->get("db"));
77 2
        $comm->find("id", $id);
78 2
        return $comm;
79
    }
80
81
82
83
    /**
84
     * Callback for submit-button which should return true if it could
85
     * carry out its work and false if something failed.
86
     *
87
     * @return boolean true if okey, false if something went wrong.
88
     */
89
    public function callbackSubmit()
90
    {
91
        $now = date("Y-m-d H:i:s");
92
93
        $textfilter = $this->di->get("textfilter");
94
95
        $userController = $this->di->get("userController");
96
        $userdetails = $userController->getOne($this->form->value("sessid"));
97
98
        $parses = ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"];
99
        $comment = $textfilter->parse($this->form->value("comment"), $parses);
100
        $comment->frontmatter['title'] = $this->form->value("title");
101
        $comment = json_encode($comment);
102
103
        $comm = new Comm();
104
        $comm->setDb($this->di->get("db"));
105
        $comm->find("id", $this->form->value("id"));
106
        $comm->updated = $now;
107
        $comm->title = $this->form->value("title");
108
        $comm->userid = $this->form->value("sessid");
109
        $comm->comment = $comment;
110
        $comm->email = $userdetails["email"];
111
        $comm->save();
112
113
        $parentid = (int)$this->form->value("parentid");
114
115
        $back = $parentid > 0 ? "/view-one/" . $parentid : "/view-one/" . $this->form->value("id");
116
117
        $pagerender = $this->di->get("pageRender");
118
        $pagerender->redirect("comm" . $back);
119
120
        return true;
121
    }
122
}
123