Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

Controller::sidebarProjects()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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 Illuminate\Foundation\Bus\DispatchesCommands;
15
use Illuminate\Routing\Controller as BaseController;
16
use Illuminate\Foundation\Validation\ValidatesRequests;
17
use Illuminate\Contracts\Auth\Guard;
18
use Tinyissue\Model;
19
20
/**
21
 * Controller is an abstract class for the controller classes.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 */
25
abstract class Controller extends BaseController
26
{
27
    use DispatchesCommands, ValidatesRequests;
0 ignored issues
show
Deprecated Code introduced by
The trait Illuminate\Foundation\Bus\DispatchesCommands has been deprecated with message: since version 5.1. Use the DispatchesJobs trait directly.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
28
29
    /**
30
     * Current logged in user.
31
     *
32
     * @var Guard
33
     */
34
    protected $auth;
35
36
    /**
37
     * Constructor, inject an instance of logged user.
38
     *
39
     * @param \Illuminate\Contracts\Auth\Guard $auth
40
     */
41 55
    public function __construct(Guard $auth)
42
    {
43 55
        $this->auth = $auth;
44 55
    }
45
46
    protected function sidebarProjects()
47
    {
48
        if (!$this->auth->guest()) {
49
            return $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...
50
        }
51
52
        $project = new Model\Project();
53
54
        return $project->publicProjects();
55
    }
56
}
57