Completed
Push — master ( 19d552...6b730b )
by Morris
21:07 queued 10:33
created

PublicKeyToken   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 179
Duplicated Lines 10.06 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 18
loc 179
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A getId() 0 3 1
A getUID() 0 3 1
A getLoginName() 0 3 1
A getPassword() 0 3 1
A jsonSerialize() 9 9 1
A getLastCheck() 0 3 1
A setLastCheck() 0 3 1
A getScope() 0 8 2
A getScopeAsArray() 9 9 2
A setScope() 0 7 2
A getName() 0 3 1
A getRemember() 0 3 1
A setToken() 0 3 1
A setPassword() 0 3 1
A setExpires() 0 3 1
A getExpires() 0 3 1
A setPasswordInvalid() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/** @noinspection ALL */
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
6
 *
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\Authentication\Token;
27
28
use OCP\AppFramework\Db\Entity;
29
30
/**
31
 * @method void setId(int $id)
32
 * @method void setUid(string $uid);
33
 * @method void setLoginName(string $loginname)
34
 * @method void setName(string $name)
35
 * @method string getToken()
36
 * @method void setType(int $type)
37
 * @method int getType()
38
 * @method void setRemember(int $remember)
39
 * @method void setLastActivity(int $lastactivity)
40
 * @method int getLastActivity()
41
 * @method string getPrivateKey()
42
 * @method void setPrivateKey(string $key)
43
 * @method string getPublicKey()
44
 * @method void setPublicKey(string $key)
45
 * @method void setVersion(int $version)
46
 * @method bool getPasswordInvalid()
47
 */
48
class PublicKeyToken extends Entity implements IToken {
49
50
	const VERSION = 2;
51
52
	/** @var string user UID */
53
	protected $uid;
54
55
	/** @var string login name used for generating the token */
56
	protected $loginName;
57
58
	/** @var string encrypted user password */
59
	protected $password;
60
61
	/** @var string token name (e.g. browser/OS) */
62
	protected $name;
63
64
	/** @var string */
65
	protected $token;
66
67
	/** @var int */
68
	protected $type;
69
70
	/** @var int */
71
	protected $remember;
72
73
	/** @var int */
74
	protected $lastActivity;
75
76
	/** @var int */
77
	protected $lastCheck;
78
79
	/** @var string */
80
	protected $scope;
81
82
	/** @var int */
83
	protected $expires;
84
85
	/** @var string */
86
	protected $privateKey;
87
88
	/** @var string */
89
	protected $publicKey;
90
91
	/** @var int */
92
	protected $version;
93
94
	/** @var bool */
95
	protected $passwordInvalid;
96
97
	public function __construct() {
98
		$this->addType('uid', 'string');
99
		$this->addType('loginName', 'string');
100
		$this->addType('password', 'string');
101
		$this->addType('name', 'string');
102
		$this->addType('token', 'string');
103
		$this->addType('type', 'int');
104
		$this->addType('remember', 'int');
105
		$this->addType('lastActivity', 'int');
106
		$this->addType('lastCheck', 'int');
107
		$this->addType('scope', 'string');
108
		$this->addType('expires', 'int');
109
		$this->addType('publicKey', 'string');
110
		$this->addType('privateKey', 'string');
111
		$this->addType('version', 'int');
112
		$this->addType('passwordInvalid', 'bool');
113
	}
114
115
	public function getId(): int {
116
		return $this->id;
117
	}
118
119
	public function getUID(): string {
120
		return $this->uid;
121
	}
122
123
	/**
124
	 * Get the login name used when generating the token
125
	 *
126
	 * @return string
127
	 */
128
	public function getLoginName(): string {
129
		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, OC\Authentication\Token\PublicKeyToken. 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...
130
	}
131
132
	/**
133
	 * Get the (encrypted) login password
134
	 *
135
	 * @return string|null
136
	 */
137
	public function getPassword() {
138
		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, OC\Authentication\Token\PublicKeyToken. 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...
139
	}
140
141 View Code Duplication
	public function jsonSerialize() {
142
		return [
143
			'id' => $this->id,
144
			'name' => $this->name,
145
			'lastActivity' => $this->lastActivity,
146
			'type' => $this->type,
147
			'scope' => $this->getScopeAsArray()
148
		];
149
	}
150
151
	/**
152
	 * Get the timestamp of the last password check
153
	 *
154
	 * @return int
155
	 */
156
	public function getLastCheck(): int {
157
		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, OC\Authentication\Token\PublicKeyToken, OC\Updater\ChangesResult. 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...
158
	}
159
160
	/**
161
	 * Get the timestamp of the last password check
162
	 *
163
	 * @param int $time
164
	 */
165
	public function setLastCheck(int $time) {
166
		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, OC\Authentication\Token\PublicKeyToken, OC\Updater\ChangesResult. 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...
167
	}
168
169
	public function getScope(): string {
170
		$scope = parent::getScope();
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 getScope() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
171
		if ($scope === null) {
172
			return '';
173
		}
174
175
		return $scope;
176
	}
177
178 View Code Duplication
	public function getScopeAsArray(): array {
179
		$scope = json_decode($this->getScope(), true);
180
		if (!$scope) {
181
			return [
182
				'filesystem'=> true
183
			];
184
		}
185
		return $scope;
186
	}
187
188
	public function setScope($scope) {
189
		if (is_array($scope)) {
190
			parent::setScope(json_encode($scope));
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 setScope() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
191
		} else {
192
			parent::setScope((string)$scope);
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 setScope() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
193
		}
194
	}
195
196
	public function getName(): string {
197
		return parent::getName();
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 getName() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OCA\OAuth2\Db\Client, OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken, 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...
198
	}
199
200
	public function getRemember(): int {
201
		return parent::getRemember();
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 getRemember() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
202
	}
203
204
	public function setToken(string $token) {
205
		parent::setToken($token);
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 setToken() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OCA\DAV\Db\Direct, OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
206
	}
207
208
	public function setPassword(string $password = null) {
209
		parent::setPassword($password);
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 setPassword() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
210
	}
211
212
	public function setExpires($expires) {
213
		parent::setExpires($expires);
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 setExpires() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
214
	}
215
216
	/**
217
	 * @return int|null
218
	 */
219
	public function getExpires() {
220
		return parent::getExpires();
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 getExpires() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\DefaultToken, OC\Authentication\Token\PublicKeyToken. 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...
221
	}
222
223
	public function setPasswordInvalid(bool $invalid) {
224
		parent::setPasswordInvalid($invalid);
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 setPasswordInvalid() does only exist in the following sub-classes of OCP\AppFramework\Db\Entity: OC\Authentication\Token\PublicKeyToken. 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...
225
	}
226
}
227