Passed
Branch master (f6241b)
by refat
03:06
created

helpers.php ➔ array_get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use System\Application;
4
use System\File;
5
6
if (!function_exists('pre')) {
7
    function pre($var)
8
    {
9
        echo '<pre>';
10
        print_r($var);
11
        echo '</pre>';
12
    }
13
}
14
15
if (!function_exists('array_get')) {
16
    function array_get($array, $key, $default = null)
17
    {
18
        return ($array[$key] || $array[$key] == '0') ? $array[$key] : $default;
19
    }
20
}
21
22
if (!function_exists('app')) {
23
    function app()
24
    {
25
        return Application::getInstance(new File(__DIR__));
26
    }
27
}
28
29
if (!function_exists('_e')) {
30
    function _e($value)
31
    {
32
        $value = trim($value);
33
        $value = preg_replace('# {2,}#', ' ', $value);
34
        $value = htmlspecialchars($value);
35
        return $value;
36
    }
37
}
38
39
if (!function_exists('getLastParameter')) {
40
    function getLastParameter($index)
41
    {
42
        $array = explode('/', $index);
43
        return end($array);
44
    }
45
}
46
47
if (!function_exists('userId')) {
48
    function userId()
49
    {
50
        return getLastParameter(app()->request->baseUrl());
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...
51
    }
52
}
53
54
if (!function_exists('url')) {
55
    function url($path)
56
    {
57
        return app()->url->link($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...
58
    }
59
}
60
61
if (!function_exists('assets')) {
62
    function assets($path = null)
63
    {
64
        return app()->url->link('public' . DS . $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...
65
    }
66
}
67
68
if (!function_exists('remove_space')) {
69
    function remove_space($str)
70
    {
71
        return str_replace(' ', '-', $str);
72
    }
73
}
74
75
if (!function_exists('remove_dash')) {
76
    function remove_dash($str)
77
    {
78
        return str_replace('-', ' ', $str);
79
    }
80
}
81
82
if (!function_exists('text_char_limit')) {
83
    function text_char_limit($text, $limit)
84
    {
85
        if (strlen($text) > $limit) {
86
            return substr($text, 0, $limit) . '...';
87
        }
88
    }
89
}
90
91
if (!function_exists('array_equal')) {
92
    function array_equal($a, $b)
93
    {
94
        return (
95
        is_array($a)
96
        && is_array($b)
97
        && count($a) == count($b)
98
        && array_diff($a, $b) === array_diff($b, $a)
99
        );
100
    }
101
}
102
103
if (!function_exists('isExtesntionAllowed')) {
104
    function isExtesntionAllowed($key, $extension)
105
    {
106
        $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...
107
108
        return in_array($extension, $allowExtesntions);
109
    }
110
}
111
112
if (!function_exists('isImage')) {
113
    function isImage($minetype)
114
    {
115
        return strpos($minetype, "image/") === 0;
116
    }
117
}
118
119
if (!function_exists('notFoundPage')) {
120
121
    function notFoundPage()
122
    {
123
        $notfound = 'Website\Notfound';
124
125
        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...
126
            $notfound = 'Admin\Notfound';
127
        }
128
129
        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...
130
    }
131
}
132
133
if (!function_exists('getAllSubDires')) {
134
135
    function getAllSubDires($direPath)
136
    {
137
        $dirs = [];
138
        $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...
139
140
        $directions = glob($direPath, GLOB_ONLYDIR);
141
        $dirs = array_merge($directions, $dirs);
142
143
        do {
144
            $direPath .= '**/';
145
            $directions = glob($direPath, GLOB_ONLYDIR);
146
            $dirs = array_merge($directions, $dirs);
147
        } while (!empty($directions));
148
149
        return $dirs;
150
    }
151
}
152
153