Completed
Branch develop (a31570)
by Mohamed
08:09 queued 04:45
created

HomeController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 1
cbo 7
dl 0
loc 78
ccs 17
cts 17
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIssues() 0 8 1
A getDashboard() 0 6 1
A getLogout() 0 6 1
A getIndex() 0 8 2
A postSignin() 0 12 2
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;
13
14
use Tinyissue\Form\Login as LoginForm;
15
use Tinyissue\Http\Requests\FormRequest;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model\User;
18
19
/**
20
 * HomeController is the controller class for login, logout, dashboard pages
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
class HomeController extends Controller
25
{
26
    /**
27
     * Public issues view
28
     *
29
     * @param User $user
30
     * @param Project $project
31
     * @return \Illuminate\View\View
32
     */
33
    public function getIssues(User $user, Project $project)
34
    {
35
        return view('index.issues', [
36
            'activeUsers' => $user->activeUsers(),
37
            'projects'    => $project->projectsWidthIssues(Project::STATUS_OPEN, Project::PRIVATE_NO)->get(),
38
            'sidebar'     => 'public',
39
        ]);
40
    }
41
42
    /**
43
     * User dashboard
44
     *
45
     * @return \Illuminate\View\View
46
     */
47 10
    public function getDashboard()
48
    {
49 10
        return view('index.dashboard', [
50 10
            'projects' => $this->auth->user()->projectsWidthActivities(Project::STATUS_OPEN)->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 projectsWidthActivities() 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...
51
        ]);
52
    }
53
54
    /**
55
     * Logout user and redirect to login page
56
     *
57
     * @return \Illuminate\Http\RedirectResponse
58
     */
59 1
    public function getLogout()
60
    {
61 1
        $this->auth->logout();
62
63 1
        return redirect('/')->with('message', trans('tinyissue.loggedout'));
64
    }
65
66
    /**
67
     * Login page
68
     *
69
     * @param LoginForm $form
70
     *
71
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
72
     */
73 12
    public function getIndex(LoginForm $form)
74
    {
75 12
        if ($this->auth->user()) {
76 3
            return redirect('dashboard');
77
        }
78
79 9
        return view('user.login', ['form' => $form]);
80
    }
81
82
    /**
83
     * Attempt to log user in or redirect to login page with error
84
     *
85
     * @param FormRequest\Login $request
86
     *
87
     * @return \Illuminate\Http\RedirectResponse
88
     */
89 8
    public function postSignin(FormRequest\Login $request)
90
    {
91 8
        $credentials = $request->only('email', 'password');
92
93 8
        if ($this->auth->attempt($credentials, $request->has('remember'))) {
94 7
            return redirect()->to('/dashboard');
95
        }
96
97 1
        return redirect('/')
98 1
                        ->withInput($request->only('email'))
99 1
                        ->with('notice-error', trans('tinyissue.password_incorrect'));
100
    }
101
}
102