Completed
Push — master ( 08f75a...b24a99 )
by Freek
07:31 queued 04:22
created

helpers.php ➔ fragment_image()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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