FormController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 106
Duplicated Lines 9.43 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 2
dl 10
loc 106
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 41 1
A callbackSubmit() 10 10 1
A callbackSubmitFail() 0 5 1
A callbackSuccess() 0 5 1
A callbackFail() 0 5 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
 * Anax base class for wrapping sessions.
7
 *
8
 */
9
class FormController
10
{
11
    use \Anax\DI\TInjectionAware,
12
        \Anax\MVC\TRedirectHelpers;
13
14
15
16
    /**
17
     * Index action.
18
     *
19
     */
20
    public function indexAction()
21
    {
22
        $this->di->session(); // Will load the session service which also starts the session
23
24
        $form = $this->di->form->create([], [
25
            'name' => [
26
                'type'        => 'text',
27
                'label'       => 'Name of contact person:',
28
                'required'    => true,
29
                'validation'  => ['not_empty'],
30
            ],
31
            'email' => [
32
                'type'        => 'text',
33
                'required'    => true,
34
                'validation'  => ['not_empty', 'email_adress'],
35
            ],
36
            'phone' => [
37
                'type'        => 'text',
38
                'required'    => true,
39
                'validation'  => ['not_empty', 'numeric'],
40
            ],
41
            'submit' => [
42
                'type'      => 'submit',
43
                'callback'  => [$this, 'callbackSubmit'],
44
            ],
45
            'submit-fail' => [
46
                'type'      => 'submit',
47
                'callback'  => [$this, 'callbackSubmitFail'],
48
            ],
49
        ]);
50
51
52
        // Check the status of the form
53
        $form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']);
54
55
        $this->di->theme->setTitle("Testing CForm with Anax");
56
        $this->di->views->add('default/page', [
57
            'title' => "Try out a form using CForm",
58
            'content' => $form->getHTML()
59
        ]);
60
    }
61
62
63
64
    /**
65
     * Callback for submit-button.
66
     *
67
     */
68 View Code Duplication
    public function callbackSubmit($form)
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...
69
    {
70
        $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>");
71
        $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>");
72
        $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>");
73
        $form->AddOutput("<p><b>Email: " . $form->Value('email') . "</b></p>");
74
        $form->AddOutput("<p><b>Phone: " . $form->Value('phone') . "</b></p>");
75
        $form->saveInSession = true;
76
        return true;
77
    }
78
79
80
81
    /**
82
     * Callback for submit-button.
83
     *
84
     */
85
    public function callbackSubmitFail($form)
86
    {
87
        $form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>");
88
        return false;
89
    }
90
91
92
93
    /**
94
     * Callback What to do if the form was submitted?
95
     *
96
     */
97
    public function callbackSuccess($form)
98
    {
99
        $form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>");
100
        $this->redirectTo();
101
    }
102
103
104
105
    /**
106
     * Callback What to do when form could not be processed?
107
     *
108
     */
109
    public function callbackFail($form)
110
    {
111
        $form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>");
112
        $this->redirectTo();
113
    }
114
}
115