Completed
Push — master ( 24a68b...bc84ac )
by Thomas
22:37 queued 09:54
created

RemoteUser::getAccountId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, 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\User;
23
24
use OCP\IUser;
25
26
/**
27
 * Class RemoteUser - an implementation of IUser for user on a remote/federated server
28
 *
29
 * @package OC\User
30
 */
31
class RemoteUser implements IUser {
32
33
	/** @var string */
34
	private $userId;
35
36
	/**
37
	 * RemoteUser constructor.
38
	 *
39
	 * @param string $userId
40
	 */
41
	public function __construct($userId) {
42
		$this->userId = $userId;
43
	}
44
45
	/**
46
	 * @inheritdoc
47
	 */
48
	public function getUID() {
49
		return $this->userId;
50
	}
51
52
	/**
53
	 * @inheritdoc
54
	 */
55
	public function getDisplayName() {
56
		return $this->userId;
57
	}
58
59
	/**
60
	 * @inheritdoc
61
	 */
62
	public function setDisplayName($displayName) {
63
		return false;
64
	}
65
66
	/**
67
	 * @inheritdoc
68
	 */
69
	public function getLastLogin() {
70
		return 0;
71
	}
72
73
	/**
74
	 * @inheritdoc
75
	 */
76
	public function updateLastLoginTimestamp() {
77
	}
78
79
	/**
80
	 * @inheritdoc
81
	 */
82
	public function delete() {
83
		return false;
84
	}
85
86
	/**
87
	 * @inheritdoc
88
	 */
89
	public function setPassword($password, $recoveryPassword = null) {
90
		return false;
91
	}
92
93
	/**
94
	 * @inheritdoc
95
	 */
96
	public function getHome() {
97
	}
98
99
	/**
100
	 * @inheritdoc
101
	 */
102
	public function getBackendClassName() {
103
		return 'Remote';
104
	}
105
106
	/**
107
	 * @inheritdoc
108
	 */
109
	public function canChangeAvatar() {
110
		return false;
111
	}
112
113
	/**
114
	 * @inheritdoc
115
	 */
116
	public function canChangePassword() {
117
		return false;
118
	}
119
120
	/**
121
	 * @inheritdoc
122
	 */
123
	public function canChangeDisplayName() {
124
		return false;
125
	}
126
127
	/**
128
	 * @inheritdoc
129
	 */
130
	public function isEnabled() {
131
		return true;
132
	}
133
134
	/**
135
	 * @inheritdoc
136
	 */
137
	public function setEnabled($enabled) {
138
		return false;
139
	}
140
141
	/**
142
	 * @inheritdoc
143
	 */
144
	public function getEMailAddress() {
145
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface OCP\IUser::getEMailAddress of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
146
	}
147
148
	/**
149
	 * @inheritdoc
150
	 */
151
	public function getAvatarImage($size) {
152
		return null;
153
	}
154
155
	/**
156
	 * @inheritdoc
157
	 */
158
	public function getCloudId() {
159
		$uid = $this->getUID();
160
		$server = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
161
		return $uid . '@' . \rtrim($this->removeProtocolFromUrl($server), '/');
162
	}
163
164
	/**
165
	 * @param string $url
166
	 * @return string
167
	 */
168 View Code Duplication
	private function removeProtocolFromUrl($url) {
169
		if (\strpos($url, 'https://') === 0) {
170
			return \substr($url, \strlen('https://'));
171
		} elseif (\strpos($url, 'http://') === 0) {
172
			return \substr($url, \strlen('http://'));
173
		}
174
175
		return $url;
176
	}
177
178
	/**
179
	 * @inheritdoc
180
	 */
181
	public function setEMailAddress($mailAddress) {
182
	}
183
184
	/**
185
	 * @inheritdoc
186
	 */
187
	public function getQuota() {
188
		return 'none';
189
	}
190
191
	/**
192
	 * @inheritdoc
193
	 */
194
	public function setQuota($quota) {
195
	}
196
197
	/**
198
	 * @inheritdoc
199
	 */
200
	public function getSearchTerms() {
201
		return [];
202
	}
203
204
	/**
205
	 * @inheritdoc
206
	 */
207
	public function setSearchTerms(array $terms) {
208
	}
209
210
	/**
211
	 * @return integer
212
	 * @since 11.0.0
213
	 */
214
	public function getAccountId() {
215
		return null;
216
	}
217
}
218