Passed
Push — master ( ceaca3...579756 )
by Kevin Olguin
01:46
created

Com2Delete   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 88
Duplicated Lines 7.95 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84.78%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 7
loc 88
ccs 39
cts 46
cp 0.8478
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 8 1
A isgetItemDetails() 7 7 1
A __construct() 0 45 3

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 Kifekenny\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Kifekenny\Comment\Comment;
8
9
/**
10
 * Form to update an item.
11
 */
12
class Com2Delete 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
0 ignored issues
show
Bug introduced by
There is no parameter named $di. 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...
18
     * @param integer             $id to update
19
     */
20 2
    public function __construct(DIInterface $dis, $id)
21
    {
22 2
        parent::__construct($dis);
23 2
        $currId = $this->di->get("session")->get("user_id");
24 2
        $comment2 = $this->isgetItemDetails($id);
25 2
        if ($currId != 1) {
26 2
            if ($comment2->userId != $currId) {
27 2
                $this->di->get("response")->redirect("");
28 2
            }
29 2
        }
30 2
        $this->form->create(
31
            [
32 2
                "id" => __CLASS__,
33 2
                "legend" => "Update details of the book",
34 2
            ],
35
            [
36
                "id" => [
37 2
                    "type" => "hidden",
38 2
                    "value" => $comment2->id,
39 2
                ],
40
41
                "Title" => [
42 2
                    "type" => "text",
43 2
                    "validation" => ["not_empty"],
44 2
                    "readonly" => true,
45 2
                    "value" => $comment2->title,
46 2
                ],
47
48
                "Content" => [
49 2
                    "type" => "textarea",
50 2
                    "validation" => ["not_empty"],
51 2
                    "value" => $comment2->content,
52 2
                    "readonly" => true,
53 2
                    "class" => "editText",
54 2
                ],
55
56
                "submit" => [
57 2
                    "type" => "submit",
58 2
                    "value" => "Delete",
59 2
                    "class" => "red",
60 2
                    "callback" => [$this, "callbackSubmit"]
61 2
                ],
62
            ]
63 2
        );
64 2
    }
65
66
67
68
    /**
69
     * Get details on item to load form with.
70
     *
71
     * @param integer $id get details on item with id.
72
     *
73
     * @return boolean true if okey, false if something went wrong.
74
     */
75 2 View Code Duplication
    public function isgetItemDetails($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...
76
    {
77 2
        $comment2 = new Comment();
78 2
        $comment2->setDb($this->di->get("db"));
79 2
        $comment2->find("id", $id);
80 2
        return $comment2;
81
    }
82
83
84
85
    /**
86
     * Callback for submit-button which should return true if it could
87
     * carry out its work and false if something failed.
88
     *
89
     * @return boolean true if okey, false if something went wrong.
90
     */
91
    public function callbackSubmit()
92
    {
93
        $comment = new Comment();
94
        $comment->setDb($this->di->get("db"));
95
        $comment->find("id", $this->form->value("id"));
96
        $comment->delete();
97
        $this->di->get("response")->redirect("");
98
    }
99
}
100