Message   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 46
ccs 21
cts 25
cp 0.84
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
A setMessage() 0 9 2
A getMessage() 0 17 4
1
<?php
2
3
namespace Tosj\Message;
4
5
class Message
6
{
7
    private $message = null;
8
    private $test;
9
10 1
    public function __construct($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 1
        $this->test = $test;
13
14
        // Save to object and free Sessions
15 1
        if (session_status() === PHP_SESSION_ACTIVE) {
16
            $this->message = (isset($_SESSION['message']) && $_SESSION['message']) ? $_SESSION['message'] : null;
17
            unset($_SESSION['message']);
18
        }
19 1
    }
20
21
22 1
    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 1
        if ($this->test) {
25 1
            $this->message[] = $thisMessage;
26 1
        } else {
27
            // Save new messages to session
28
            $_SESSION['message'][] = $thisMessage;
29
        }
30 1
    }
31
32
33 1
    public function getMessage()
34
    {
35 1
        $html = null;
36
        // Print messages
37 1
        if (!empty($this->message) && $this->message !== '') {
38 1
            $html = '<div id="messagebox">';
39 1
            foreach ($this->message as $msg)
40
            {
41
                $html .= '
42 1
                <div class="message '.htmlentities($msg["type"]).'">
43 1
                    <p>'.htmlentities($msg["msg"]).'</p>
44 1
                </div>';
45 1
            }
46 1
            $html .= '</div>';
47 1
        }
48 1
        return $html;
49
    }
50
}
51