UrlHelper::link()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 10
dl 0
loc 4
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
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 Jitamin\Helper;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * Url Helper.
18
 */
19
class UrlHelper extends Base
20
{
21
    private $base = '';
22
    private $directory = '';
23
24
    /**
25
     * Helper to generate a link to the documentation.
26
     *
27
     * @param string $label
28
     * @param string $file
29
     *
30
     * @return string
31
     */
32
    public function doc($label, $file)
33
    {
34
        return $this->link($label, 'DocumentationController', 'show', ['file' => $file], false, '', '', true);
35
    }
36
37
    /**
38
     * Button Link Element.
39
     *
40
     * @param string $icon       Font-Awesome icon
41
     * @param string $label      Link label
42
     * @param string $controller Controller name
43
     * @param string $action     Action name
44
     * @param array  $params     Url parameters
45
     * @param string $class      CSS class attribute
46
     *
47
     * @return string
48
     */
49
    public function button($icon, $label, $controller, $action, array $params = [], $class = '')
50
    {
51
        $icon = '<i class="fa '.$icon.' fa-fw"></i> ';
52
        $class = 'btn '.$class;
53
54
        return $this->link($icon.$label, $controller, $action, $params, false, $class);
55
    }
56
57
    /**
58
     * Link element.
59
     *
60
     * @param string $label      Link label
61
     * @param string $controller Controller name
62
     * @param string $action     Action name
63
     * @param array  $params     Url parameters
64
     * @param bool   $csrf       Add a CSRF token
65
     * @param string $class      CSS class attribute
66
     * @param string $title      Link title
67
     * @param bool   $new_tab    Open the link in a new tab
68
     * @param string $anchor     Link Anchor
69
     * @param bool   $absolute
70
     *
71
     * @return string
72
     */
73
    public function link($label, $controller, $action, array $params = [], $csrf = false, $class = '', $title = '', $new_tab = false, $anchor = '', $absolute = false)
74
    {
75
        return '<a href="'.$this->href($controller, $action, $params, $csrf, $anchor, $absolute).'" class="'.$class.'" title=\''.$title.'\' '.($new_tab ? 'target="_blank"' : '').'>'.$label.'</a>';
76
    }
77
78
    /**
79
     * Absolute link.
80
     *
81
     * @param string $label
82
     * @param string $controller
83
     * @param string $action
84
     * @param array  $params
85
     *
86
     * @return string
87
     */
88
    public function absoluteLink($label, $controller, $action, array $params = [])
89
    {
90
        return $this->link($label, $controller, $action, $params, false, '', '', true, '', true);
91
    }
92
93
    /**
94
     * HTML Hyperlink.
95
     *
96
     * @param string $controller Controller name
97
     * @param string $action     Action name
98
     * @param array  $params     Url parameters
99
     * @param bool   $csrf       Add a CSRF token
100
     * @param string $anchor     Link Anchor
101
     * @param bool   $absolute   Absolute or relative link
102
     *
103
     * @return string
104
     */
105
    public function href($controller, $action, array $params = [], $csrf = false, $anchor = '', $absolute = false)
106
    {
107
        if (isset($params['q']) && $params['q'] === 'status:open') {
108
            unset($params['q']);
109
        }
110
111
        return $this->build('&amp;', $controller, $action, $params, $csrf, $anchor, $absolute);
112
    }
113
114
    /**
115
     * Generate controller/action url.
116
     *
117
     * @param string $controller Controller name
118
     * @param string $action     Action name
119
     * @param array  $params     Url parameters
120
     * @param string $anchor     Link Anchor
121
     * @param bool   $absolute   Absolute or relative link
122
     *
123
     * @return string
124
     */
125
    public function to($controller, $action, array $params = [], $anchor = '', $absolute = false)
126
    {
127
        return $this->build('&', $controller, $action, $params, false, $anchor, $absolute);
128
    }
129
130
    /**
131
     * Get application base url.
132
     *
133
     * @return string
134
     */
135
    public function base()
136
    {
137
        if (empty($this->base)) {
138
            $this->base = $this->settingModel->get('application_url') ?: $this->server();
0 ignored issues
show
Documentation introduced by
The property settingModel does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
        }
140
141
        return $this->base;
142
    }
143
144
    /**
145
     * Get application base directory.
146
     *
147
     * @return string
148
     */
149
    public function dir()
150
    {
151
        if ($this->directory === '' && $this->request->getMethod() !== '') {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
            $this->directory = str_replace('\\', '/', dirname($this->request->getServerVariable('PHP_SELF')));
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
            $this->directory = $this->directory !== '/' ? $this->directory.'/' : '/';
154
            $this->directory = str_replace('//', '/', $this->directory);
155
        }
156
157
        return $this->directory;
158
    }
159
160
    /**
161
     * Get current server base url.
162
     *
163
     * @return string
164
     */
165
    public function server()
166
    {
167
        if ($this->request->getServerVariable('SERVER_NAME') === '') {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
            return 'http://localhost/';
169
        }
170
171
        $url = $this->request->isHTTPS() ? 'https://' : 'http://';
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
172
        $url .= $this->request->getServerVariable('SERVER_NAME');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
173
        $url .= $this->request->getServerVariable('SERVER_PORT') == 80 || $this->request->getServerVariable('SERVER_PORT') == 443 ? '' : ':'.$this->request->getServerVariable('SERVER_PORT');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
174
        $url .= $this->dir() ?: '/';
175
176
        return $url;
177
    }
178
179
    /**
180
     * Build relative url.
181
     *
182
     * @param string $separator  Querystring argument separator
183
     * @param string $controller Controller name
184
     * @param string $action     Action name
185
     * @param array  $params     Url parameters
186
     * @param bool   $csrf       Add a CSRF token
187
     * @param string $anchor     Link Anchor
188
     * @param bool   $absolute   Absolute or relative link
189
     *
190
     * @return string
191
     */
192
    protected function build($separator, $controller, $action, array $params = [], $csrf = false, $anchor = '', $absolute = false)
193
    {
194
        $path = $this->route->findUrl($controller, $action, $params);
0 ignored issues
show
Documentation introduced by
The property route does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
195
        $qs = [];
196
197
        if (empty($path)) {
198
            $qs['controller'] = $controller;
199
            $qs['action'] = $action;
200
            $qs += $params;
201
        } else {
202
            unset($params['plugin']);
203
        }
204
205
        if ($csrf) {
206
            $qs['csrf_token'] = $this->token->getCSRFToken();
0 ignored issues
show
Documentation introduced by
The property token does not exist on object<Jitamin\Helper\UrlHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
        }
208
209
        if (!empty($qs)) {
210
            $prefix = '';
211
            if (isset($qs['controller'])) {
212
                unset($qs['controller']);
213
                $prefix = 'controller='.$controller.$separator;
214
            }
215
            $path .= '?'.$prefix.http_build_query($qs, '', $separator);
216
        }
217
218
        return ($absolute ? $this->base() : $this->dir()).$path.(empty($anchor) ? '' : '#'.$anchor);
219
    }
220
}
221