Completed
Push — master ( af41fc...386a3a )
by Mihail
02:49
created

NativeGenerator::isCurrentLink()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
rs 6.3327
cc 13
eloc 37
nc 12
nop 3

How to fix   Long Method    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
namespace Ffcms\Core\Helper\HTML;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Security;
7
use Ffcms\Core\Helper\Type\Obj;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Core\Helper\Url;
10
11
abstract class NativeGenerator
12
{
13
14
    /**
15
     * Make data "safe" - all dangerous html/js/etc will be removed
16
     * @param string $data
17
     * @param bool $quotes
18
     * @return string
19
     */
20
    public static function safe($data, $quotes = false)
21
    {
22
        $data = App::$Security->secureHtml($data);
23
        return $quotes ? $data : App::$Security->escapeQuotes($data);
24
    }
25
26
    /**
27
     * Remove all html tags from data
28
     * @param string $data
29
     * @return string
30
     */
31
    public static function nohtml($data)
32
    {
33
        return App::$Security->escapeQuotes(App::$Security->strip_tags($data));
0 ignored issues
show
Bug introduced by
It seems like \Ffcms\Core\App::$Security->strip_tags($data) targeting Ffcms\Core\Helper\Security::strip_tags() can also be of type array; however, Ffcms\Core\Helper\Security::escapeQuotes() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
34
    }
35
36
    /**
37
     * Build property for html element from array.
38
     * IMPORTANT: $property can be null-string (some times this happend's) - do not remove NULL!!
39
     * @param array $property
40
     * @return null|string
41
     */
42
    public static function applyProperty(array $property = null)
43
    {
44
        if (!Obj::isArray($property) || count($property) < 1) {
45
            return null;
46
        }
47
48
        $build = null;
49
        foreach ($property as $p => $v) {
0 ignored issues
show
Bug introduced by
The expression $property of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
50
            if ($v === null || $v === false) {
51
                $build .= ' ' . self::nohtml($p);
52
            } else {
53
                $build .= ' ' . self::nohtml($p) . '="' . self::nohtml($v) . '"';
54
            }
55
        }
56
        return $build;
57
    }
58
59
    /**
60
     * Fast building single tag based on property's array
61
     * @param string $tagName
62
     * @param array|null $property
63
     * @return string
64
     */
65
    public static function buildSingleTag($tagName, array $property = null)
66
    {
67
        return '<' . self::nohtml($tagName) . self::applyProperty($property) . '/>';
68
    }
69
70
    /**
71
     * Fast building container type tag based on property's and value
72
     * @param string $tagName
73
     * @param array|null $property
74
     * @param null|string $value
75
     * @param bool $valueHtml
76
     * @return string
77
     */
78
    public static function buildContainerTag($tagName, array $property = null, $value = null, $valueHtml = false)
79
    {
80
        $tagName = self::nohtml($tagName);
81
        if ($valueHtml !== true) {
82
            $value = self::nohtml($value);
83
        }
84
        return '<' . $tagName . self::applyProperty($property) . '>' . $value . '</' . $tagName . '>';
85
    }
86
87
    /**
88
     * Make parts of URI safe and usable
89
     * @param string $string
90
     * @param bool $encode
91
     * @return string
92
     */
93
    public static function safeUri($string, $encode = true)
94
    {
95
        $string = Str::lowerCase($string);
96
        $string = self::nohtml($string);
97
        if ($encode === true) {
98
            $string = urlencode($string);
99
        }
100
        return $string;
101
    }
102
103
    /**
104
     * Check if uri $source is equal to current uri point with array of $aliases and active $order set
105
     * @param null $source
106
     * @param array|null $aliases
107
     * @param bool $order
108
     * @return bool
109
     */
110
    public static function isCurrentLink($source = null, array $aliases = null, $order = false)
111
    {
112
        $elementPoint = Url::buildPathway($source);
113
        $currentPoint = Url::buildPathwayFromRequest();
114
115
        // use special active element order type: controller, action
116
        switch ($order) {
117
            case 'controller':
118
                $elementPoint = Str::firstIn($elementPoint, '/');
119
                $active = Str::startsWith($elementPoint, $currentPoint);
120
                break;
121
            case 'action':
122
                $elementArray = explode('/', $elementPoint);
123
                if (!Str::contains('/', $elementPoint) || count($elementArray) < 2) {
124
                    $active = $elementPoint === $currentPoint;
125
                } else {
126
                    $elementPoint = $elementArray[0] . '/' . $elementArray[1];
127
                    $active = Str::startsWith($elementPoint, $currentPoint);
128
                }
129
                break;
130
            case 'id':
131
                $elementArray = explode('/', $elementPoint);
132
                $elementPoint = $elementArray[0] . '/' . $elementArray[1];
133
                if (null !== $elementArray[2]) {
134
                    $elementPoint .= '/' . $elementArray[2];
135
                }
136
137
                $active = Str::startsWith($elementPoint, $currentPoint);
138
                break;
139
            default:
140
                $active = $elementPoint === $currentPoint;
141
                break;
142
        }
143
144
        // check if current uri equals with aliases
145
        if (Obj::isArray($aliases) && count($aliases) > 0) {
146
            foreach ($aliases as $activeUri) {
0 ignored issues
show
Bug introduced by
The expression $aliases of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
147
                $activeUri = trim($activeUri, '/');
148
                if (Str::endsWith('*', $activeUri)) {
149
                    $activeUri = rtrim($activeUri, '*');
150
                    if (Str::startsWith($activeUri, $currentPoint)) {
151
                        $active = true;
152
                    }
153
                } else {
154
                    if ($activeUri === $currentPoint) {
155
                        $active = true;
156
                    }
157
                }
158
            }
159
        }
160
161
        return $active;
162
    }
163
164
    /**
165
     * Apply security for string to output as html
166
     * @param string|null $object
167
     * @param bool $html
168
     * @param bool $secure
169
     * @return null|string
170
     */
171
    public static function applyEscape($object = null, $html = false, $secure = false)
172
    {
173
        $object = (string)$object;
174
        if ($html !== true) {
175
            $object = self::nohtml($object);
176
        } elseif ($secure !== true) {
177
            $object = self::safe($object, true);
178
        }
179
180
        return $object;
181
    }
182
183
    /**
184
     * Convert link-binding type to classic link with security filter
185
     * @param string|array $uri
186
     * @return string
187
     */
188
    public static function convertLink($uri)
189
    {
190
        $link = App::$Alias->baseUrl . '/';
191
        if (Obj::isArray($uri)) {
192
            $link .= Url::buildPathway($uri);
0 ignored issues
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 188 can also be of type string; however, Ffcms\Core\Helper\Url::buildPathway() does only seem to accept null|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
193
        } elseif (Str::startsWith('http', $uri)) {
0 ignored issues
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 188 can also be of type array; however, Ffcms\Core\Helper\Type\Str::startsWith() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
194
            $link = self::nohtml($uri);
0 ignored issues
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 188 can also be of type array; however, Ffcms\Core\Helper\HTML\NativeGenerator::nohtml() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
195
        } elseif (Str::startsWith('#', $uri)) { // allow pass #part
0 ignored issues
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 188 can also be of type array; however, Ffcms\Core\Helper\Type\Str::startsWith() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
196
            $link = self::nohtml($uri);
0 ignored issues
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 188 can also be of type array; however, Ffcms\Core\Helper\HTML\NativeGenerator::nohtml() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
197
        } else {
198
            $link .= self::nohtml(trim($uri, '/'));
199
        }
200
        return $link;
201
    }
202
203
204
}