Completed
Push — master ( a82218...06e071 )
by Thomas
11:03
created

DIContainer   C

Complexity

Total Complexity 15

Size/Duplication

Total Lines 450
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 19

Importance

Changes 0
Metric Value
dl 0
loc 450
rs 6.875
c 0
b 0
f 0
wmc 15
lcom 3
cbo 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 344 2
A getCoreApi() 0 4 1
A getServer() 0 4 1
A registerMiddleWare() 0 3 1
A getAppName() 0 3 1
A isLoggedIn() 0 3 1
A isAdminUser() 0 4 1
A getUserId() 0 3 1
B log() 0 20 5
A registerCapability() 0 6 1
1
<?php
2
/**
3
 * @author Bernhard Posselt <[email protected]>
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin McCorkell <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Thomas Tanghus <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2017, ownCloud GmbH
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
32
namespace OC\AppFramework\DependencyInjection;
33
34
use OC;
35
use OC\AppFramework\Core\API;
36
use OC\AppFramework\Http;
37
use OC\AppFramework\Http\Dispatcher;
38
use OC\AppFramework\Http\Output;
39
use OC\AppFramework\Middleware\MiddlewareDispatcher;
40
use OC\AppFramework\Middleware\Security\CORSMiddleware;
41
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
42
use OC\AppFramework\Middleware\SessionMiddleware;
43
use OC\AppFramework\Utility\SimpleContainer;
44
use OC\Core\Middleware\TwoFactorMiddleware;
45
use OCP\AppFramework\IApi;
46
use OCP\AppFramework\IAppContainer;
47
use OCP\Files\Mount\IMountManager;
48
use OCP\IDateTimeFormatter;
49
50
51
class DIContainer extends SimpleContainer implements IAppContainer {
52
53
	/**
54
	 * @var array
55
	 */
56
	private $middleWares = [];
57
58
	/**
59
	 * Put your class dependencies in here
60
	 * @param string $appName the name of the app
61
	 */
62
	public function __construct($appName, $urlParams = []){
63
		parent::__construct();
64
		$this['AppName'] = $appName;
65
		$this['urlParams'] = $urlParams;
66
67
		/** @var \OC\ServerContainer $server */
68
		$server = $this->getServer();
69
		$server->registerAppContainer($appName, $this);
70
71
		// aliases
72
		$this->registerAlias('appName', 'AppName');
73
		$this->registerAlias('webRoot', 'WebRoot');
74
		$this->registerAlias('userId', 'UserId');
75
76
		/**
77
		 * Core services
78
		 */
79
		$this->registerService('OCP\\IAppConfig', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
			return $this->getServer()->getAppConfig();
81
		});
82
83
		$this->registerService('OCP\\App\\IAppManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
			return $this->getServer()->getAppManager();
85
		});
86
87
		$this->registerService('OCP\\AppFramework\\Http\\IOutput', function($c){
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
			return new Output($this->getServer()->getWebRoot());
89
		});
90
91
		$this->registerService('OCP\\IAvatarManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
			return $this->getServer()->getAvatarManager();
93
		});
94
95
		$this->registerService('OCP\\Activity\\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
			return $this->getServer()->getActivityManager();
97
		});
98
99
		$this->registerService('OCP\\ICache', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
			return $this->getServer()->getCache();
101
		});
102
103
		$this->registerService('OCP\\ICacheFactory', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
			return $this->getServer()->getMemCacheFactory();
105
		});
106
107
		$this->registerService('OC\\CapabilitiesManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
			return $this->getServer()->getCapabilitiesManager();
109
		});
110
111
		$this->registerService('OCP\Comments\ICommentsManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
			return $this->getServer()->getCommentsManager();
113
		});
114
115
		$this->registerService('OCP\\IConfig', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
			return $this->getServer()->getConfig();
117
		});
118
119
		$this->registerService('OCP\\Contacts\\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
			return $this->getServer()->getContactsManager();
121
		});
122
123
		$this->registerService('OCP\\IDateTimeZone', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
			return $this->getServer()->getDateTimeZone();
125
		});
126
127
		$this->registerService('OCP\\IDb', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
			return $this->getServer()->getDb();
129
		});
130
131
		$this->registerService('OCP\\IDBConnection', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
			return $this->getServer()->getDatabaseConnection();
133
		});
134
135
		$this->registerService('OCP\\Diagnostics\\IEventLogger', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
			return $this->getServer()->getEventLogger();
137
		});
138
139
		$this->registerService('OCP\\Diagnostics\\IQueryLogger', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
			return $this->getServer()->getQueryLogger();
141
		});
142
143
		$this->registerService('OCP\\Files\\IMimeTypeDetector', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
			return $this->getServer()->getMimeTypeDetector();
145
		});
146
147
		$this->registerService('OCP\\Files\\Config\\IMountProviderCollection', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
			return $this->getServer()->getMountProviderCollection();
149
		});
150
151
		$this->registerService('OCP\\Files\\IRootFolder', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
			return $this->getServer()->getRootFolder();
153
		});
154
155
		$this->registerService('OCP\\Http\\Client\\IClientService', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
			return $this->getServer()->getHTTPClientService();
157
		});
158
159
		$this->registerService('OCP\\IGroupManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
			return $this->getServer()->getGroupManager();
161
		});
162
163
		$this->registerService('OCP\\Http\\Client\\IClientService', function() {
164
			return $this->getServer()->getHTTPClientService();
165
		});
166
167
		$this->registerService('OCP\\IL10N', function($c) {
168
			return $this->getServer()->getL10N($c->query('AppName'));
169
		});
170
171
		$this->registerService('OCP\\L10N\\IFactory', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
			return $this->getServer()->getL10NFactory();
173
		});
174
175
		$this->registerService('OCP\\ILogger', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
			return $this->getServer()->getLogger();
177
		});
178
179
		$this->registerService('OCP\\BackgroundJob\\IJobList', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
			return $this->getServer()->getJobList();
181
		});
182
183
		$this->registerAlias('OCP\\AppFramework\\Utility\\IControllerMethodReflector', 'OC\AppFramework\Utility\ControllerMethodReflector');
184
		$this->registerAlias('ControllerMethodReflector', 'OCP\\AppFramework\\Utility\\IControllerMethodReflector');
185
186
		$this->registerService('OCP\\Files\\IMimeTypeDetector', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
187
			return $this->getServer()->getMimeTypeDetector();
188
		});
189
190
		$this->registerService('OCP\\Mail\\IMailer', function() {
191
			return $this->getServer()->getMailer();
192
		});
193
194
		$this->registerService('OCP\\INavigationManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
			return $this->getServer()->getNavigationManager();
196
		});
197
198
		$this->registerService('OCP\\Notification\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
199
			return $this->getServer()->getNotificationManager();
200
		});
201
202
		$this->registerService('OCP\\IPreview', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
			return $this->getServer()->getPreviewManager();
204
		});
205
206
		$this->registerService('OCP\\IRequest', function () {
207
			return $this->getServer()->getRequest();
208
		});
209
		$this->registerAlias('Request', 'OCP\\IRequest');
210
211
		$this->registerService('OCP\\ITagManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
212
			return $this->getServer()->getTagManager();
213
		});
214
215
		$this->registerService('OCP\\ITempManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
216
			return $this->getServer()->getTempManager();
217
		});
218
219
		$this->registerAlias('OCP\\AppFramework\\Utility\\ITimeFactory', 'OC\AppFramework\Utility\TimeFactory');
220
		$this->registerAlias('TimeFactory', 'OCP\\AppFramework\\Utility\\ITimeFactory');
221
222
223
		$this->registerService('OCP\\Route\\IRouter', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
			return $this->getServer()->getRouter();
225
		});
226
227
		$this->registerService('OCP\\ISearch', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
228
			return $this->getServer()->getSearch();
229
		});
230
231
		$this->registerService('OCP\\ISearch', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
232
			return $this->getServer()->getSearch();
233
		});
234
235
		$this->registerService('OCP\\Security\\ICrypto', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
236
			return $this->getServer()->getCrypto();
237
		});
238
239
		$this->registerService('OCP\\Security\\IHasher', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
240
			return $this->getServer()->getHasher();
241
		});
242
243
		$this->registerService('OCP\\Security\\ICredentialsManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
244
			return $this->getServer()->getCredentialsManager();
245
		});
246
247
		$this->registerService('OCP\\Security\\ISecureRandom', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
248
			return $this->getServer()->getSecureRandom();
249
		});
250
251
		$this->registerService('OCP\\Share\\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
			return $this->getServer()->getShareManager();
253
		});
254
255
		$this->registerService('OCP\\SystemTag\\ISystemTagManager', function() {
256
			return $this->getServer()->getSystemTagManager();
257
		});
258
259
		$this->registerService('OCP\\SystemTag\\ISystemTagObjectMapper', function() {
260
			return $this->getServer()->getSystemTagObjectMapper();
261
		});
262
263
		$this->registerService('OCP\\IURLGenerator', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
264
			return $this->getServer()->getURLGenerator();
265
		});
266
267
		$this->registerService('OCP\\IUserManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
268
			return $this->getServer()->getUserManager();
269
		});
270
271
		$this->registerService('OCP\\IUserSession', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
272
			return $this->getServer()->getUserSession();
273
		});
274
275
		$this->registerService('OCP\\ISession', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
276
			return $this->getServer()->getSession();
277
		});
278
279
		$this->registerService('OCP\\Security\\IContentSecurityPolicyManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
280
			return $this->getServer()->getContentSecurityPolicyManager();
281
		});
282
283
		$this->registerService('ServerContainer', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
			return $this->getServer();
285
		});
286
		$this->registerAlias('OCP\\IServerContainer', 'ServerContainer');
287
288
		$this->registerService('Symfony\Component\EventDispatcher\EventDispatcherInterface', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
289
			return $this->getServer()->getEventDispatcher();
290
		});
291
292
		$this->registerService('OCP\\AppFramework\\IAppContainer', function ($c) {
293
			return $c;
294
		});
295
296
		$this->registerService(IDateTimeFormatter::class, function () {
297
			return $this->getServer()->getDateTimeFormatter();
298
		});
299
		$this->registerService(IMountManager::class, function () {
300
			return $this->getServer()->getMountManager();
301
		});
302
303
		// commonly used attributes
304
		$this->registerService('UserId', function ($c) {
305
			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
306
		});
307
308
		$this->registerService('WebRoot', function ($c) {
309
			return $c->query('ServerContainer')->getWebRoot();
310
		});
311
312
313
		/**
314
		 * App Framework APIs
315
		 */
316
		$this->registerService('API', function($c){
317
			$c->query('OCP\\ILogger')->debug(
318
				'Accessing the API class is deprecated! Use the appropriate ' .
319
				'services instead!'
320
			);
321
			return new API($c['AppName']);
0 ignored issues
show
Deprecated Code introduced by
The class OC\AppFramework\Core\API has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
322
		});
323
324
		$this->registerService('Protocol', function($c){
325
			/** @var \OC\Server $server */
326
			$server = $c->query('ServerContainer');
327
			$protocol = $server->getRequest()->getHttpProtocol();
328
			return new Http($_SERVER, $protocol);
329
		});
330
331
		$this->registerService('Dispatcher', function($c) {
332
			return new Dispatcher(
333
				$c['Protocol'],
334
				$c['MiddlewareDispatcher'],
335
				$c['ControllerMethodReflector'],
336
				$c['Request']
337
			);
338
		});
339
340
		$this->registerService('OCP\Theme\IThemeService', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
341
			return $this->getServer()->getThemeService();
342
		});
343
344
345
		/**
346
		 * Middleware
347
		 */
348
		$app = $this;
349
		$this->registerService('SecurityMiddleware', function($c) use ($app){
350
			return new SecurityMiddleware(
351
				$c['Request'],
352
				$c['ControllerMethodReflector'],
353
				$app->getServer()->getNavigationManager(),
354
				$app->getServer()->getURLGenerator(),
355
				$app->getServer()->getLogger(),
356
				$c['AppName'],
357
				$app->isLoggedIn(),
358
				$app->isAdminUser(),
359
				$app->getServer()->getContentSecurityPolicyManager()
0 ignored issues
show
Compatibility introduced by
$app->getServer()->getCo...SecurityPolicyManager() of type object<OCP\Security\ICon...tSecurityPolicyManager> is not a sub-type of object<OC\Security\CSP\C...tSecurityPolicyManager>. It seems like you assume a concrete implementation of the interface OCP\Security\IContentSecurityPolicyManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
360
			);
361
		});
362
363
		$this->registerService('CORSMiddleware', function($c) {
364
			return new CORSMiddleware(
365
				$c['Request'],
366
				$c['ControllerMethodReflector'],
367
				$c['OCP\IUserSession'],
368
				$c['OCP\IConfig']
369
			);
370
		});
371
372
		$this->registerService('SessionMiddleware', function($c) use ($app) {
373
			return new SessionMiddleware(
374
				$c['Request'],
375
				$c['ControllerMethodReflector'],
376
				$app->getServer()->getSession()
377
			);
378
		});
379
380
		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
381
			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OC\AppFramework\Utility\SimpleContainer as the method getServer() does only exist in the following sub-classes of OC\AppFramework\Utility\SimpleContainer: OC\AppFramework\DependencyInjection\DIContainer. 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...
382
			$userSession = $app->getServer()->getUserSession();
383
			$session = $app->getServer()->getSession();
384
			$urlGenerator = $app->getServer()->getURLGenerator();
385
			$reflector = $c['ControllerMethodReflector'];
386
			$request = $app->getServer()->getRequest();
387
			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
0 ignored issues
show
Compatibility introduced by
$userSession of type object<OCP\IUserSession> is not a sub-type of object<OC\User\Session>. It seems like you assume a concrete implementation of the interface OCP\IUserSession to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
388
		});
389
390
		$middleWares = &$this->middleWares;
391
		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
392
			$dispatcher = new MiddlewareDispatcher();
393
			$dispatcher->registerMiddleware($c['CORSMiddleware']);
394
			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
395
			$dispatcher->registerMiddleWare($c['TwoFactorMiddleware']);
396
397
			foreach($middleWares as $middleWare) {
398
				$dispatcher->registerMiddleware($c[$middleWare]);
399
			}
400
401
			$dispatcher->registerMiddleware($c['SessionMiddleware']);
402
			return $dispatcher;
403
		});
404
405
	}
406
407
408
	/**
409
	 * @deprecated implements only deprecated methods
410
	 * @return IApi
411
	 */
412
	function getCoreApi()
413
	{
414
		return $this->query('API');
415
	}
416
417
	/**
418
	 * @return \OCP\IServerContainer
419
	 */
420
	function getServer()
421
	{
422
		return OC::$server;
423
	}
424
425
	/**
426
	 * @param string $middleWare
427
	 * @return boolean|null
428
	 */
429
	function registerMiddleWare($middleWare) {
430
		array_push($this->middleWares, $middleWare);
431
	}
432
433
	/**
434
	 * used to return the appname of the set application
435
	 * @return string the name of your application
436
	 */
437
	function getAppName() {
438
		return $this->query('AppName');
439
	}
440
441
	/**
442
	 * @deprecated use IUserSession->isLoggedIn()
443
	 * @return boolean
444
	 */
445
	function isLoggedIn() {
446
		return $this->getServer()->getUserSession()->isLoggedIn();
447
	}
448
449
	/**
450
	 * @deprecated use IGroupManager->isAdmin($userId)
451
	 * @return boolean
452
	 */
453
	function isAdminUser() {
454
		$uid = $this->getUserId();
455
		return \OC_User::isAdminUser($uid);
456
	}
457
458
	private function getUserId() {
459
		return $this->getServer()->getSession()->get('user_id');
460
	}
461
462
	/**
463
	 * @deprecated use the ILogger instead
464
	 * @param string $message
465
	 * @param string $level
466
	 * @return mixed
467
	 */
468
	function log($message, $level) {
469
		switch($level){
470
			case 'debug':
471
				$level = \OCP\Util::DEBUG;
472
				break;
473
			case 'info':
474
				$level = \OCP\Util::INFO;
475
				break;
476
			case 'warn':
477
				$level = \OCP\Util::WARN;
478
				break;
479
			case 'fatal':
480
				$level = \OCP\Util::FATAL;
481
				break;
482
			default:
483
				$level = \OCP\Util::ERROR;
484
				break;
485
		}
486
		\OCP\Util::writeLog($this->getAppName(), $message, $level);
487
	}
488
489
	/**
490
	 * Register a capability
491
	 *
492
	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
493
	 */
494
	public function registerCapability($serviceName) {
495
		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
496
			return $this->query($serviceName);
497
		});
498
499
	}
500
}
501