Passed
Branch master (a0cc06)
by Dāvis
17:06
created

BrowserExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A isIE() 0 9 4
A getFunctions() 0 20 2
A getName() 0 3 1
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Twig;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Twig_Extension;
7
use Twig_SimpleFunction;
8
9
class BrowserExtension extends Twig_Extension
0 ignored issues
show
Coding Style introduced by
The property $short_functions is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
10
{
11
    protected $short_functions;
12
13
    public function __construct($container)
14
    {
15
        $this->short_functions = $container->hasParameter('sludio_helper.script.short_functions') && $container->getParameter('sludio_helper.script.short_functions', false);
16
    }
17
18
    public function getFunctions()
19
    {
20
        $array = [
21
            new Twig_SimpleFunction('sludio_is_ie', [
22
                $this,
23
                'isIE',
24
            ]),
25
        ];
26
27
        $short_array = [
28
            new Twig_SimpleFunction('is_ie', [
29
                $this,
30
                'isIE',
31
            ]),
32
        ];
33
34
        if ($this->short_functions) {
35
            return array_merge($array, $short_array);
36
        } else {
37
            return $array;
38
        }
39
    }
40
41
    public function getName()
42
    {
43
        return 'sludio_helper.twig.browser_extension';
44
    }
45
46
    public function isIE()
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 5 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
47
    {
48
        $request = Request::createFromGlobals();
49
        $ua = $request->server->get('HTTP_USER_AGENT');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ua. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
50
        if (strpos($ua, 'MSIE') || strpos($ua, 'Edge') || strpos($ua, 'Trident/7')) {
51
            return 1;
52
        }
53
54
        return 0;
55
    }
56
}
57