Controller   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 10
eloc 30
c 5
b 0
f 2
dl 0
loc 105
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A forbidden() 0 4 1
A accessGlobal() 0 5 1
A notFound() 0 4 1
A tableName() 0 4 1
A formatDate() 0 8 1
A __construct() 0 6 1
A render() 0 7 1
A antiXss() 0 3 1
A previousPage() 0 6 2
1
<?php
2
3
namespace Core\Controller;
4
5
use Core\Services\Globals\Globals;
6
7
class Controller
8
{
9
    protected string $viewPath;
10
    protected $template;
11
12
    public function __construct()
13
    {
14
        $this->GET = $this->accessGlobal('GET');
0 ignored issues
show
Bug Best Practice introduced by
The property GET does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
        $this->POST = $this->accessGlobal('POST');
0 ignored issues
show
Bug Best Practice introduced by
The property POST does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->SERVER = $this->accessGlobal('SERVER');
0 ignored issues
show
Bug Best Practice introduced by
The property SERVER does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->SESSION = $this->accessGlobal('SESSION');
0 ignored issues
show
Bug Best Practice introduced by
The property SESSION does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
    }
19
20
    /**
21
     * Sends to the targeted View and sends the $variables
22
     * @param string $view ex: table.methode.attributs - 'post.show.1'
23
     * @param array $variables
24
     * @return void
25
     */
26
    protected function render(string $view, array $variables = [])
27
    {
28
        \ob_start();
29
        \extract($variables);
30
        require($this->viewPath . \str_replace('.', '/', $view) . '.php');
31
        $content = \ob_get_clean();
32
        require($this->viewPath . 'templates/' . $this->template . '.php');
33
    }
34
35
    /**
36
     * Get the name of the table associated with the controller
37
     * @return string ex : 'Post'
38
     */
39
    protected function tableName(): string
40
    {
41
        \preg_match('/[\w\-\_]+(?=Controller)/im', \get_called_class(), $tableName);
42
        return $tableName[0];
43
    }
44
45
    /**
46
     * Return to the previous page
47
     * @return void
48
     */
49
    protected function previousPage()
50
    {
51
        if (isset($this->GET['return'])) {
52
            return \header('Location: index.php?p=' . $this->GET['return']);
0 ignored issues
show
Bug introduced by
Are you sure the usage of header('Location: index..... $this->GET['return']) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
        }
54
        return \header('Location: index.php');
0 ignored issues
show
Bug introduced by
Are you sure the usage of header('Location: index.php') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
    }
56
57
    /**
58
     * Format a date at the Local stardard
59
     * @param string $date
60
     * @return string
61
     */
62
    protected function formatDate(string $date): string
63
    {
64
        setlocale(LC_TIME, 'fr', 'fr_FR', 'fr_FR.ISO8859-1');
65
66
        $date = \strtotime($date);
67
        $day = strftime("%d ", $date);
68
        $day .= ucfirst(strftime("%B ", $date));
69
        return $day .= strftime("%G", $date);
70
    }
71
72
    /**
73
     * Management in the event of an error HTTP 403
74
     * @return void
75
     */
76
    protected function forbidden()
77
    {
78
        header("HTTP/1.0 403 Forbidden");
79
        return \header('Location: index.php?p=user.login');
0 ignored issues
show
Bug introduced by
Are you sure the usage of header('Location: index.php?p=user.login') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
80
    }
81
82
    /**
83
     * Management in the event of an error 404
84
     * @return void
85
     */
86
    protected function notFound()
87
    {
88
        header("HTTP/1.0 404 Not Found");
89
        return \header('Location: index.php');
0 ignored issues
show
Bug introduced by
Are you sure the usage of header('Location: index.php') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
90
    }
91
92
    /**
93
     * Add an XSS filter on the displayed datas
94
     * @param string $input
95
     * @return string
96
     */
97
    protected function antiXss(string $input): string
98
    {
99
        return \nl2br(\htmlspecialchars($input), ENT_QUOTES);
0 ignored issues
show
Bug introduced by
Core\Controller\ENT_QUOTES of type integer is incompatible with the type boolean expected by parameter $use_xhtml of nl2br(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        return \nl2br(\htmlspecialchars($input), /** @scrutinizer ignore-type */ ENT_QUOTES);
Loading history...
100
    }
101
102
    /**
103
     * Secure get access to superglobals
104
     * @param string $global name of the superglobal targeted
105
     * @return mixed
106
     */
107
    protected function accessGlobal(string $global)
108
    {
109
        $globals = new Globals;
110
        $global = 'get' . $global;
111
        return $globals->$global();
112
    }
113
}
114