Completed
Push — master ( 2a3466...2685c9 )
by Mikael
03:29
created

ViewHelperTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 207
Duplicated Lines 7.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 32.14%

Importance

Changes 0
Metric Value
dl 15
loc 207
ccs 9
cts 28
cp 0.3214
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A asset() 0 4 1
A url() 0 4 1
A renderView() 0 7 1
A regionHasContent() 0 4 1
A renderRegion() 0 4 1
A classList() 15 15 4
A currentUrl() 0 4 1
A currentRoute() 0 4 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
3
namespace Anax\View;
4
5
/**
6
 * Trait with view helpers, provided by the ThemeEngine to the views.
7
 *
8
 */
9
trait ViewHelperTrait
10
{
11
    /**
12
     * Shortcut to create an url for a static asset.
13
     *
14
     * @param string $url url to use when creating the url.
15
     *
16
     * @return string as resulting url.
17
     */
18
    public function asset($url = "")
19
    {
20
        return $this->app->url->asset($url);
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23
24
25
    /**
26
     * Shortcut to create an url for routing in the framework.
27
     *
28
     * @param null|string $url url to use when creating the url.
29
     *
30
     * @return string as resulting url.
31
     */
32
    public function url($url = "")
33
    {
34
        return $this->app->url->create($url);
35
    }
36
37
38
39
    /**
40
     * Render a view with an optional data set of variables.
41
     *
42
     * @param string $template the template file, or array
43
     * @param array  $data     variables to make available to the
44
     *                         view, default is empty
45
     *
46
     * @return void
47
     */
48
    public function renderView($template, $data = [])
49
    {
50
        $view = new View();
51
        $template = $this->app->view->getTemplateFile($template);
52
        $view->set($template, $data);
53
        $view->render($this->app);
54
    }
55
56
57
58
    /**
59
     * Check if the region in the view container has views to render.
60
     *
61
     * @param string $region to check
62
     *
63
     * @return boolean true or false
64
     */
65
    public function regionHasContent($region)
66
    {
67
        return $this->app->view->hasContent($region);
68
    }
69
70
71
72
    /**
73
     * Render views, from the view container, in the region.
74
     *
75
     * @param string $region to render in
76
     *
77
     * @return boolean true or false
78
     */
79
    public function renderRegion($region)
80
    {
81
        $this->app->view->render($region);
82
    }
83
84
85
86
    /**
87
     * Create a class attribute from a string or array.
88
     *
89
     * @param string|array $args variable amount of classlists.
90
     *
91
     * @return string as complete class attribute
92
     */
93 5 View Code Duplication
    public function classList(...$args)
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...
94
    {
95 5
        $classes = [];
96
    
97 5
        foreach ($args as $arg) {
98 5
            if (empty($arg)) {
99 2
                continue;
100 4
            } elseif (is_string($arg)) {
101 4
                $arg = explode(" ", $arg);
102
            }
103 4
            $classes = array_merge($classes, $arg);
104
        }
105
    
106 5
        return "class=\"" . implode(" ", $classes) . "\"";
107
    }
108
109
110
111
    /**
112
     * Get current url, without querystring.
113
     *
114
     * @return string as resulting url.
115
     */
116
    public function currentUrl()
117
    {
118
        return $this->app->request->getCurrentUrl(false);
119
    }
120
121
122
123
    /**
124
     * Get current route.
125
     *
126
     * @return string as resulting route.
127
     */
128
    public function currentRoute()
129
    {
130
        return $this->app->request->getRoute();
131
    }
132
133
134
135
    /**
136
     * Load content from a route and return details to view.
137
     *
138
     * @param string $route to load content from.
139
     *
140
     * @return array with values to extract in view.
141
     */
142
    // public function getContentForRoute($route)
143
    // {
144
    //     $content = $this->di->get("content")->contentForInternalRoute($route);
145
    //     return $content->views["main"]["data"];
146
    // }
147
148
149
150
    /**
151
     * Wrap a HTML element with start and end.
152
     *
153
     * @param string  $text  with content
154
     * @param string  $tag   HTML tag to search for
155
     * @param string  $start wrap start part
156
     * @param string  $end   wrap end part
157
     * @param number  $count hits to search for
158
     *
159
     * @return array with values to extract in view.
160
     */
161
    // public function wrapElementWithStartEnd($text, $tag, $start, $end, $count)
162
    // {
163
    //     return $this->di->get("textFilter")->wrapElementWithStartEnd($text, $tag, $start, $end, $count);
164
    // }
165
166
167
168
    /**
169
     * Wrap content of a HTML element with start and end.
170
     *
171
     * @param string  $text  with content
172
     * @param string  $tag   HTML tag to search for
173
     * @param string  $start wrap start part
174
     * @param string  $end   wrap end part
175
     * @param number  $count hits to search for
176
     *
177
     * @return array with values to extract in view.
178
     */
179
    // public function wrapElementContentWithStartEnd($text, $tag, $start, $end, $count)
180
    // {
181
    //     return $this->di->get("textFilter")->wrapElementContentWithStartEnd($text, $tag, $start, $end, $count);
182
    // }
183
184
185
186
    /**
187
     * Extrat the publish or update date for the article.
188
     *
189
     * @param array $dates a collection of possible date values.
190
     *
191
     * @return array with values for showing the date.
192
     */
193
    // public function getPublishedDate($dates)
194
    // {
195
    //     $defaults = [
196
    //         "revision" => [],
197
    //         "published" => null,
198
    //         "updated" => null,
199
    //         "created" => null,
200
    //     ];
201
    //     $dates = array_merge($defaults, $dates);
202
    //
203
    //     if ($dates["revision"]) {
204
    //         return [t("Latest revision"), key($dates["revision"])];
205
    //     } elseif ($dates["published"]) {
206
    //         return [t("Published"), $dates["published"]];
207
    //     } elseif ($dates["updated"]) {
208
    //         return [t("Updated"), $dates["updated"]];
209
    //     } elseif ($dates["created"]) {
210
    //         return [t("Created"), $dates["created"]];
211
    //     }
212
    //
213
    //     return [t("Missing pubdate."), null];
214
    // }
215
}
216