Completed
Push — master ( b2303c...877715 )
by Thomas
14:04 queued 04:21
created

DefaultToken::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Authentication\Token;
23
24
use OCP\AppFramework\Db\Entity;
25
26
/**
27
 * @method void setId(int $id)
28
 * @method void setUid(string $uid);
29
 * @method void setPassword(string $password)
30
 * @method string getName()
31
 * @method void setToken(string $token)
32
 * @method string getToken()
33
 * @method void setType(string $type)
34
 * @method int getType()
35
 * @method void setLastActivity(int $lastActivity)
36
 * @method int getLastActivity()
37
 */
38
class DefaultToken extends Entity implements IToken {
39
40
	/**
41
	 * @var string user UID
42
	 */
43
	protected $uid;
44
45
	/**
46
	 * @var string login name used for generating the token
47
	 */
48
	protected $loginName;
49
50
	/**
51
	 * @var string encrypted user password
52
	 */
53
	protected $password;
54
55
	/**
56
	 * @var string token name (e.g. browser/OS)
57
	 */
58
	protected $name;
59
60
	/**
61
	 * @var string
62
	 */
63
	protected $token;
64
65
	/**
66
	 * @var int
67
	 */
68
	protected $type;
69
70
	/**
71
	 * @var int
72
	 */
73
	protected $lastActivity;
74
75
	/**
76
	 * @var int
77
	 */
78
	protected $lastCheck;
79
80
	public function getId() {
81
		return $this->id;
82
	}
83
84
	public function getUID() {
85
		return $this->uid;
86
	}
87
88
	/**
89
	 * Get the login name used when generating the token
90
	 *
91
	 * @return string
92
	 */
93
	public function getLoginName() {
94
		return parent::getLoginName();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getLoginName() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken. 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...
95
	}
96
97
	/**
98
	 * Get the (encrypted) login password
99
	 *
100
	 * @return string
101
	 */
102
	public function getPassword() {
103
		return parent::getPassword();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getPassword() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken. 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...
104
	}
105
106
	public function jsonSerialize() {
107
		return [
108
			'id' => $this->id,
109
			'name' => $this->name,
110
			'lastActivity' => $this->lastActivity,
111
			'type' => $this->type,
112
			'canDelete' => true,
113
		];
114
	}
115
116
	/**
117
	 * Get the timestamp of the last password check
118
	 *
119
	 * @return int
120
	 */
121
	public function getLastCheck() {
122
		return parent::getLastCheck();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method getLastCheck() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken. 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...
123
	}
124
125
	/**
126
	 * Get the timestamp of the last password check
127
	 *
128
	 * @param int $time
129
	 */
130
	public function setLastCheck($time) {
131
		parent::setLastCheck($time);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method setLastCheck() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken. 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...
132
	}
133
134
	/**
135
	 * @param string $name
136
	 */
137
	public function setName($name) {
138
		if (strlen($name) < 1) {
139
			throw new \InvalidArgumentException();
140
		}
141
		parent::setName($name);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method setName() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Tagging\Tag. 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...
142
	}
143
144
	/**
145
	 * @param string $loginName
146
	 */
147
	public function setLoginName($loginName) {
148
		if (strlen($loginName) < 1) {
149
			throw new \InvalidArgumentException();
150
		}
151
		parent::setLoginName($loginName);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OCP\AppFramework\Db\Entity as the method setLoginName() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken. 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...
152
	}
153
154
}
155