Completed
Push — master ( fcf66f...4020b1 )
by Sam
02:59
created

User   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 55
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUsername() 0 4 1
A getId() 0 4 1
A getGroups() 0 3 1
A userRightsUrl() 0 5 1
1
<?php
2
3
namespace Xtools;
4
5
/**
6
 * A User is a wiki user who has the same username across all projects in an Xtools installation.
7
 */
8
class User extends Model
9
{
10
11
    /** @var int */
12
    protected $id;
13
14
    /** @var string */
15
    protected $username;
16
17
    /**
18
     * Create a new User given a username.
19
     * @param string $username
20
     */
21
    public function __construct($username)
22
    {
23
        $this->username = ucfirst($username);
24
    }
25
26
    /**
27
     * Get the username.
28
     * @return string
29
     */
30
    public function getUsername()
31
    {
32
        return $this->username;
33
    }
34
35
    /**
36
     * Get the user's ID on the given project.
37
     * @param Project $project
38
     * @return int
39
     */
40
    public function getId(Project $project)
41
    {
42
        return $this->getRepository()->getId($project->getDatabaseName(), $this->getUsername());
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Xtools\Repository as the method getId() does only exist in the following sub-classes of Xtools\Repository: Xtools\UserRepository. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
43
    }
44
45
    /**
46
     * Get a list of this user's groups on the given project.
47
     */
48
    public function getGroups(Project $project)
0 ignored issues
show
Unused Code introduced by
The parameter $project is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
    }
51
52
    /**
53
     * Get the full URL to Special:UserRights for this user on the given project.
54
     * @param Project $project
55
     * @return string
56
     */
57
    public function userRightsUrl(Project $project)
58
    {
59
        return $project->getUrl() . $project->getScriptPath() . "?title=Special:UserRights&user=" .
60
               $this->getUsername();
61
    }
62
}
63