Completed
Push — master ( 40ffb0...19db0b )
by Mathieu
03:05
created

LogoutTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 59
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A deleteUserAuthTokens() 0 11 1
A authRequired() 0 4 1
A showHeaderMenu() 0 4 1
A showFooterMenu() 0 4 1
1
<?php
2
3
namespace Charcoal\Admin\Template;
4
5
// Local parent namespace dependencies
6
use \Charcoal\Admin\AdminTemplate;
7
use \Charcoal\Admin\User;
8
9
use \Charcoal\Admin\Object\AuthToken;
10
11
/**
12
 * Logout template
13
 */
14
class LogoutTemplate extends AdminTemplate
15
{
16
17
    /**
18
     * @param array|\ArayAccess $data
19
     */
20
    public function __construct($data)
21
    {
22
        $user = User::getAuthenticated();
23
        if($user) {
24
            $user->logout();
25
            $this->deleteUserAuthTokens($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Charcoal\User\UserInterface>, but the function expects a object<Charcoal\Admin\Template\UserInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
        }
27
28
        parent::__construct($data);
29
    }
30
31
    /**
32
     * @param UserInterface $user
33
     * @return LogoutTemplate Chainable
34
     */
35
    private function deleteUserAuthTokens($user)
36
    {
37
        $token = new AuthToken([
38
            'logger' => $this->logger
39
        ]);
40
41
        $table = $token->source()->table();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Charcoal\Source\SourceInterface as the method table() does only exist in the following implementations of said interface: Charcoal\Source\DatabaseSource.

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...
42
        $q = 'delete from '.$table.' where username = :username';
43
        $token->source()->dbQuery($q, ['username'=>$user->username()]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Charcoal\Source\SourceInterface as the method dbQuery() does only exist in the following implementations of said interface: Charcoal\Source\DatabaseSource.

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...
44
        return $this;
45
    }
46
47
    /**
48
     * Authentication is obviously never required for the login page.
49
     *
50
     * @return boolean
51
     */
52
    protected function authRequired()
53
    {
54
        return false;
55
    }
56
57
    /**
58
     * @return boolean
59
     */
60
    public function showHeaderMenu()
61
    {
62
        return false;
63
    }
64
65
    /**
66
     * @return boolean
67
     */
68
    public function showFooterMenu()
69
    {
70
        return false;
71
    }
72
}
73