Completed
Push — master ( d1522b...c27b25 )
by Günter
10s
created

Helpers::active()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 11
nop 2
1
<?php
2
3
/**
4
 * This file is part of the NINEJKH/php-tpl library.
5
 *
6
 * (c) 9JKH (Pty) Ltd. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace NINEJKH\Tpl;
13
14
use Detection\MobileDetect;
15
16
class Helpers
17
{
18
    protected static $mobileDetect;
19
20
    public static function mobileDetect()
21
    {
22
        if (self::$mobileDetect === null) {
23
            self::$mobileDetect = new MobileDetect;
24
        }
25
26
        return self::$mobileDetect;
27
    }
28
29
    public static function active($menu, $return = false)
0 ignored issues
show
Coding Style introduced by
active uses the super-global variable $_SERVER 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
        if (empty($_SERVER['REQUEST_URI'])) {
32
            return;
33
        }
34
35
        // normalise request-uri
36
        $request_uri = mb_strtolower(urldecode($_SERVER['REQUEST_URI']));
37
        if (($pos = strpos($request_uri, '?')) !== false) {
38
            $request_uri = substr($request_uri, 0, $pos);
39
        }
40
41
        if (preg_match('~' . $menu . '~i', $request_uri)) {
42
            if (!$return) {
43
                echo ' active';
44
            } else {
45
                return true;
46
            }
47
        }
48
49
        if ($return) {
50
            return false;
51
        }
52
    }
53
54
    public static function checked($key, $default)
0 ignored issues
show
Coding Style introduced by
checked uses the super-global variable $_POST 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...
55
    {
56
        // if POST then use it
57
        if (static::posted()) {
58
            if (!empty($_POST[$key])) {
59
                echo ' checked';
60
                return;
61
            } else {
62
                echo '';
63
                return;
64
            }
65
        }
66
67
        // fallback
68
        if ($default) {
69
            echo ' checked';
70
            return;
71
        } else {
72
            echo '';
73
            return;
74
        }
75
    }
76
77
    public static function selected($key, $value, $default)
0 ignored issues
show
Coding Style introduced by
selected uses the super-global variable $_POST 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...
78
    {
79
        if (static::posted()) {
80
            if (!empty($_POST[$key]) && $_POST[$key] == $value) {
81
                echo ' selected';
82
                return;
83
            } else {
84
                echo '';
85
                return;
86
            }
87
        }
88
89
        if ($default == $value) {
90
            echo ' selected';
91
            return;
92
        } else {
93
            echo '';
94
            return;
95
        }
96
    }
97
98
    public static function posted()
0 ignored issues
show
Coding Style introduced by
posted uses the super-global variable $_SERVER 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...
99
    {
100
        return (!empty($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST');
101
    }
102
103
    public static function caseTitle($string)
104
    {
105
        return mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
106
    }
107
108
    public static function truncate($string, $max_length)
109
    {
110
        if (mb_strlen($string, 'UTF-8') <= $max_length) {
111
            return $string;
112
        }
113
114
        $string = mb_substr($string, 0, ($max_length - 3), 'UTF-8');
115
        $string .= '...';
116
        return $string;
117
    }
118
119
    public static function showAds()
0 ignored issues
show
Coding Style introduced by
showAds uses the super-global variable $_SERVER 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...
Coding Style introduced by
showAds 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...
120
    {
121
        if (($_SERVER['REMOTE_ADDR'] === '::1' || $_SERVER['REMOTE_ADDR'] === '127.0.0.1') && empty($_GET['force_ads'])) {
122
            return false;
123
        }
124
125
        return true;
126
    }
127
128
    public static function isDev()
0 ignored issues
show
Coding Style introduced by
isDev uses the super-global variable $_SERVER 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...
Coding Style introduced by
isDev 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...
129
    {
130
        if ($_SERVER['REMOTE_ADDR'] === '::1' || $_SERVER['REMOTE_ADDR'] === '127.0.0.1' || !empty($_GET['force_dev'])) {
131
            return true;
132
        }
133
134
        return false;
135
    }
136
137
    public static function isMobile()
138
    {
139
        if (self::mobileDetect()->isMobile()) {
140
            return true;
141
        }
142
143
        return false;
144
    }
145
146
    public static function isTablet()
147
    {
148
        if (self::mobileDetect()->isTablet()) {
149
            return true;
150
        }
151
152
        return false;
153
    }
154
}
155