Completed
Push — master ( b47a89...8fd593 )
by Sam
02:40
created

User::isCurrentlyLoggedIn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * This file contains only the User class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * A User is a wiki user who has the same username across all projects in an XTools installation.
10
 */
11
class User extends Model
12
{
13
14
    /** @var int The user's ID. */
15
    protected $id;
16
17
    /** @var string The user's username. */
18
    protected $username;
19
20
    /**
21
     * Create a new User given a username.
22
     * @param string $username
23
     */
24
    public function __construct($username)
25
    {
26
        $this->username = ucfirst(trim($username));
27
    }
28
29
    /**
30
     * Get the username.
31
     * @return string
32
     */
33
    public function getUsername()
34
    {
35
        return $this->username;
36
    }
37
38
    /**
39
     * Get the user's ID on the given project.
40
     * @param Project $project
41
     * @return int
42
     */
43
    public function getId(Project $project)
44
    {
45
        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...
46
    }
47
48
    /**
49
     * Get a list of this user's groups on the given project.
50
     * @param Project $project The project.
51
     * @return string[]
52
     */
53
    public function getGroups(Project $project)
54
    {
55
        $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...
56
        $groups = preg_grep('/\*/', $groupsData, PREG_GREP_INVERT);
57
        sort($groups);
58
        return $groups;
59
    }
60
61
    /**
62
     * Get a list of this user's groups on all projects.
63
     * @param Project $project A project to query; if not provided, the default will be used.
64
     */
65
    public function getGlobalGroups(Project $project = null)
66
    {
67
        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...
68
    }
69
70
    /**
71
     * Does this user exist on the given project.
72
     * @param Project $project
73
     * @return bool
74
     */
75
    public function existsOnProject(Project $project)
76
    {
77
        $id = $this->getId($project);
78
        return $id > 0;
79
    }
80
81
    /**
82
     * Is this user an Administrator on the given project?
83
     * @param Project $project The project.
84
     * @return bool
85
     */
86
    public function isAdmin(Project $project)
87
    {
88
        return (false !== array_search('sysop', $this->getGroups($project)));
89
    }
90
91
    /**
92
     * Is this user an anonymous user (IP)?
93
     * @return bool
94
     */
95
    public function isAnon()
96
    {
97
        return filter_var($this->username, FILTER_VALIDATE_IP);
98
    }
99
100
    /**
101
     * Is this user the same as the current XTools user?
102
     * @return bool
103
     */
104
    public function isCurrentlyLoggedIn()
105
    {
106
        $ident = $this->getRepository()->getXtoolsUserInfo();
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 getXtoolsUserInfo() 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...
107
        return isset($ident->username) && $ident->username === $this->getUsername();
108
    }
109
}
110