Completed
Push — master ( 3745d5...788192 )
by Freek
03:26
created

helpers.php ➔ fragment_slug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Collection;
4
5
function article(\App\Models\Enums\SpecialArticle $specialArticle): App\Models\Article
6
{
7
    return App\Repositories\ArticleRepository::findSpecialArticle($specialArticle);
8
}
9
10
function content_locale(): string
11
{
12
    return \App\Services\Locale\CurrentLocale::getContentLocale();
13
}
14
15
/**
16
 * @return \App\Services\Auth\Back\User|\App\Services\Auth\Front\User|null
17
 *
18
 * @throws \Exception
19
 */
20
function current_user()
21
{
22
    if (request()->isFront()) {
23
        return current_front_user();
24
    }
25
26
    if (request()->isBack()) {
27
        return current_back_user();
28
    }
29
30
    throw new \Exception('Coud not determine current user');
31
}
32
33
/**
34
 * @return \App\Services\Auth\Front\User|null
35
 */
36
function current_front_user()
37
{
38
    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...
39
        return;
40
    }
41
42
    return auth()->guard('front')->user();
43
}
44
45
/**
46
 * @return \App\Services\Auth\Back\User|null
47
 */
48
function current_back_user()
49
{
50
    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...
51
        return;
52
    }
53
54
    return auth()->guard('back')->user();
55
}
56
57
function diff_date_for_humans(Carbon\Carbon $date): string
58
{
59
    return (new Jenssegers\Date\Date($date->timestamp))->ago();
60
}
61
62
function el(string $tag, $attributes = null, $contents = null): string
63
{
64
    return \Spatie\HtmlElement\HtmlElement::render($tag, $attributes, $contents);
65
}
66
67
function fragment($name, array $replacements = []): string
68
{
69
    return trans($name, $replacements);
70
}
71
72
function fragment_slug($name, array $replacements = []): string
73
{
74
    $translation = fragment($name, $replacements);
75
76
    return str_slug($translation);
77
}
78
79
function locale(): string
80
{
81
    return app()->getLocale();
82
}
83
84
function locales(): Collection
85
{
86
    return collect(config('app.locales'));
87
}
88
89
function login_url(): string
90
{
91
    return request()->isFront() ?
92
        action('Front\Auth\LoginController@showLoginForm') :
93
        action('Back\Auth\LoginController@showLoginForm');
94
}
95
96
function logout_url(): string
97
{
98
    return request()->isFront() ?
99
        action('Front\Auth\LoginController@logout') :
100
        action('Back\Auth\LoginController@logout');
101
}
102
103
function roman_year(int $year = null): string
104
{
105
    $year = $year ?? date('Y');
106
107
    $romanNumerals = [
108
        'M' => 1000,
109
        'CM' => 900,
110
        'D' => 500,
111
        'CD' => 400,
112
        'C' => 100,
113
        'XC' => 90,
114
        'L' => 50,
115
        'XL' => 40,
116
        'X' => 10,
117
        'IX' => 9,
118
        'V' => 5,
119
        'IV' => 4,
120
        'I' => 1,
121
    ];
122
123
    $result = '';
124
125
    foreach ($romanNumerals as $roman => $yearNumber) {
126
        // Divide to get  matches
127
        $matches = intval($year / $yearNumber);
128
129
        // Assign the roman char * $matches
130
        $result .= str_repeat($roman, $matches);
131
132
        // Substract from the number
133
        $year = $year % $yearNumber;
134
    }
135
136
    return $result;
137
}
138
139
function register_url(): string
140
{
141
    return action('Front\Auth\RegisterController@showRegistrationForm');
142
}
143
144
function translate_field_name(string $name, string $locale = ''): string
145
{
146
    $locale = $locale ?? content_locale();
147
148
    return "translated_{$locale}_{$name}";
149
}
150
151
/**
152
 * Validate some data.
153
 *
154
 * @param string|array $fields
155
 * @param string|array $rules
156
 *
157
 * @return bool
158
 */
159
function validate($fields, $rules): bool
160
{
161
    if (! is_array($fields)) {
162
        $fields = ['default' => $fields];
163
    }
164
165
    if (! is_array($rules)) {
166
        $rules = ['default' => $rules];
167
    }
168
169
    return Validator::make($fields, $rules)->passes();
170
}
171