Completed
Push — master ( 55311e...acc5b2 )
by Craig
06:34
created

PageVarExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 15.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 16
loc 103
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getFunctions() 0 8 1
A pageAddVar() 0 9 2
A pageSetVar() 8 8 3
A pageGetVar() 8 8 2

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
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\ThemeModule\Twig\Extension;
13
14
use Psr\Log\LoggerInterface;
15
use Zikula\Common\Translator\TranslatorInterface;
16
use Zikula\ThemeModule\Engine\ParameterBag;
17
18
class PageVarExtension extends \Twig_Extension
19
{
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    private $translator;
24
25
    /**
26
     * @var ParameterBag
27
     */
28
    private $pageVars;
29
30
    /**
31
     * @var LoggerInterface
32
     * @deprecated - remove at Core-2.0
33
     */
34
    private $logger;
35
36
    /**
37
     * @var AssetExtension
38
     * @deprecated - remove at Core-2.0
39
     */
40
    private $assetExtension;
41
42
    /**
43
     * PageVarExtension constructor.
44
     * @param TranslatorInterface $translator
45
     * @param ParameterBag $pageVars
46
     * @param LoggerInterface $logger
47
     * @param AssetExtension $assetExtension
48
     */
49
    public function __construct(
50
        TranslatorInterface $translator,
51
        ParameterBag $pageVars,
52
        LoggerInterface $logger,
53
        AssetExtension $assetExtension
54
    ) {
55
        $this->translator = $translator;
56
        $this->pageVars = $pageVars;
57
        $this->logger = $logger;
0 ignored issues
show
Deprecated Code introduced by
The property Zikula\ThemeModule\Twig\...geVarExtension::$logger has been deprecated with message: - remove at Core-2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
58
        $this->assetExtension = $assetExtension;
0 ignored issues
show
Deprecated Code introduced by
The property Zikula\ThemeModule\Twig\...ension::$assetExtension has been deprecated with message: - remove at Core-2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
59
    }
60
61
    /**
62
     * Returns a list of functions to add to the existing list.
63
     *
64
     * @return array An array of functions
65
     */
66
    public function getFunctions()
67
    {
68
        return [
69
            new \Twig_SimpleFunction('pageAddVar', [$this, 'pageAddVar']), // @deprecated
70
            new \Twig_SimpleFunction('pageSetVar', [$this, 'pageSetVar']),
71
            new \Twig_SimpleFunction('pageGetVar', [$this, 'pageGetVar']),
72
        ];
73
    }
74
75
    /**
76
     * @deprecated at Core 1.4.1, remove at Core-2.0
77
     * @see use pageSetVar() or pageAddAsset()
78
     * @param string $name
79
     * @param string $value
80
     */
81
    public function pageAddVar($name, $value)
82
    {
83
        $this->logger->log(\Monolog\Logger::DEBUG, '\Zikula\Bundle\CoreBundle\Twig\Extension\CoreExtension::pageAddVar is deprecated use pageAddAsset() or pageSetVar().');
0 ignored issues
show
Deprecated Code introduced by
The property Zikula\ThemeModule\Twig\...geVarExtension::$logger has been deprecated with message: - remove at Core-2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
84
        if (in_array($name, ['stylesheet', 'javascript', 'header', 'footer'])) {
85
            $this->assetExtension->pageAddAsset($name, $value);
0 ignored issues
show
Deprecated Code introduced by
The property Zikula\ThemeModule\Twig\...ension::$assetExtension has been deprecated with message: - remove at Core-2.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
86
        } else {
87
            $this->pageSetVar($name, $value);
88
        }
89
    }
90
91
    /**
92
     * Zikula imposes no restriction on page variable names.
93
     * Typical usage is to set `title` `meta.charset` `lang` etc.
94
     * array values are set using `.` in the `$name` string (e.g. `meta.charset`)
95
     * @param string $name
96
     * @param string $value
97
     */
98 View Code Duplication
    public function pageSetVar($name, $value)
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...
99
    {
100
        if (empty($name) || empty($value)) {
101
            throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
102
        }
103
104
        $this->pageVars->set($name, $value);
105
    }
106
107
    /**
108
     * @param $name
109
     * @param null $default
110
     * @return mixed
111
     */
112 View Code Duplication
    public function pageGetVar($name, $default = null)
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...
113
    {
114
        if (empty($name)) {
115
            throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
116
        }
117
118
        return $this->pageVars->get($name, $default);
119
    }
120
}
121