Completed
Push — master ( 48cc3d...5640fa )
by Craig
12:32 queued 05:19
created

TwigExtension::displayPathAsString()   C

Complexity

Conditions 11
Paths 22

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 34
nc 22
nop 2
dl 0
loc 50
rs 5.4893
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Routes.
4
 *
5
 * @copyright Zikula contributors (Zikula)
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 * @author Zikula contributors <[email protected]>.
8
 * @link http://www.zikula.org
9
 * @link http://zikula.org
10
 * @version Generated by ModuleStudio 0.7.4 (http://modulestudio.de).
11
 */
12
13
namespace Zikula\RoutesModule\Twig;
14
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Zikula\RoutesModule\Entity\RouteEntity;
17
use Zikula\RoutesModule\Twig\Base\AbstractTwigExtension;
18
use ZLanguage;
19
20
/**
21
 * Twig extension implementation class.
22
 */
23
class TwigExtension extends AbstractTwigExtension
24
{
25
    /**
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function getFunctions()
34
    {
35
        return [
36
            new \Twig_SimpleFunction('zikularoutesmodule_userAvatar', [$this, 'getUserAvatar'], ['is_safe' => ['html']]) // from base class
37
        ];
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function getFilters()
44
    {
45
        return [
46
            new \Twig_SimpleFilter('zikularoutesmodule_listEntry', [$this, 'getListEntry']), // from base class
47
            new \Twig_SimpleFilter('zikularoutesmodule_arrayToString', [$this, 'displayArrayAsString']),
48
            new \Twig_SimpleFilter('zikularoutesmodule_pathToString', [$this, 'displayPathAsString'])
49
        ];
50
    }
51
52
    /**
53
     * Sets the service container.
54
     *
55
     * @param ContainerInterface $container
56
     */
57
    public function setContainer(ContainerInterface $container) {
58
        $this->container = $container;
59
    }
60
61
    /**
62
     * The zikularoutesmodule_arrayToString filter displays the content of a given array.
63
     * Example:
64
     *    {{ route.defaults|zikularoutesmodule_arrayToString }}
65
     *
66
     * @param array $array The input array.
0 ignored issues
show
Bug introduced by
There is no parameter named $array. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
     *
68
     * @return string Output string for display.
69
     */
70
    public function displayArrayAsString(array $input = [])
71
    {
72
        return '<pre>' . print_r($input, true) . '</pre>';
73
    }
74
75
    /**
76
     * The zikularoutesmodule_pathToString filter displays a route's path.
77
     * Example:
78
     *    {{ route.path|zikularoutesmodule_pathToString(route) }}
79
     *
80
     * @param string      $path  The route path.
81
     * @param RouteEntity $route The route object.
82
     *
83
     * @return string Output string for display.
84
     */
85
    public function displayPathAsString($path, $route)
86
    {
87
        $prefix = '';
88
        $translationPrefix = $route->getTranslationPrefix();
89
        if (!empty($translationPrefix)) {
90
            $prefix = '/' . $translationPrefix;
91
        }
92
93
        if ($route->getTranslatable()) {
94
            $languages = ZLanguage::getInstalledLanguages();
95
            $isRequiredLangParam = ZLanguage::isRequiredLangParam();
96
            if (!$isRequiredLangParam) {
97
                $defaultLanguage = $this->variableApi->getSystemVar('language_i18n');
98
                unset($languages[array_search($defaultLanguage, $languages)]);
99
            }
100
            if (count($languages) > 0) {
101
                $prefix = ($isRequiredLangParam ? '/' : '{/') . implode('|', $languages) . ($isRequiredLangParam ? '' : '}');
102
            }
103
        }
104
105
        $prefix = htmlspecialchars($prefix);
106
        $path = htmlspecialchars($route->getPathWithBundlePrefix());
107
        $container = $this->container;
108
109
        $path = preg_replace_callback('#%(.*?)%#', function ($matches) use ($container) {
110
            return '<abbr title="' . htmlspecialchars($matches[0]) . '">' . htmlspecialchars($container->getParameter($matches[1])) . '</abbr>';
111
        }, $path);
112
113
        $defaults = $route->getDefaults();
114
        $requirements = $route->getRequirements();
115
        $path = preg_replace_callback('#{(.*?)}#', function ($matches) use ($defaults, $requirements) {
116
            $title = '';
117
            if (isset($defaults[$matches[1]])) {
118
                $title .= $this->__f('Default: %s', ['%s' => htmlspecialchars($defaults[$matches[1]])]);
119
            }
120
            if (isset($requirements[$matches[1]])) {
121
                if ($title != '') {
122
                    $title .= ' | ';
123
                }
124
                $title .= $this->__f('Requirement: %s', ['%s' => htmlspecialchars($requirements[$matches[1]])]);
125
            }
126
            if ($title == '') {
127
                return $matches[0];
128
            }
129
130
            return '<abbr title="' . $title . '">' . $matches[0] . '</abbr>';
131
        }, $path);
132
133
        return $prefix . '<strong>' . $path . '</strong>';
134
    }
135
}
136