ProfileController::getFilename()   A
last analyzed

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 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use Yajra\CMS\Events\Users\ProfileWasUpdated;
6
use Yajra\CMS\Http\Requests\ProfileFormRequest;
7
8
class ProfileController extends Controller
9
{
10
    /**
11
     * Show the form for editing the specified resource.
12
     *
13
     * @return \Illuminate\Http\Response
14
     */
15
    public function edit()
16
    {
17
        return view('administrator.profile.edit');
18
    }
19
20
    /**
21
     * Update the specified resource in storage.
22
     *
23
     * @param  \Yajra\CMS\Http\Requests\ProfileFormRequest $request
24
     * @return \Illuminate\Http\Response
25
     * @throws \Laracasts\Presenter\Exceptions\PresenterException
26
     */
27
    public function update(ProfileFormRequest $request)
28
    {
29
        $profile = $request->user();
30
        $profile->fill($request->only(['first_name', 'last_name', 'email']));
31
32
        if ($request->get('password')) {
33
            $profile->password = bcrypt($request->get('password'));
34
        }
35
36
        if ((null !== $request->file('avatar')) && $request->file('avatar')->isValid()) {
37
            $fileType = $request->file('avatar')->getClientOriginalExtension();
38
            $filename = auth()->user()->id . '-' . $request->file('avatar')->getFilename() . '.' . $fileType;
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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
            $request->file('avatar')->move('media/avatar', $filename);
40
            $profile->avatar = url('media/avatar/' . $filename);
41
        }
42
43
        $profile->save();
44
45
        event(new ProfileWasUpdated($profile));
46
47
        flash()->success(trans('cms::profile.alert.success'));
0 ignored issues
show
Bug introduced by
It seems like trans('cms::profile.alert.success') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, Laracasts\Flash\FlashNotifier::success() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
49
        return redirect()->route('administrator.profile.edit');
50
    }
51
52
    /**
53
     * Remove current user's avatar.
54
     *
55
     * @return \Illuminate\Http\RedirectResponse
56
     */
57
    public function removeAvatar()
58
    {
59
        $profile         = auth()->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
60
        $profile->avatar = '';
61
        $profile->save();
62
63
        event(new ProfileWasUpdated($profile));
64
65
        return redirect()->route('administrator.profile.edit');
66
    }
67
68
    /**
69
     * @param \Yajra\CMS\Http\Requests\ProfileFormRequest $request
70
     * @return string
71
     * @throws \Laracasts\Presenter\Exceptions\PresenterException
72
     */
73
    protected function getFilename(ProfileFormRequest $request)
74
    {
75
        $fileName = $request->user()->id . '.' . $request->file('avatar')->getClientOriginalExtension();
76
77
        return $fileName;
78
    }
79
}
80