Passed
Push — master ( 200f34...68234c )
by Mehmet
01:49
created

TwigExtensions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 46.02 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 52
loc 113
rs 10
c 0
b 0
f 0
ccs 0
cts 96
cp 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A extendForGetUrl() 15 15 1
A extendForWidget() 12 12 1
A extendForQueryParams() 13 11 1
A extendForSiteUrl() 11 11 1
A extendForVarDump() 0 14 1
B extendForPagination() 0 32 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Selami\View\Twig;
5
6
use Selami\Stdlib\CaseConverter;
7
use Selami\View\ExtensionsAbstract;
8
use Twig_Environment;
9
use InvalidArgumentException;
10
use BadMethodCallException;
11
use Twig_SimpleFunction;
12
13
/**
14
 * Class TwigExtensions extends ViewExtensionsAbstracts
15
 *
16
 * @package Selami\View\Twig
17
 */
18
class TwigExtensions extends ExtensionsAbstract
19
{
20
    private $twig;
21
    private $config;
22
23
    public function __construct(Twig_Environment $twig, array $config)
24
    {
25
        $this->twig = $twig;
26
        $this->config = $config;
27
        $this->loadFunctions();
28
    }
29
30 View Code Duplication
    protected function extendForGetUrl() : void
1 ignored issue
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...
31
    {
32
        $filter = new Twig_SimpleFunction(
33
            'getUrl',
34
            function (
35
                $alias,
36
                $params = []
37
            ) {
38
                $function = new Functions\GetUrl($this->config['base_url'], $this->config['aliases'], $alias, $params);
39
                return $function->run();
40
            },
41
            array('is_safe' => array('html'))
42
        );
43
        $this->twig->addFunction($filter);
44
    }
45
46 View Code Duplication
    protected function extendForWidget() : void
1 ignored issue
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...
47
    {
48
        $filter = new \Twig_SimpleFunction(
49
            'Widget_*_*',
50
            function ($widgetNameStr, $widgetActionStr, $args = []) {
51
                $function = new Functions\Widget($this->twig, $this->config, $widgetNameStr, $widgetActionStr, $args);
1 ignored issue
show
Compatibility introduced by
$this->twig of type object<Twig_Environment> is not a sub-type of object<Twig\Environment>. It seems like you assume a child class of the class Twig_Environment to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
52
                return $function->run();
53
            },
54
            array('is_safe' => array('html'))
55
        );
56
        $this->twig->addFunction($filter);
57
    }
58
59 View Code Duplication
    protected function extendForQueryParams() : void
1 ignored issue
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...
60
    {
61
        $filter = new \Twig_SimpleFunction(
62
            'queryParams',
63
            function ($queryParams, $prefix = '?') {
64
                return $prefix . http_build_query($queryParams);
65
            },
66
            array('is_safe' => array('html'))
67
        );
68
        $this->twig->addFunction($filter);
69
    }
70
71 View Code Duplication
    protected function extendForSiteUrl() : void
1 ignored issue
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...
72
    {
73
        $filter = new \Twig_SimpleFunction(
74
            'siteUrl',
75
            function ($path = '') {
76
                return $this->config['base_url'] . $path;
77
            },
78
            array('is_safe' => array('html'))
79
        );
80
        $this->twig->addFunction($filter);
81
    }
82
83
    protected function extendForVarDump() : void
84
    {
85
        $filter = new \Twig_SimpleFunction(
86
            'varDump',
87
            function ($args) {
88
                /**
89
            * @noinspection ForgottenDebugOutputInspection
90
            */
91
                var_dump($args);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($args); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
92
            },
93
            array('is_safe' => array('html'))
94
        );
95
        $this->twig->addFunction($filter);
96
    }
97
98
    protected function extendForPagination() : void
99
    {
100
        /**
101
 * @noinspection MoreThanThreeArgumentsInspection
102
*/
103
        $filter = new \Twig_SimpleFunction(
104
            'Pagination',
105
            function (
106
                int $total,
107
                int $current,
108
                string $linkTemplate,
109
                string $parentTemplate = '<ul class="pagination">(items)</ul>',
110
                string $itemTemplate = '<li class="(item_class)">(link)</li>',
111
                string $linkItemTemplate = '<a href="(href)" class="(link_class)">(text)</a>',
112
                string $ellipsesTemplate = '<li><a>...</a></li>'
113
            ) {
114
115
                $function = new Functions\Pagination(
116
                    $total,
117
                    $current,
118
                    $linkTemplate,
119
                    $parentTemplate,
120
                    $itemTemplate,
121
                    $linkItemTemplate,
122
                    $ellipsesTemplate
123
                );
124
                return $function->run();
125
            },
126
            array('is_safe' => array('html'))
127
        );
128
        $this->twig->addFunction($filter);
129
    }
130
}
131