Passed
Pull Request — master (#343)
by
unknown
04:45
created

formatProblemSolvedTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
use Illuminate\Support\Str;
4
use Illuminate\Support\Carbon;
5
use Illuminate\Support\HtmlString;
6
use Illuminate\Container\Container;
7
use Illuminate\Queue\CallQueuedClosure;
8
use Illuminate\Contracts\Bus\Dispatcher;
9
use Illuminate\Queue\SerializableClosure;
10
use Illuminate\Contracts\Auth\Access\Gate;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gate. Consider defining an alias.

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...
11
use Illuminate\Contracts\Support\Responsable;
12
use Illuminate\Contracts\Routing\UrlGenerator;
13
use Illuminate\Foundation\Bus\PendingDispatch;
14
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Response. Consider defining an alias.

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...
15
use Illuminate\Contracts\Debug\ExceptionHandler;
16
use Illuminate\Contracts\Routing\ResponseFactory;
17
use Illuminate\Contracts\Auth\Factory as AuthFactory;
18
use Illuminate\Contracts\View\Factory as ViewFactory;
19
use Illuminate\Http\Exceptions\HttpResponseException;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpResponseException. Consider defining an alias.

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...
20
use Illuminate\Contracts\Cookie\Factory as CookieFactory;
21
use Symfony\Component\Debug\Exception\FatalThrowableError;
22
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
23
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
24
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
25
use Illuminate\Support\Facades\DB;
26
use GrahamCampbell\Markdown\Facades\Markdown;
27
use App\Models\Eloquent\MessageModel;
28
29
30
if (!function_exists('version')) {
31
    function version()
32
    {
33
        $version=new Version(
34
            '0.0.0',
35
            base_path()
36
        );
37
        return $version->getVersion();
38
    }
39
}
40
41
if (!function_exists('getCustomUrl')) {
42
    function getCustomUrl()
43
    {
44
        $customUrlCached=Cache::tags(['custom'])->get('url');
45
46
        if ($customUrlCached==null) {
47
            $urls=DB::table("custom_url")->where(["available"=>1])->get()->all();
48
            Cache::tags(['custom'])->put('url', $urls, 1200);
49
            return $urls;
50
        }
51
52
        return $customUrlCached;
53
    }
54
}
55
56
if (!function_exists('emailVerified')) {
57
    function emailVerified()
58
    {
59
        if (Auth::check()) {
60
            return !is_null(Auth::user()->email_verified_at);
0 ignored issues
show
Bug Best Practice introduced by
The property email_verified_at does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
        }
62
63
        return null;
64
    }
65
}
66
67
if (! function_exists('babel_path')) {
68
    /**
69
     * Get the path to the application folder.
70
     *
71
     * @param  string  $path
72
     * @return string
73
     */
74
    function babel_path($path = '')
75
    {
76
        return app('path').DIRECTORY_SEPARATOR.'Babel'.($path ? DIRECTORY_SEPARATOR.$path : $path);
0 ignored issues
show
Bug introduced by
Are you sure app('path') of type Illuminate\Contracts\Foundation\Application|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return /** @scrutinizer ignore-type */ app('path').DIRECTORY_SEPARATOR.'Babel'.($path ? DIRECTORY_SEPARATOR.$path : $path);
Loading history...
77
    }
78
}
79
80
if (! function_exists('glob_recursive')) {
81
    /**
82
     * Find pathnames matching a pattern recursively.
83
     *
84
     * @param  string  $pattern The pattern. No tilde expansion or parameter substitution is done.
85
     * @param  int     $flags   Valid flags: GLOB_MARK
86
     * @return array|false      an array containing the matched files/directories, an empty array if no file matched or false on error.
87
     */
88
    function glob_recursive($pattern, $flags = 0)
89
    {
90
        $files = glob($pattern, $flags);
91
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
92
            $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type false; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            $files = array_merge(/** @scrutinizer ignore-type */ $files, glob_recursive($dir.'/'.basename($pattern), $flags));
Loading history...
Bug introduced by
It seems like glob_recursive($dir . '/...name($pattern), $flags) can also be of type false; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            $files = array_merge($files, /** @scrutinizer ignore-type */ glob_recursive($dir.'/'.basename($pattern), $flags));
Loading history...
93
        }
94
        return $files;
95
    }
96
}
97
98
if (!function_exists('adminMenu')) {
99
    function adminMenu()
100
    {
101
        return json_decode(file_get_contents(app_path('Admin/menu.json')), true);
102
    }
103
}
104
105
if (!function_exists('getOpenSearchXML')) {
106
    function getOpenSearchXML()
107
    {
108
        $url=config("app.url");
109
110
        return '<?xml version="1.0" encoding="UTF-8"?>
111
        <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
112
            <ShortName>NOJ</ShortName>
113
            <Description>Gracefully Search NOJ Problems and others.</Description>
114
            <InputEncoding>UTF-8</InputEncoding>
115
            <Image width="16" height="16" type="image/x-icon">'.$url.'/favicon.ico</Image>
116
            <Url type="text/html" method="get" template="'.$url.'/search/?q={searchTerms}&amp;tab=problems&amp;opensearch=1" />
117
            <moz:SearchForm>'.$url.'/search</moz:SearchForm>
118
        </OpenSearchDescription>';
119
    }
120
}
121
122
if (!function_exists('delFile')) {
123
    function delFile($dirName)
124
    {
125
        if (file_exists($dirName) && $handle=opendir($dirName)) {
126
            while (false!==($item = readdir($handle))) {
127
                if ($item!= "." && $item != "..") {
128
                    if (file_exists($dirName.'/'.$item) && is_dir($dirName.'/'.$item)) {
129
                        delFile($dirName.'/'.$item);
130
                    } else {
131
                        if (unlink($dirName.'/'.$item)) {
132
                            return true;
133
                        }
134
                    }
135
                }
136
            }
137
            closedir($handle);
138
        }
139
    }
140
}
141
142
if (!function_exists('convertMarkdownToHtml')) {
143
    function convertMarkdownToHtml($md)
144
    {
145
        return is_string($md)?Markdown::convertToHtml($md):'';
146
    }
147
}
148
149
if (!function_exists('sendMessage')) {
150
    function sendMessage($config)
151
    {
152
        return MessageModel::send($config);
153
    }
154
}
155
156
if (!function_exists('formatHumanReadableTime')) {
157
    function formatHumanReadableTime($date)
158
    {
159
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
160
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
161
162
        $now=time();
163
        $unix_date=strtotime($date);
164
165
        if (empty($unix_date)) {
166
            return "Bad date";
167
        }
168
169
        if ($now>$unix_date) {
170
            $difference=$now-$unix_date;
171
            $tense="ago";
172
        } else {
173
            $difference=$unix_date-$now;
174
            $tense="from now";
175
        }
176
177
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
178
            $difference/=$lengths[$j];
179
        }
180
181
        $difference=round($difference);
182
183
        if ($difference!=1) {
184
            $periods[$j].="s";
185
        }
186
187
        return "$difference $periods[$j] {$tense}";
188
    }
189
}
190
191
if (!function_exists('formatProblemSolvedTime')) {
192
    function formatProblemSolvedTime($seconds)
193
    {
194
        if ($seconds>3600) {
195
            $hours=intval($seconds / 3600);
196
            $minutes=$seconds % 3600;
197
            $time=$hours.":".gmstrftime('%M:%S', $minutes);
198
        } else {
199
            $time=gmstrftime('%H:%M:%S', $seconds);
200
        }
201
        return $time;
202
    }
203
}
204