Completed
Push — master ( 717411...f84bc1 )
by Sam
02:56
created

User::getGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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
     * @return string[]
48
     */
49
    public function getGroups(Project $project)
50
    {
51
        $groupsData = $this->getRepository()->getGroups($project, $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 getGroups() 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...
52
        $groups = preg_grep('/\*/', $groupsData, PREG_GREP_INVERT);
53
        sort($groups);
54
        return $groups;
55
    }
56
57
    /**
58
     * Get a list of this user's groups on all projects.
59
     * @param Project $project A project to query; if not provided, the default will be used.
60
     */
61
    public function getGlobalGroups(Project $project = null)
62
    {
63
        return $this->getRepository()->getGlobalGroups($this->getUsername(), $project);
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 getGlobalGroups() 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...
64
    }
65
66
    /**
67
     * Get the full URL to Special:UserRights for this user on the given project.
68
     * @param Project $project
69
     * @return string
70
     */
71
    public function userRightsUrl(Project $project)
72
    {
73
        return $project->getUrl() . $project->getScriptPath() . "?title=Special:UserRights&user=" .
74
               $this->getUsername();
75
    }
76
77
    /**
78
     * Does this user exist on the given project.
79
     * @param Project $project
80
     * @return bool
81
     */
82
    public function existsOnProject(Project $project)
83
    {
84
        $id = $this->getId($project);
85
        return $id > 0;
86
    }
87
88
    /**
89
     * Is this user an Administrator on the given project?
90
     * @return bool
91
     */
92
    public function isAdmin(Project $project)
93
    {
94
        return (false !== array_search('sysop', $this->getGroups($project)));
95
    }
96
}
97