Completed
Push — erdiko2 ( bc4546...cc200f )
by John
03:59
created

Examples   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 205
Duplicated Lines 56.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 115
loc 205
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 17 17 1
A getOnecolumn() 9 9 1
A getTwocolumn() 9 9 1
A getThreecolumn() 9 9 1
A getJohn() 13 13 1
A getMarkup() 13 13 1
A getCarousel() 13 13 1
A getTheme() 0 9 1
A getTheme2() 0 11 1
A getFlash() 0 19 1
B getGrid() 4 31 2
A getFullpage() 13 13 1
A getException() 15 15 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
namespace app\controllers;
3
4
class Examples extends \erdiko\controllers\Web
5
{
6
    use \erdiko\theme\traits\Controller; // Add theme engine suport (for convenience)
7
8 View Code Duplication
    public function get($request, $response, $args)
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...
9
    {
10
        // $this->container->logger->debug("examples");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
11
        $view = 'examples/list.html';
12
13
        // Get erdiko config, this gets application.json and loads the theme specified
14
        // $themeData = \erdiko\theme\Config::get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
16
        // $themeData['args'] = $args; // optional
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
17
18
        $themeData['page'] = [
19
            'title' => "Erdiko Web Example",
20
            'hello' => "world"
21
            ];
22
23
        return $this->container->theme->render($response, $view, $themeData);
24
    }
25
26 View Code Duplication
    public function getOnecolumn($request, $response, $args)
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...
27
    {
28
        $this->container->logger->debug("route: /config");
29
        $view = 'layouts/1column.html';
30
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
31
        $themeData['page']['title'] = "1 Column Layout";
32
33
        return $this->container->theme->render($response, $view, $themeData);
34
    }
35
36 View Code Duplication
    public function getTwocolumn($request, $response, $args)
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...
37
    {
38
        $this->container->logger->debug("route: /config");
39
        $view = 'layouts/2column.html';
40
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
41
        $themeData['page']['title'] = "2 Column Layout";
42
43
        return $this->container->theme->render($response, $view, $themeData);
44
    }
45
46 View Code Duplication
    public function getThreecolumn($request, $response, $args)
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...
47
    {
48
        $this->container->logger->debug("route: /config");
49
        $view = 'layouts/3column.html';
50
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
51
        $themeData['page']['title'] = "3 Column Layout";
52
53
        return $this->container->theme->render($response, $view, $themeData);
54
    }
55
56 View Code Duplication
    public function getJohn($request, $response, $args)
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...
57
    {
58
        $view = 'pages/example.html';
59
60
        // Get erdiko config, this gets application.json and loads the theme specified
61
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
62
        $themeData['args'] = $args; // optional
63
        $themeData['page'] = [
64
            'title' => "Erdiko Web Example"
65
            ];
66
67
        return $this->container->theme->render($response, $view, $themeData);
68
    }
69
70 View Code Duplication
    public function getMarkup($request, $response, $args)
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...
71
    {
72
        $view = 'examples/markup.html';
73
74
        // Get erdiko config, this gets application.json and loads the theme specified
75
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
76
        $themeData['args'] = $args; // optional
77
        $themeData['page'] = [
78
            'title' => "Markup Example"
79
            ];
80
81
        return $this->container->theme->render($response, $view, $themeData);
82
    }
83
84 View Code Duplication
    public function getCarousel($request, $response, $args)
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...
85
    {
86
        $view = 'examples/carousel.html';
87
88
        // Get erdiko config, this gets application.json and loads the theme specified
89
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90
        $themeData['page'] = [
91
            'title' => "Fullpage Example",
92
            'description' => "This is the description of the page."
93
            ];
94
95
        return $this->container->theme->render($response, $view, $themeData);
96
    }
97
98
    /**
99
     * Leverage the theme trait to easily add content to an action
100
     */
101
    public function getTheme($request, $response, $args)
102
    {
103
        $this->getThemeEngine();
104
        $this->theme->title = "Theme Engine Example";
105
        $this->theme->description = "This page is rendered using the erdiko theme engine.
106
            \\erdiko\\theme\\Engine";
107
108
        return $this->render($response, null, $theme);
0 ignored issues
show
Bug introduced by
The variable $theme does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
109
    }
110
111
    /**
112
     * Alternative way to use the theme Engine (explicit)
113
     * In this approach it does not rely on the theme trait
114
     */
115
    public function getTheme2($request, $response, $args)
116
    {
117
        $theme = new \erdiko\theme\Engine( $this->container->get('settings')['theme'] );
0 ignored issues
show
Unused Code introduced by
The call to Engine::__construct() has too many arguments starting with $this->container->get('settings')['theme'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
118
        $theme->title = "Theme Engine Example";
119
        $theme->description = "This page is rendered with the erdiko theme engine.
120
            It does not leverage the erdiko theme trait.
121
            \\erdiko\\theme\\Engine";
122
123
        return $this->container->theme->render($response, $theme->getDefaultView(), $theme->toArray());
124
        // return $this->render($response, null, $theme);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
    }
126
127
    public function getFlash($request, $response, $args)
128
    {
129
        $view = 'page.html';
130
131
        // Add some flash messages
132
        $this->container->flash->addMessage('success', 'This is a success message');
133
        $this->container->flash->addMessage('info', 'This is an info message');
134
        $this->container->flash->addMessage('warning', 'This is a warning message');
135
        $this->container->flash->addMessage('danger', 'This is a danger (error) message');
136
137
        // Get erdiko config, this gets application.json and loads the theme specified
138
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
139
        $themeData['args'] = $args;
140
        $themeData['page'] = [
141
            'title' => "Flash Message Example"
142
            ];
143
144
        return $this->container->theme->render($response, $view, $themeData);
145
    }
146
147
    public function getGrid($request, $response, $args)
148
    {
149
        $this->container->logger->debug("/controller");
150
        $view = 'examples/grid.html';
151
152
        // Get erdiko config, this gets application.json and loads the theme specified
153
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
154
        // $this->container->logger->debug("config: ".print_r($config, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
156
        // Generate data for grid
157
        $item = [
158
            'url' => "#",
159
            'image' => "/images/grid-item.png",
160
            'name' => "Item"
161
        ];
162
        $items = array();
163
        $max = (int)$args['param'];
164
        $this->container->logger->debug("param: ".$max);
165
166 View Code Duplication
        for($i = 0; $i < $max; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
167
            $item['name'] = "Item $i";
168
            $items[] = $item;
169
        }
170
171
        $themeData['page'] = [
172
            'title' => "Grid Example",
173
            'items' => $items
174
            ];
175
176
        return $this->container->theme->render($response, $view, $themeData);
177
    }
178
179 View Code Duplication
    public function getFullpage($request, $response, $args)
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...
180
    {
181
        $view = 'fullpage.html';
182
183
        // Get erdiko config, this gets application.json and loads the theme specified
184
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$themeData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $themeData = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
185
        $themeData['page'] = [
186
            'title' => "Fullpage Example",
187
            'description' => "This is the description of the page."
188
            ];
189
190
        return $this->container->theme->render($response, $view, $themeData);
191
    }
192
193 View Code Duplication
    public function getException($request, $response, $args)
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...
194
    {
195
        $view = 'page.html';
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
196
197
        throw new \Exception("This is an exception");
198
199
        // Get erdiko config, this gets application.json and loads the theme specified
200
        $themeData['theme'] = \erdiko\theme\Config::get($this->container->get('settings')['theme']);
0 ignored issues
show
Unused Code introduced by
// Get erdiko config, th...('settings')['theme']); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
201
        $themeData['page'] = [
202
            'title' => "Fullpage Example",
203
            'description' => "This is the description of the page."
204
            ];
205
206
        return $this->container->theme->render($response, $view, $themeData);
207
    }
208
}
209