Completed
Push — master ( 932e32...be0a32 )
by Mikael
02:35
created

SampleController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 199
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 1
dl 199
loc 199
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 5 5 1
A indexAction() 5 5 1
A dumpDiActionGet() 6 6 1
A infoActionGet() 5 5 1
A createActionGet() 5 5 1
A createActionPost() 5 5 1
A argumentActionGet() 5 5 1
A defaultArgumentActionGet() 5 5 1
A typedArgumentActionGet() 5 5 1
A variadicActionGet() 5 5 1
A catchAll() 6 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\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 */
19 View Code Duplication
class SampleController implements ContainerInjectableInterface
0 ignored issues
show
Duplication introduced by
This class 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...
20
{
21
    use ContainerInjectableTrait;
22
23
24
25
    /**
26
     * @var string $db a sample member variable that gets initialised
27
     */
28
    private $db = "not active";
29
30
31
32
    /**
33
     * The initialize method is optional and will always be called before the
34
     * target method/action. This is a convienient method where you could
35
     * setup internal properties that are commonly used by several methods.
36
     *
37
     * @return void
38
     */
39
    public function initialize() : void
40
    {
41
        // Use to initialise member variables.
42
        $this->db = "active";
43
    }
44
45
46
47
    /**
48
     * This is the index method action, it handles:
49
     * ANY METHOD mountpoint
50
     * ANY METHOD mountpoint/
51
     * ANY METHOD mountpoint/index
52
     *
53
     * @return string
54
     */
55
    public function indexAction() : string
56
    {
57
        // Deal with the action and return a response.
58
        return __METHOD__ . ", \$db is {$this->db}";
59
    }
60
61
62
63
    /**
64
     * This sample method dumps the content of $di.
65
     * GET mountpoint/dump-app
66
     *
67
     * @return string
68
     */
69
    public function dumpDiActionGet() : string
70
    {
71
        // Deal with the action and return a response.
72
        $services = implode(", ", $this->di->getServices());
73
        return __METHOD__ . "<p>\$di contains: $services";
74
    }
75
76
77
78
    /**
79
     * Add the request method to the method name to limit what request methods
80
     * the handler supports.
81
     * GET mountpoint/info
82
     *
83
     * @return string
84
     */
85
    public function infoActionGet() : string
86
    {
87
        // Deal with the action and return a response.
88
        return __METHOD__ . ", \$db is {$this->db}";
89
    }
90
91
92
93
    /**
94
     * This sample method action it the handler for route:
95
     * GET mountpoint/create
96
     *
97
     * @return string
98
     */
99
    public function createActionGet() : string
100
    {
101
        // Deal with the action and return a response.
102
        return __METHOD__ . ", \$db is {$this->db}";
103
    }
104
105
106
107
    /**
108
     * This sample method action it the handler for route:
109
     * POST mountpoint/create
110
     *
111
     * @return string
112
     */
113
    public function createActionPost() : string
114
    {
115
        // Deal with the action and return a response.
116
        return __METHOD__ . ", \$db is {$this->db}";
117
    }
118
119
120
121
    /**
122
     * This sample method action takes one argument:
123
     * GET mountpoint/argument/<value>
124
     *
125
     * @param mixed $value
126
     *
127
     * @return string
128
     */
129
    public function argumentActionGet($value) : string
130
    {
131
        // Deal with the action and return a response.
132
        return __METHOD__ . ", \$db is {$this->db}, got argument '$value'";
133
    }
134
135
136
137
    /**
138
     * This sample method action takes zero or one argument and you can use - as a separator which will then be removed:
139
     * GET mountpoint/defaultargument/
140
     * GET mountpoint/defaultargument/<value>
141
     * GET mountpoint/default-argument/
142
     * GET mountpoint/default-argument/<value>
143
     *
144
     * @param mixed $value with a default string.
145
     *
146
     * @return string
147
     */
148
    public function defaultArgumentActionGet($value = "default") : string
149
    {
150
        // Deal with the action and return a response.
151
        return __METHOD__ . ", \$db is {$this->db}, got argument '$value'";
152
    }
153
154
155
156
    /**
157
     * This sample method action takes two typed arguments:
158
     * GET mountpoint/typed-argument/<string>/<int>
159
     *
160
     * NOTE. Its recommended to not use int as type since it will still
161
     * accept numbers such as 2hundred givving a PHP NOTICE. So, its better to
162
     * deal with type check within the action method and throuw exceptions
163
     * when the expected type is not met.
164
     *
165
     * @param mixed $value with a default string.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. 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...
166
     *
167
     * @return string
168
     */
169
    public function typedArgumentActionGet(string $str, int $int) : string
170
    {
171
        // Deal with the action and return a response.
172
        return __METHOD__ . ", \$db is {$this->db}, got string argument '$str' and int argument '$int'.";
173
    }
174
175
176
177
    /**
178
     * This sample method action takes a variadic list of arguments:
179
     * GET mountpoint/variadic/
180
     * GET mountpoint/variadic/<value>
181
     * GET mountpoint/variadic/<value>/<value>
182
     * GET mountpoint/variadic/<value>/<value>/<value>
183
     * etc.
184
     *
185
     * @param array $value as a variadic parameter.
186
     *
187
     * @return string
188
     */
189
    public function variadicActionGet(...$value) : string
190
    {
191
        // Deal with the action and return a response.
192
        return __METHOD__ . ", \$db is {$this->db}, got '" . count($value) . "' arguments: " . implode(", ", $value);
193
    }
194
195
196
197
    /**
198
     * Adding an optional catchAll() method will catch all actions sent to the
199
     * router. YOu can then reply with an actual response or return void to
200
     * allow for the router to move on to next handler.
201
     * A catchAll() handles the following, if a specific action method is not
202
     * created:
203
     * ANY METHOD mountpoint/**
204
     *
205
     * @param array $args as a variadic parameter.
206
     *
207
     * @return mixed
208
     *
209
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
210
     */
211
    public function catchAll(...$args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
212
    {
213
        // Deal with the request and send an actual response, or not.
214
        //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args);
215
        return;
216
    }
217
}
218