Completed
Push — master ( 188ffb...31684b )
by Maxime
02:48
created

UserController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 24.62 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 16
loc 65
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getProfile() 0 4 1
A postProfile() 0 11 2
A postSearchWithRole() 0 7 1
A postUnLock() 8 8 1
A postLock() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Distilleries\Expendable\Http\Controllers\Backend;
2
3
use Distilleries\Expendable\Contracts\LayoutManagerContract;
4
use Distilleries\Expendable\Http\Datatables\User\UserDatatable;
5
use Distilleries\Expendable\Http\Forms\User\UserForm;
6
use Distilleries\Expendable\Http\Controllers\Backend\Base\BaseComponent;
7
use Distilleries\Expendable\Models\User;
8
use Illuminate\Contracts\Auth\Guard;
9
use Illuminate\Http\Request;
10
11
class UserController extends BaseComponent {
12
13
14
    public function __construct(UserDatatable $datatable, UserForm $form, User $model, LayoutManagerContract $layoutManager)
15
    {
16
        parent::__construct($model, $layoutManager);
17
        $this->datatable = $datatable;
18
        $this->form      = $form;
19
    }
20
21
    // ------------------------------------------------------------------------------------------------
22
    // ------------------------------------------------------------------------------------------------
23
    // ------------------------------------------------------------------------------------------------
24
25
26
    public function getProfile(Guard $auth)
27
    {
28
        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, Illuminate\Foundation\Auth\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
    public function postProfile(Request $request, Guard $auth)
34
    {
35
        if ($auth->user()->getAuthIdentifier() == $request->get($this->model->getKeyName()))
36
        {
37
            $this->postEdit($request);
38
39
            return $this->getProfile($auth);
40
        }
41
42
        abort(403, trans('permission-util::errors.unthorized'));
0 ignored issues
show
Bug introduced by
It seems like trans('permission-util::errors.unthorized') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, abort() 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...
43
    }
44
45
    // ------------------------------------------------------------------------------------------------
46
47
    public function postSearchWithRole(Request $request)
48
    {
49
        $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
        $this->postSearch($request, $query);
51
52
        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
}