Passed
Branch master (b52c46)
by refat
04:18
created

helpers.php ➔ cleanUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
use System\Application;
4
use System\File;
5
6
if (!function_exists('app')) {
7
    /**
8
     * Get Application instance
9
     *
10
     * @param \System\File
11
     * @return \System\Application
12
     */
13
    function app()
14
    {
15
        return Application::getInstance(new File(__DIR__));
16
    }
17
}
18
19
if (!function_exists('pre')) {
20
    /**
21
     * Display the give $input
22
     *
23
     * @param string $input
24
     * @return string
25
     */
26
    function pre($input)
27
    {
28
        echo '<pre>';
29
        print_r($input);
30
        echo '</pre>';
31
    }
32
}
33
34
if (!function_exists('sp')) {
35
    /**
36
     * Echo ======
37
     *
38
     * @return void
39
     */
40
    function sp()
41
    {
42
        echo '============================';
43
    }
44
}
45
46
if (!function_exists('_e')) {
47
    /**
48
     * Clean the fiven $value
49
     *
50
     * @param string $value
51
     * @return string
52
     */
53
    function _e($value)
54
    {
55
        $value = trim($value);
56
        $value = preg_replace('# {2,}#', ' ', $value);
57
        $value = htmlspecialchars($value);
58
        return $value;
59
    }
60
}
61
62
if (!function_exists('assets')) {
63
    /**
64
     * Get the assets of the fiven $path
65
     *
66
     * @property object $url
67
     * @param string $path
68
     * @return string
69
     */
70
    function assets($path = null)
71
    {
72
        return app()->url->link('public/' . ($path ? $path : ''));
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<System\Application>. 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...
73
    }
74
}
75
76
if (!function_exists('array_get')) {
77
    /**
78
     * Get the value of the given key of the given array
79
     * if the given key is not exist than return the given default
80
     *
81
     * @param array $array
82
     * @param string $key
83
     * @param mixed $default
84
     * @return mixed
85
     */
86
    function array_get(array $array, string $key, $default = null)
87
    {
88
        return ($array[$key] || $array[$key] == '0') ? $array[$key] : $default;
89
    }
90
}
91
92
if (!function_exists('array_equal')) {
93
    /**
94
     * Check if the given arrays are equal
95
     *
96
     * @param array $a
97
     * @param array $b
98
     * @return bool
99
     */
100
    function array_equal(array $a, array $b)
101
    {
102
        return (is_array($a)
103
            && is_array($b)
104
            && count($a) == count($b)
105
            && array_diff($a, $b) === array_diff($b, $a));
106
    }
107
}
108
109
if (!function_exists('isExtesntionAllowed')) {
110
    /**
111
     * Check if the given extension is allow to use
112
     *
113
     * @param string $key
114
     * @param string $extension
115
     * @property object $file
116
     * @return bool
117
     */
118
    function isExtesntionAllowed($key, $extension)
119
    {
120
        $allowExtesntions = app()->file->call('config/uploads.php')[$key];
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<System\Application>. 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...
121
122
        return in_array($extension, $allowExtesntions);
123
    }
124
}
125
126
if (!function_exists('isImage')) {
127
    /**
128
     * Check if the given minetype is an image
129
     *
130
     * @param string $minetype
131
     * @return bool
132
     */
133
    function isMinetypeisAnImage($minetype)
134
    {
135
        return strpos($minetype, "image/") === 0;
136
    }
137
}
138
139
if (!function_exists('notFoundPage')) {
140
    /**
141
     * Display Notfound page of admin or user depends on the user persmissions
142
     *
143
     * @property object $load
144
     * @return string
145
     */
146
    function notFoundPage()
147
    {
148
        $notfound = 'Website\Notfound';
149
150
        if (app()->request->isRequestToAdminManagement()) {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<System\Application>. 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...
151
            $notfound = 'Admin\Notfound';
152
        }
153
154
        return (string) app()->load->action($notfound, 'index', []);
0 ignored issues
show
Documentation introduced by
The property load does not exist on object<System\Application>. 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...
155
    }
156
}
157
158
159
if (!function_exists('getAllSubDires')) {
160
    /**
161
     * Getting all the sub-folders in the given path
162
     *
163
     * @param string $direPath
164
     * @return array
165
     */
166
    function getAllSubDires($direPath)
167
    {
168
        $dirs = [];
169
        $directions = [];
0 ignored issues
show
Unused Code introduced by
$directions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
170
171
        $directions = glob($direPath, GLOB_ONLYDIR);
172
        $dirs = array_merge($directions, $dirs);
173
174
        do {
175
            $direPath .= '**/';
176
            $directions = glob($direPath, GLOB_ONLYDIR);
177
            $dirs = array_merge($directions, $dirs);
178
        } while (!empty($directions));
179
180
        return $dirs;
181
    }
182
}
183
184
if (!function_exists('cleanUrl')) {
185
    /**
186
     * Clean url
187
     *
188
     * @param string $script
189
     * @param string $requestUri
190
     * @return string
191
     */
192
    function cleanUrl($script, $requestUri)
193
    {
194
        if (!in_array($script, ['/', '\\'])) {
195
            $url = preg_replace('#^' . $script . '#', '', $requestUri);
196
        } else {
197
            $url = $requestUri;
198
        }
199
200
        if ($url !== '/') {
201
            $url = rtrim($url, '/');
202
        }
203
204
        return $url;
205
    }
206
}
207