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

TranslationExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Translatable\Twig;
4
5
use Twig_Extension;
6
use Twig_SimpleFilter;
7
8
class TranslationExtension 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...
9
{
10
    protected $em;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. 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...
11
    protected $request;
12
    protected $defaultLocale;
13
    protected $short_functions;
14
15 View Code Duplication
    public function __construct($em, $request_stack, $default, $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $em. 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...
Coding Style Naming introduced by
The parameter $request_stack is not named in camelCase.

This check marks parameter 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...
16
    {
17
        $this->em = $em;
18
        $this->request = $request_stack->getCurrentRequest();
19
        $this->defaultLocale = $default;
20
        $this->short_functions = $container->hasParameter('sludio_helper.script.short_functions') && $container->getParameter('sludio_helper.script.short_functions', false);
21
    }
22
23
    public function getName()
24
    {
25
        return 'sludio_helper.twig.translate_extension';
26
    }
27
28 View Code Duplication
    public function getFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $array = [
31
            new Twig_SimpleFilter('sludio_var', [
32
                $this,
33
                'getVar',
34
            ]),
35
        ];
36
37
        $short_array = [
38
            new Twig_SimpleFilter('var', [
39
                $this,
40
                'getVar',
41
            ]),
42
        ];
43
44
        if ($this->short_functions) {
45
            return array_merge($array, $short_array);
46
        } else {
47
            return $array;
48
        }
49
    }
50
51
    public function getVar($type, $object, $original = false, $locale = null)
52
    {
53
        if ($object && is_object($object)) {
54
            $hl = $this->request ? $this->request->cookies->get('hl') : $this->defaultLocale;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $hl. 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...
55
56
            $new_locale = $locale;
57
            if (!$locale) {
58
                $new_locale = $this->request ? $this->request->get('_locale') : $hl;
59
            }
60
61
            $trans = $object->getVariableByLocale($type, $new_locale, $original);
62
            $class = get_class($object);
63
            $class = str_replace('Proxies\__CG__\\', '', $class);
0 ignored issues
show
Unused Code introduced by
The assignment to $class is dead and can be removed.
Loading history...
64
65
            return $trans;
66
        } else {
67
            return $type;
68
        }
69
    }
70
}
71