Completed
Push — erdiko2 ( 719271...84f584 )
by John
05:49
created

Session::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
namespace app\controllers\examples;
3
4
class Session extends \erdiko\controllers\Web
5
{
6
    use \erdiko\theme\traits\Controller; // Add theme engine suport (for convenience)
7
8
    public function get($request, $response, $args)
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
    /**
27
     * Test the session
28
     */
29 View Code Duplication
    public function getSession($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...
Coding Style introduced by
getSession uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
30
    {
31
        $this->getThemeEngine();
32
        $this->theme->title = "Session Test";
33
34
        $value = (isset($_GET["index"])) ? $_GET["index"] : \erdiko\session\Session::get('index');
0 ignored issues
show
Bug introduced by
The call to get() misses a required argument $expired.

This check looks for function calls that miss required arguments.

Loading history...
35
        \erdiko\session\Session::set('index', $value);
36
37
        // $this->container->logger->debug("session::index = ".\erdiko\session\Session::get('index'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
38
        $this->theme->description = "Session value: ".\erdiko\session\Session::get('index');
0 ignored issues
show
Bug introduced by
The call to get() misses a required argument $expired.

This check looks for function calls that miss required arguments.

Loading history...
39
40
        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...
41
    }
42
43 View Code Duplication
    public function getFlash($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...
44
    {
45
        $view = 'page.html';
46
47
        // Add some flash messages
48
        $this->container->flash->addMessage('success', 'This is a success message');
49
        $this->container->flash->addMessage('info', 'This is an info message');
50
        $this->container->flash->addMessage('warning', 'This is a warning message');
51
        $this->container->flash->addMessage('danger', 'This is a danger (error) message');
52
53
        // Get erdiko config, this gets application.json and loads the theme specified
54
        $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...
55
        $themeData['args'] = $args;
56
        $themeData['page'] = [
57
            'title' => "Flash Message Example"
58
            ];
59
60
        return $this->container->theme->render($response, $view, $themeData);
61
    }
62
63
}
64