Test Failed
Push — master ( c5de53...cf76ff )
by Mikael
02:00
created

ViewHelperFunctions.php ➔ currentRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\View;
4
5
use \Anax\View\View2 as View;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Anax\View\View.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 * Define helper functions to include before processing the view template.
9
 * The functions here are exposed to the view and can be used in the view.
10
 */
11
12
/**
13
 * Shortcut to create an url for a static asset.
14
 *
15
 * @param string $url url to use when creating the url.
16
 *
17
 * @return string as resulting url.
18
 */
19
function asset($url = "")
20
{
21
    global $di;
22
    return $di->get("url")->asset($url);
23
}
24
25
26
27
/**
28
 * Shortcut to create an url for routing in the framework.
29
 *
30
 * @param null|string $url url to use when creating the url.
31
 *
32
 * @return string as resulting url.
33
 */
34
function url($url = "")
35
{
36
    global $di;
37
    return $di->get("url")->create($url);
38
}
39
40
41
42
/**
43
 * Render a view with an optional data set of variables.
44
 *
45
 * @param string $template the template file, or array
46
 * @param array  $data     variables to make available to the
47
 *                         view, default is empty
48
 *
49
 * @return void
50
 */
51
function renderView($template, $data = [])
52
{
53
    global $di;
54
    $view = new View();
55
    $template = $di->get("view")->getTemplateFile($template);
56
    $view->set($template, $data);
57
    $view->render($this->app);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
58
}
59
60
61
62
/**
63
 * Check if the region in the view container has views to render.
64
 *
65
 * @param string $region to check
66
 *
67
 * @return boolean true or false
68
 */
69
function regionHasContent($region)
70
{
71
    global $di;
72
    return $di->get("view")->hasContent($region);
73
}
74
75
76
77
/**
78
 * Render views, from the view container, in the region.
79
 *
80
 * @param string $region to render in
81
 *
82
 * @return boolean true or false
83
 */
84
function renderRegion($region)
85
{
86
    global $di;
87
    return $di->get("view")->render($region);
88
}
89
90
91
92
/**
93
 * Create a class attribute from a string or array.
94
 *
95
 * @param string|array $args variable amount of classlists.
96
 *
97
 * @return string as complete class attribute
98
 */
99 View Code Duplication
function classList(...$args)
0 ignored issues
show
Duplication introduced by
This function 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...
100
{
101
    $classes = [];
102
103
    foreach ($args as $arg) {
104
        if (empty($arg)) {
105
            continue;
106
        } elseif (is_string($arg)) {
107
            $arg = explode(" ", $arg);
108
        }
109
        $classes = array_merge($classes, $arg);
110
    }
111
112
    return "class=\"" . implode(" ", $classes) . "\"";
113
}
114
115
116
117
/**
118
 * Get current url, without querystring.
119
 *
120
 * @return string as resulting url.
121
 */
122
function currentUrl()
123
{
124
    global $di;
125
    return $di->get("request")->getCurrentUrl(false);
126
}
127
128
129
130
/**
131
 * Get current route.
132
 *
133
 * @return string as resulting route.
134
 */
135
function currentRoute()
136
{
137
    global $di;
138
    return $di->get("request")->getRoute();
139
}
140