Completed
Push — master ( 22caaa...988d36 )
by Maxime
15:13 queued 03:59
created

UserController::postProfile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.0116
1
<?php namespace Distilleries\Expendable\Http\Controllers\Admin;
2
3
use Distilleries\Expendable\Contracts\LayoutManagerContract;
4
use Distilleries\Expendable\Datatables\User\UserDatatable;
5
use Distilleries\Expendable\Forms\User\UserForm;
6
use Distilleries\Expendable\Http\Controllers\Admin\Base\BaseComponent;
7
use Distilleries\Expendable\Models\User;
8
use Illuminate\Auth\Guard;
9
use Illuminate\Http\Request;
10
11
class UserController extends BaseComponent {
12
13
14 52
    public function __construct(UserDatatable $datatable, UserForm $form, User $model, LayoutManagerContract $layoutManager)
15
    {
16 52
        parent::__construct($model, $layoutManager);
17 52
        $this->datatable = $datatable;
18 52
        $this->form      = $form;
19 52
    }
20
21
    // ------------------------------------------------------------------------------------------------
22
    // ------------------------------------------------------------------------------------------------
23
    // ------------------------------------------------------------------------------------------------
24
25
26 8
    public function getProfile(Guard $auth)
27
    {
28 8
        return $this->getEdit($auth->user()->getKey());
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 getKey() does only exist in the following implementations of said interface: Distilleries\Expendable\Models\User, Orchestra\Testbench\TestCase\User, User, User, UserImplement.

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...
29
    }
30
31
    // ------------------------------------------------------------------------------------------------
32
33 8
    public function postProfile(Request $request, Guard $auth)
34
    {
35 8
        if ($auth->user()->getAuthIdentifier() == $request->get($this->model->getKeyName()))
36 8
        {
37 4
            $this->postEdit($request);
38
39 4
            return $this->getProfile($auth);
40
        }
41
42 4
        abort(403, trans('permission-util::errors.unthorized'));
43
    }
44
45
    // ------------------------------------------------------------------------------------------------
46
47 4
    public function postSearchWithRole(Request $request)
48
    {
49 4
        $query = $this->model->where('role_id', '=', $request->get('role'));
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Distilleries\Expendable\Models\BaseModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50 4
        $this->postSearch($request, $query);
51
52 4
        return $this->postSearch($request, $query);
53
    }
54
55
56 View Code Duplication
    public function postUnLock(Request $request){
57
58
        $model = $this->model->findOrFail($request->get('id'));
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<Distilleries\Expendable\Models\BaseModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
59
        $model->nb_of_try = 0;
60
        $model->save();
61
62
        return redirect()->back();
63
    }
64
65
66 View Code Duplication
    public function postLock(Request $request){
67
68
        $model = $this->model->findOrFail($request->get('id'));
0 ignored issues
show
Documentation Bug introduced by
The method findOrFail does not exist on object<Distilleries\Expendable\Models\BaseModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
69
        $model->nb_of_try = config('expendable.auth.nb_of_try');
70
        $model->save();
71
72
        return redirect()->back();
73
    }
74
75
}