Test Failed
Push — master ( 194522...eda1b6 )
by Mikael
01:43
created

FormModelCheckboxMultiple   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 24.19 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 15
loc 62
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A callbackSubmit() 15 15 1
A callbackSubmitFail() 0 6 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\HTMLForm;
4
5
/**
6
 * Example of FormModel implementation.
7
 */
8
class FormModelCheckboxMultiple extends FormModel
9
{
10
    /**
11
     * Constructor
12
     */
13
    public function __construct()
14
    {
15
        parent::__construct(
16
            [
17
                "id" => __CLASS__,
18
            ],
19
            [
20
                "items" => [
21
                    "type"        => "checkbox-multiple",
22
                    "values"      => ["tomato", "potato", "apple", "pear", "banana"],
23
                    "checked"     => ["potato", "pear"],
24
                ],
25
                "submit" => [
26
                    "type"      => "submit",
27
                    "callback"  => [$this, "callbackSubmit"],
28
                ],
29
                "submit-fail" => [
30
                    "type"      => "submit",
31
                    "callback"  => [$this, "callbackSubmitFail"],
32
                ],
33
            ]
34
        );
35
    }
36
37
38
39
    /**
40
     * Callback for submit-button.
41
     */
42 View Code Duplication
    public function callbackSubmit()
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...
43
    {
44
        $this->AddOutput("<p>#callbackSubmit()</p>");
45
        
46
        // Get the selected items as an array
47
        $items = $this->value("items");
48
        $itemsAsString = implode(", ", $items);
49
        $this->AddOutput("<p>Selected items are: '$itemsAsString'.");
50
51
        $this->AddOutput("<pre>" . print_r($_POST, 1) . "</pre>");
52
        $this->AddOutput("<pre>" . print_r($this["items"], 1) . "</pre>");
53
        $this->saveInSession = true;
0 ignored issues
show
Bug introduced by
The property saveInSession does not seem to exist. Did you mean session?

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...
54
        
55
        return true;
56
    }
57
58
59
60
    /**
61
     * Callback for submit-button.
62
     */
63
    public function callbackSubmitFail()
64
    {
65
        $this->AddOutput("<p>#callbackSubmitFail()</p>");
66
        $this->AddOutput("<p>Form was submitted but I failed to process/save/validate it</p>");
67
        return false;
68
    }
69
}
70