Completed
Push — develop ( 1c3d8e...dc7d05 )
by Mohamed
07:44
created

UsersController::getDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Http\Controllers\Administration;
13
14
use Tinyissue\Form\User as Form;
15
use Tinyissue\Http\Controllers\Controller;
16
use Tinyissue\Http\Requests\FormRequest;
17
use Tinyissue\Model\Role;
18
use Tinyissue\Model\User;
19
20
/**
21
 * UsersController is the controller class for managing administration request related to users.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 */
25
class UsersController extends Controller
26
{
27
    /**
28
     * Users index page (List current users).
29
     *
30
     * @param Role $role
31
     *
32
     * @return \Illuminate\View\View
33
     */
34 3
    public function getIndex(Role $role)
35
    {
36 3
        return view('administration.users.index', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('administration.use...le->rolesWithUsers())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 36 which is incompatible with the return type documented by Tinyissue\Http\Controlle...ersController::getIndex of type Illuminate\View\View.
Loading history...
37 3
            'projects' => $this->auth->user()->projects()->get(),
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 projects() does only exist in the following implementations of said interface: Tinyissue\Model\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...
38 3
            'roles'    => $role->rolesWithUsers(),
39
        ]);
40
    }
41
42
    /**
43
     * Add new user.
44
     *
45
     * @param Form $form
46
     *
47
     * @return \Illuminate\View\View
48
     */
49 1
    public function getAdd(Form $form)
50
    {
51 1
        return view('administration.users.add', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('administration.use...)->projects()->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 51 which is incompatible with the return type documented by Tinyissue\Http\Controlle...UsersController::getAdd of type Illuminate\View\View.
Loading history...
52 1
            'form'     => $form,
53 1
            'projects' => $this->auth->user()->projects()->get(),
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 projects() does only exist in the following implementations of said interface: Tinyissue\Model\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...
54
        ]);
55
    }
56
57
    /**
58
     * To save new user.
59
     *
60
     * @param User             $user
61
     * @param FormRequest\User $request
62
     *
63
     * @return \Illuminate\Http\RedirectResponse
64
     */
65 1
    public function postAdd(User $user, FormRequest\User $request)
66
    {
67 1
        $user->createUser($request->all());
68
69 1
        return redirect('administration/users')
70 1
            ->with('notice', trans('tinyissue.user_added'));
71
    }
72
73
    /**
74
     * Edit existing user.
75
     *
76
     * @param User $user
77
     * @param Form $form
78
     *
79
     * @return \Illuminate\View\View
80
     */
81 1
    public function getEdit(User $user, Form $form)
82
    {
83 1
        return view('administration.users.edit', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('administration.use...)->projects()->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 83 which is incompatible with the return type documented by Tinyissue\Http\Controlle...sersController::getEdit of type Illuminate\View\View.
Loading history...
84 1
            'user'     => $user,
85 1
            'form'     => $form,
86 1
            'projects' => $this->auth->user()->projects()->get(),
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 projects() does only exist in the following implementations of said interface: Tinyissue\Model\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...
87
        ]);
88
    }
89
90
    /**
91
     * To update existing user.
92
     *
93
     * @param User             $user
94
     * @param FormRequest\User $request
95
     *
96
     * @return \Illuminate\Http\RedirectResponse
97
     */
98 1
    public function postEdit(User $user, FormRequest\User $request)
99
    {
100 1
        $user->updateUser($request->all());
101
102 1
        return redirect('administration/users')
103 1
            ->with('notice', trans('tinyissue.user_updated'));
104
    }
105
106
    /**
107
     * Delete an existing user.
108
     *
109
     * @param User $user
110
     *
111
     * @return \Illuminate\Http\RedirectResponse
112
     */
113 1
    public function getDelete(User $user)
114
    {
115 1
        $user->delete();
116
117 1
        return redirect('administration/users')
118 1
            ->with('notice', trans('tinyissue.user_deleted'));
119
    }
120
}
121