MyAccountController::postAccountInfoForm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base\app\Http\Controllers\Auth;
4
5
use Alert;
6
use Backpack\Base\app\Http\Controllers\Controller;
7
use Backpack\Base\app\Http\Requests\AccountInfoRequest;
8
use Backpack\Base\app\Http\Requests\ChangePasswordRequest;
9
use Illuminate\Support\Facades\Hash;
10
11
class MyAccountController extends Controller
12
{
13
    protected $data = [];
14
15
    public function __construct()
16
    {
17
        $this->middleware(backpack_middleware());
18
    }
19
20
    /**
21
     * Show the user a form to change his personal information.
22
     */
23 View Code Duplication
    public function getAccountInfoForm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $this->data['title'] = trans('backpack::base.my_account');
26
        $this->data['user'] = $this->guard()->user();
27
28
        return view('backpack::auth.account.update_info', $this->data);
29
    }
30
31
    /**
32
     * Save the modified personal information for a user.
33
     */
34
    public function postAccountInfoForm(AccountInfoRequest $request)
35
    {
36
        $result = $this->guard()->user()->update($request->except(['_token']));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method update() does only exist in the following implementations of said interface: Illuminate\Foundation\Auth\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
37
38
        if ($result) {
39
            Alert::success(trans('backpack::base.account_updated'))->flash();
40
        } else {
41
            Alert::error(trans('backpack::base.error_saving'))->flash();
42
        }
43
44
        return redirect()->back();
45
    }
46
47
    /**
48
     * Show the user a form to change his login password.
49
     */
50 View Code Duplication
    public function getChangePasswordForm()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $this->data['title'] = trans('backpack::base.my_account');
53
        $this->data['user'] = $this->guard()->user();
54
55
        return view('backpack::auth.account.change_password', $this->data);
56
    }
57
58
    /**
59
     * Save the new password for a user.
60
     */
61
    public function postChangePasswordForm(ChangePasswordRequest $request)
62
    {
63
        $user = $this->guard()->user();
64
        $user->password = Hash::make($request->new_password);
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Documentation introduced by
The property new_password does not exist on object<Backpack\Base\app...\ChangePasswordRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
66
        if ($user->save()) {
67
            Alert::success(trans('backpack::base.account_updated'))->flash();
68
        } else {
69
            Alert::error(trans('backpack::base.error_saving'))->flash();
70
        }
71
72
        return redirect()->back();
73
    }
74
75
    /**
76
     * Get the guard to be used for account manipulation.
77
     *
78
     * @return \Illuminate\Contracts\Auth\StatefulGuard
79
     */
80
    protected function guard()
81
    {
82
        return backpack_auth();
83
    }
84
}
85