DeleteCommForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 8.24 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 7
loc 85
ccs 26
cts 44
cp 0.5909
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B callbackSubmit() 0 22 4
B __construct() 0 31 1
A getCommDetails() 7 7 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\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\Comments\Comm;
8
9
/**
10
 * Form to delete an item.
11
 */
12
class DeleteCommForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di, $commid)
20
    {
21 1
        parent::__construct($di);
22 1
        $comm = $this->getCommDetails($commid);
23
24 1
        $this->form->create(
25
            [
26 1
                "id" => __CLASS__,
27 1
                "legend" => "Alla kommentarer tas också bort",
28 1
            ],
29
            [
30
                "userid" => [
31 1
                    "type"        => "hidden",
32 1
                    "value"       => $comm->userid,
33 1
                ],
34
35
                "id" => [
36 1
                    "type"        => "text",
37 1
                    "readonly"    => true,
38 1
                    "value"       => $commid,
39 1
                    "label"       => "Ta bort inlägget",
40 1
                ],
41
42
                "submit" => [
43 1
                    "type" => "submit",
44 1
                    "value" => "Ta bort",
45 1
                    "callback" => [$this, "callbackSubmit"]
46 1
                ],
47
            ]
48 1
        );
49 1
    }
50
51
52
53
    /**
54
     * Get all items as array suitable for display in select option dropdown.
55
     *
56
     * @return array with key value of all items.
57
     */
58 1 View Code Duplication
    protected 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...
59
    {
60 1
        $comm = new Comm();
61 1
        $comm->setDb($this->di->get("db"));
62 1
        $comm->find("id", $id);
63 1
        return $comm;
64
    }
65
66
67
68
    /**
69
     * Callback for submit-button which should return true if it could
70
     * carry out its work and false if something failed.
71
     *
72
     * @return boolean true if okey, false if something went wrong.
73
     */
74
    public function callbackSubmit()
75
    {
76
        $comm = new Comm();
77
        $comm->setDb($this->di->get("db"));
78
79
        $all = $comm->findAll();
80
        $objects = "";
81
82
        foreach ($all as $obj) {
83
            if ($obj->parentid && $obj->parentid == $this->form->value("id")) {
84
                $comm->find("id", $obj->id);
85
                $objects .= $comm->title . ", ";
86
                $comm->delete();
87
            }
88
        }
89
90
        $comm->find("id", $this->form->value("id"));
91
        $objects .= $comm->title;
92
        $comm->delete();
93
        $pagerender = $this->di->get("pageRender");
94
        $pagerender->redirect("comm");
95
    }
96
}
97