helpers.php ➔ logout_url()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use App\Models\Fragment;
4
use Carbon\Carbon;
5
use Illuminate\Support\Collection;
6
7
function article(string $specialArticle): App\Models\Article
8
{
9
    return App\Repositories\ArticleRepository::findSpecialArticle($specialArticle);
10
}
11
12
function content_locale(): string
13
{
14
    return \App\Services\Locale\CurrentLocale::getContentLocale();
15
}
16
17
/**
18
 * @return \App\Services\Auth\Back\User|\App\Services\Auth\Front\User|null
19
 *
20
 * @throws \Exception
21
 */
22
function current_user()
23
{
24
    if (request()->isFront()) {
25
        return current_front_user();
26
    }
27
28
    if (request()->isBack()) {
29
        return current_back_user();
30
    }
31
32
    throw new \Exception('Coud not determine current user');
33
}
34
35
/**
36
 * @return \App\Services\Auth\Front\User|null
37
 */
38
function current_front_user()
39
{
40
    if (! auth()->guard('front')->check()) {
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
41
        return;
42
    }
43
44
    return auth()->guard('front')->user();
45
}
46
47
/**
48
 * @return \App\Services\Auth\Back\User|null
49
 */
50
function current_back_user()
51
{
52
    if (! auth()->guard('back')->check()) {
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
        return;
54
    }
55
56
    return auth()->guard('back')->user();
57
}
58
59
function diff_date_for_humans(Carbon $date): string
60
{
61
    return (new Jenssegers\Date\Date($date->timestamp))->ago();
62
}
63
64
function el(string $tag, $attributes = null, $contents = null): string
65
{
66
    return \Spatie\HtmlElement\HtmlElement::render($tag, $attributes, $contents);
67
}
68
69
function fragment($name, array $replacements = []): string
70
{
71
    return trans($name, $replacements);
72
}
73
74
function fragment_slug($name, array $replacements = []): string
75
{
76
    $translation = fragment($name, $replacements);
77
78
    return str_slug($translation);
79
}
80
81
function fragment_image($name, $conversion = 'thumb'): string
82
{
83
    if (! str_contains($name, '.')) {
84
        return $name;
85
    }
86
87
    [$group, $key] = explode('.', $name, 2);
0 ignored issues
show
Bug introduced by
The variable $group does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
88
89
    $fragment = Fragment::with('media')
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
90
        ->where('group', $group)
91
        ->where('key', $key)
92
        ->first();
93
94
    if (! $fragment) {
95
        return $name;
96
    }
97
98
    return $fragment->getFirstMediaUrl('images', $conversion);
99
}
100
101
function locale(): string
102
{
103
    return app()->getLocale();
104
}
105
106
function locales(): Collection
107
{
108
    return collect(config('app.locales'));
109
}
110
111
function login_url(): string
112
{
113
    return request()->isFront() ?
114
        action('Front\Auth\LoginController@showLoginForm') :
115
        action('Back\Auth\LoginController@showLoginForm');
116
}
117
118
function logout_url(): string
119
{
120
    return request()->isFront() ?
121
        action('Front\Auth\LoginController@logout') :
122
        action('Back\Auth\LoginController@logout');
123
}
124
125
function roman_year(int $year = null): string
126
{
127
    $year = $year ?? date('Y');
128
129
    $romanNumerals = [
130
        'M' => 1000,
131
        'CM' => 900,
132
        'D' => 500,
133
        'CD' => 400,
134
        'C' => 100,
135
        'XC' => 90,
136
        'L' => 50,
137
        'XL' => 40,
138
        'X' => 10,
139
        'IX' => 9,
140
        'V' => 5,
141
        'IV' => 4,
142
        'I' => 1,
143
    ];
144
145
    $result = '';
146
147
    foreach ($romanNumerals as $roman => $yearNumber) {
148
        // Divide to get  matches
149
        $matches = intval($year / $yearNumber);
150
151
        // Assign the roman char * $matches
152
        $result .= str_repeat($roman, $matches);
153
154
        // Substract from the number
155
        $year = $year % $yearNumber;
156
    }
157
158
    return $result;
159
}
160
161
function carbon(string $date = null, string $format = null): Carbon
162
{
163
    if (func_num_args() === 0) {
164
        return Carbon::now();
165
    }
166
167
    if (is_null($date)) {
168
        throw new InvalidArgumentException("Date can't be null");
169
    }
170
171
    if (is_null($format)) {
172
        return Carbon::parse($date);
173
    }
174
175
    return Carbon::createFromFormat($format, $date);
176
}
177
178
function register_url(): string
179
{
180
    return action('Front\Auth\RegisterController@showRegistrationForm');
181
}
182
183
function translate_field_name(string $name, string $locale = ''): string
184
{
185
    $locale = $locale ?? content_locale();
186
187
    return "translated_{$locale}_{$name}";
188
}
189
190
/**
191
 * Validate some data.
192
 *
193
 * @param string|array $fields
194
 * @param string|array $rules
195
 *
196
 * @return bool
197
 */
198
function validate($fields, $rules): bool
199
{
200
    if (! is_array($fields)) {
201
        $fields = ['default' => $fields];
202
    }
203
204
    if (! is_array($rules)) {
205
        $rules = ['default' => $rules];
206
    }
207
208
    return Validator::make($fields, $rules)->passes();
209
}
210