Completed
Push — master ( e832f1...3f2565 )
by Tomas
02:13
created

Message   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
A setMessage() 0 9 2
1
<?php
2
3
namespace Tosj\Message;
4
5
class Message
6
{
7
    private $message = null;
8
    private $test;
9
10
    public function __construct(bool $test = false)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SESSION 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...
11
    {
12
        $this->test = $test;
13
14
        // Save to object and free Session
15
        if (session_status() === PHP_SESSION_ACTIVE) {
16
            $this->message = (isset($_SESSION['message']) && $_SESSION['message']) ? $_SESSION['message'] : null;
17
            unset($_SESSION['message']);
18
        }
19
    }
20
21
22
    public function setMessage($thisMessage)
0 ignored issues
show
Coding Style introduced by
setMessage uses the super-global variable $_SESSION 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...
23
    {
24
        if ($this->test) {
25
            $this->message[] = $thisMessage;
26
        } else {
27
            // Save new messages to session
28
            $_SESSION['message'][] = $thisMessage;
29
        }
30
    }
31
32
33
    public function getMessage()
34
    {
35
        $html = null;
36
        // Print messages
37
        if ($this->message && $this->message !== '') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->message of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            $html = '<div id="messagebox">';
39
            foreach ($this->message as $msg)
40
            {
41
                $html .= '
42
                <div class="message '.htmlentities($msg["type"]).'">
43
                    <p>'.htmlentities($msg["msg"]).'</p>
44
                </div>';
45
            }
46
            $html .= '</div>';
47
        }
48
        return $html;
49
    }
50
}
51