Completed
Push — master ( e1f3e1...8a67ce )
by Patrick
02:59
created

AuthProvider::mergeResult()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * AuthProvider class
4
 *
5
 * This file describes the AuthProvider Singleton
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2015, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * Allow other classes to be loaded as needed
16
 */
17
require_once('Autoload.php');
18
/**
19
 * Require the FlipsideSettings file
20
 */
21
if(isset($GLOBALS['FLIPSIDE_SETTINGS_LOC']))
22
{
23
    require_once($GLOBALS['FLIPSIDE_SETTINGS_LOC'].'/class.FlipsideSettings.php');
24
}
25
else
26
{
27
    require_once('/var/www/secure_settings/class.FlipsideSettings.php');
28
}
29
30
/**
31
 * A Singleton class to abstract access to the authentication providers.
32
 *
33
 * This class is the primary method to access user data, login, and other authenication information.
34
 */
35
class AuthProvider extends Singleton
36
{
37
    /** The authentication methods loaded by the provider */
38
    protected $methods;
39
40
    /**
41
     * Load the authentrication providers specified in the FlipsideSettings::$authProviders array
42
     */
43 View Code Duplication
    protected function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $this->methods = array();
46
        if(isset(FlipsideSettings::$authProviders))
47
        {
48
            $keys = array_keys(FlipsideSettings::$authProviders);
49
            $count = count($keys);
50
            for($i = 0; $i < $count; $i++)
51
            {
52
                $class = $keys[$i];
53
                array_push($this->methods, new $class(FlipsideSettings::$authProviders[$keys[$i]]));
54
            }
55
        }
56
    }
57
58
    /**
59
     * Get the Authenticator class instance by name
60
     *
61
     * @param string $methodName The class name of the Authenticator to get the instance for
62
     *
63
     * @return Auth\Authenticator|false The specified Authenticator class instance or false if it is not loaded
64
     */
65 View Code Duplication
    public function getAuthenticator($methodName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $count = count($this->methods);
68
        for($i = 0; $i < $count; $i++)
69
        {
70
            if(strcasecmp(get_class($this->methods[$i]), $methodName) === 0)
71
            {
72
                return $this->methods[$i];
73
            }
74
        }
75
        return false;
76
    }
77
78
    /**
79
     * Get the Auth\User class instance for the specified login
80
     *
81
     * Unlike the AuthProvider::login() function. This function will not impact the SESSION
82
     *
83
     * @param string $username The username of the User
84
     * @param string $password The password of the User
85
     *
86
     * @return Auth\User|false The User with the specified credentials or false if the credentials are not valid
87
     */
88
    public function getUserByLogin($username, $password)
89
    {
90
        $res = false;
91
        $count = count($this->methods);
92
        for($i = 0; $i < $count; $i++)
93
        {
94
            $res = $this->methods[$i]->login($username, $password);
95
            if($res !== false)
96
            {
97
                return $this->methods[$i]->getUser($res);
98
            }
99
        }
100
        return $res;
101
    }
102
103
    /**
104
     * Use the provided credetials to log the user on
105
     *
106
     * @param string $username The username of the User
107
     * @param string $password The password of the User
108
     *
109
     * @return true|false true if the login was successful, false otherwise
110
     */
111
    public function login($username, $password)
112
    {
113
        $res = false;
114
        $count = count($this->methods);
115
        for($i = 0; $i < $count; $i++)
116
        {
117
            $res = $this->methods[$i]->login($username, $password);
118
            if($res !== false)
119
            {
120
                FlipSession::setVar('AuthMethod', get_class($this->methods[$i]));
121
                FlipSession::setVar('AuthData', $res);
122
                break;
123
            }
124
        }
125
        return $res;
126
    }
127
128
    /**
129
     * Determine if the user is still logged on from the session data
130
     *
131
     * @param stdClass $data The AuthData from the session
132
     * @param string $methodName The AuthMethod from the session
133
     *
134
     * @return true|false true if user is logged on, false otherwise
135
     */
136
    public function isLoggedIn($data, $methodName)
137
    {
138
        $auth = $this->getAuthenticator($methodName);
139
        return $auth->isLoggedIn($data);
140
    }
141
142
    /**
143
     * Obtain the currently logged in user from the session data
144
     *
145
     * @param stdClass $data The AuthData from the session
146
     * @param string $methodName The AuthMethod from the session
147
     *
148
     * @return Auth\User|false The User instance if user is logged on, false otherwise
149
     */
150
    public function getUser($data, $methodName)
151
    {
152
        $auth = $this->getAuthenticator($methodName);
153
        return $auth->getUser($data);
154
    }
155
156
    /**
157
     * Merge or set the returnValue as appropriate
158
     *
159
     * @param false|Auth\Group|Auth\User $returnValue The value to merge to
160
     * @param Auth\Group|Auth\User $res The value to merge from
161
     *
162
     * @return Auth\Group|false The merged returnValue
163
     */
164
    private function mergeResult(&$returnValue, $res)
165
    {
166
        if($res === false)
167
        {
168
            return;
169
        }
170
        if($returnValue === false)
171
        {
172
            $returnValue = $res;
173
            return;
174
        }
175
        $returnValue->merge($res);
176
    }
177
178
    /**
179
     * Calls the indicated function on each Authenticator and merges the result
180
     *
181
     * @param string $functionName The function to call
182
     * @param array $args The arguments for the function
183
     * @param string $checkField A field to check if it is set a certain way before calling the function
184
     * @param mixed $checkValue The value that field should be set to to not call the function
185
     *
186
     * @return Auth\Group|Auth\User|false The merged returnValue
187
     */
188 View Code Duplication
    private function callOnEach($functionName, $args, $checkField = false, $checkValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        $ret = false;
191
        $count = count($this->methods);
192
        for($i = 0; $i < $count; $i++)
193
        {
194
            if($checkField)
0 ignored issues
show
Bug Best Practice introduced by
The expression $checkField of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
195
            {
196
                if($this->methods[$i]->{$checkField} === $checkValue)
197
                {
198
                    continue;
199
                }
200
            }
201
            $res = call_user_func_array(array($this->methods[$i], $functionName), $args);
202
            $this->mergeResult($ret, $res);
203
        }
204
        return $ret;
205
    }
206
207
    /**
208
     * Calls the indicated function on each Authenticator and add the result
209
     *
210
     * @param string $functionName The function to call
211
     * @param string $checkField A field to check if it is set a certain way before calling the function
212
     * @param mixed $checkValue The value that field should be set to to not call the function
213
     *
214
     * @return integer The added returnValue
215
     */
216 View Code Duplication
    private function addFromEach($functionName, $checkField = false, $checkValue = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
        $retCount = 0;
219
        $count = count($this->methods);
220
        for($i = 0; $i < $count; $i++)
221
        {
222
            if($checkField)
0 ignored issues
show
Bug Best Practice introduced by
The expression $checkField of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
223
            {
224
                if($this->methods[$i]->{$checkField} === $checkValue)
225
                {
226
                    continue;
227
                }
228
            }
229
            $res = call_user_func(array($this->methods[$i], $functionName));
230
            $retCount += $res;
231
        }
232
        return $retCount;
233
    }
234
235
    /**
236
     * Get an Auth\Group by its name
237
     *
238
     * @param string $name The name of the group
239
     * @param string $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
240
     *
241
     * @return Auth\Group|false The Group instance if a group with that name exists, false otherwise
242
     */
243
    public function getGroupByName($name, $methodName = false)
244
    {
245
        if($methodName === false)
246
        {
247
            return $this->callOnEach('getGroupByName', array($name));
248
        }
249
        $auth = $this->getAuthenticator($methodName);
250
        return $auth->getGroupByName($name);
251
    }
252
253
    /**
254
     * Get an array of Auth\User from a filtered set
255
     *
256
     * @param Data\Filter|false $filter The filter conditions or false to retreive all
257
     * @param array|false $methodName The user fields to obtain or false to obtain all
258
     * @param integer|false $top The number of users to obtain or false to obtain all
259
     * @param integer|false $skip The number of users to skip or false to skip none
260
     * @param array|false $orderby The field to sort by and the method to sort or false to not sort
261
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
262
     *
263
     * @return array|false An array of Auth\User objects or false if no users were found
264
     */
265 View Code Duplication
    public function getUsersByFilter($filter, $select=false, $top=false, $skip=false, $orderby=false, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
    {
267
        if($methodName === false)
268
        {
269
            return $this->callOnEach('getUsersByFilter', array($filter, $select, $top, $skip, $orderby), 'current');
270
        }
271
        $auth = $this->getAuthenticator($methodName);
0 ignored issues
show
Documentation introduced by
$methodName is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
272
        return $auth->getUsersByFilter($filter, $select, $top, $skip, $orderby);
273
    }
274
275
    /**
276
     * Get an array of Auth\PendingUser from a filtered set
277
     *
278
     * @param Data\Filter|false $filter The filter conditions or false to retreive all
279
     * @param array|false $methodName The user fields to obtain or false to obtain all
280
     * @param integer|false $top The number of users to obtain or false to obtain all
281
     * @param integer|false $skip The number of users to skip or false to skip none
282
     * @param array|false $orderby The field to sort by and the method to sort or false to not sort
283
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
284
     *
285
     * @return array|false An array of Auth\PendingUser objects or false if no pending users were found
286
     */
287 View Code Duplication
    public function getPendingUsersByFilter($filter, $select=false, $top=false, $skip=false, $orderby=false, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
    {
289
        if($methodName === false)
290
        {
291
            return $this->callOnEach('getPendingUsersByFilter', array($filter, $select, $top, $skip, $orderby), 'pending');
292
        }
293
        $auth = $this->getAuthenticator($methodName);
0 ignored issues
show
Documentation introduced by
$methodName is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
294
        return $auth->getPendingUsersByFilter($filter, $select, $top, $skip, $orderby);
295
    }
296
297
    /**
298
     * Get an array of Auth\Group from a filtered set
299
     *
300
     * @param Data\Filter|false $filter The filter conditions or false to retreive all
301
     * @param array|false $methodName The group fields to obtain or false to obtain all
302
     * @param integer|false $top The number of groups to obtain or false to obtain all
303
     * @param integer|false $skip The number of groups to skip or false to skip none
304
     * @param array|false $orderby The field to sort by and the method to sort or false to not sort
305
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
306
     *
307
     * @return array|false An array of Auth\Group objects or false if no pending users were found
308
     */
309 View Code Duplication
    public function getGroupsByFilter($filter, $select=false, $top=false, $skip=false, $orderby=false, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
310
    {
311
        if($methodName === false)
312
        {
313
            return $this->callOnEach('getGroupsByFilter', array($filter, $select, $top, $skip, $orderby), 'current');
314
        }
315
        $auth = $this->getAuthenticator($methodName);
0 ignored issues
show
Documentation introduced by
$methodName is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
316
        return $auth->getGroupsByFilter($filter, $select, $top, $skip, $orderby);
317
    }
318
319
    /**
320
     * Get the number of currently active users on the system
321
     *
322
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
323
     *
324
     * @return integer The number of currently active users on the system
325
     */
326
    public function getActiveUserCount($methodName = false)
327
    {
328
        if($methodName === false)
329
        {
330
            return $this->addFromEach('getActiveUserCount', 'current');
331
        }
332
        $auth = $this->getAuthenticator($methodName);
333
        return $auth->getActiveUserCount();
334
    }
335
336
    /**
337
     * Get the number of currently pending users on the system
338
     *
339
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
340
     *
341
     * @return integer The number of currently pending users on the system
342
     */
343
    public function getPendingUserCount($methodName = false)
344
    {
345
        if($methodName === false)
346
        {
347
            return $this->addFromEach('getPendingUserCount', 'pending');
348
        }
349
        $auth = $this->getAuthenticator($methodName);
350
        return $auth->getPendingUserCount();
351
    }
352
353
    /**
354
     * Get the number of current groups on the system
355
     *
356
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
357
     *
358
     * @return integer The number of current groups on the system
359
     */
360
    public function getGroupCount($methodName = false)
361
    {
362
        if($methodName === false)
363
        {
364
            return $this->addFromEach('getGroupCount', 'current');
365
        }
366
        $auth = $this->getAuthenticator($methodName);
367
        return $auth->getGroupCount();
368
    }
369
370
    /**
371
     * Get the login links for all supplementary Authenitcation mechanisms
372
     *
373
     * This will return an array of links to any supplementary authentication mechanims. For example, Goodle is 
374
     * a supplementary authentication mechanism.
375
     *
376
     * @return array An array of suppmentary authentication mechanism links
377
     */
378
    public function getSupplementaryLinks()
379
    {
380
        $ret = array();
381
        $count = count($this->methods);
382 View Code Duplication
        for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
        {
384
            if($this->methods[$i]->supplement === false) continue;
385
386
            array_push($ret, $this->methods[$i]->getSupplementLink());
387
        }
388
        return $ret;
389
    }
390
391
    /**
392
     * Impersonate the user specified
393
     *
394
     * This will replace the user in the session with the specified user. In order
395
     * to undo this operation a user must logout.
396
     *
397
     * @param array|Auth\User $userArray Data representing the user
398
     */
399
    public function impersonateUser($userArray)
400
    {
401
        if(!is_object($userArray))
402
        {
403
            $user = new $userArray['class']($userArray);
404
        }
405
        \FlipSession::setUser($user);
0 ignored issues
show
Bug introduced by
The variable $user does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
406
    }
407
408
    /**
409
     * Get the pending user reresented by the supplied hash
410
     *
411
     * @param string $hash The hash value representing the Penging User
412
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
413
     *
414
     * @return Auth\PendingUser|false The Auth\PendingUser instance or false if no user is matched by the provided hash
415
     */
416
    public function getTempUserByHash($hash, $methodName = false)
417
    {
418
        if($methodName === false)
419
        {
420
            $count = count($this->methods);
421
            for($i = 0; $i < $count; $i++)
422
            {
423
                if($this->methods[$i]->pending === false) continue;
424
425
                $ret = $this->methods[$i]->getTempUserByHash($hash);
426
                if($ret !== false)
427
                {
428
                    return $ret;
429
                }
430
            }
431
            return false;
432
        }
433
        $auth = $this->getAuthenticator($methodName);
434
        return $auth->getTempUserByHash($hash);
435
    }
436
437
    /**
438
     * Create a pending user
439
     *
440
     * @param array $user An array of information about the user to create
441
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
442
     *
443
     * @return true|false true if the user was successfully created. Otherwise false.
444
     */
445
    public function createPendingUser($user, $methodName = false)
446
    {
447
        if($methodName === false)
448
        {
449
            $count = count($this->methods);
450
            for($i = 0; $i < $count; $i++)
451
            {
452
                if($this->methods[$i]->pending === false) continue;
453
454
                $ret = $this->methods[$i]->createPendingUser($user);
455
                if($ret !== false)
456
                {
457
                    return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by AuthProvider::createPendingUser of type true|false.

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...
458
                }
459
            }
460
            return false;
461
        }
462
        $auth = $this->getAuthenticator($methodName);
463
        return $auth->createPendingUser($user);
0 ignored issues
show
Documentation introduced by
$user is of type array, but the function expects a object<Auth\Auth\PendingUser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
464
    }
465
466
    /**
467
     * Convert a Auth\PendingUser into an Auth\User
468
     *
469
     * This will allow a previously pending user the ability to log on in the future as an active user. It will also
470
     * have the side effect of logging the user on now.
471
     *
472
     * @param Auth\PendingUser $user The user to turn into a current user
473
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
474
     *
475
     * @return true|false true if the user was successfully created. Otherwise false.
476
     */
477 View Code Duplication
    public function activatePendingUser($user, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
478
    {
479
        if($methodName === false)
480
        {
481
            $count = count($this->methods);
482
            for($i = 0; $i < $count; $i++)
483
            {
484
                if($this->methods[$i]->current === false) continue;
485
486
                $ret = $this->methods[$i]->activatePendingUser($user);
487
                if($ret !== false)
488
                {
489
                    $this->impersonateUser($ret);
490
                    return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by AuthProvider::activatePendingUser of type true|false.

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...
491
                }
492
            }
493
            return false;
494
        }
495
        $auth = $this->getAuthenticator($methodName);
496
        return $auth->activatePendingUser($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Auth\PendingUser>, but the function expects a object<Auth\Auth\PendingUser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
497
    }
498
499
    /**
500
     * Get a current user by a password reset hash
501
     *
502
     * @param string $hash The current password reset hash for the user
503
     * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator
504
     *
505
     * @return Auth\User|false The user if the password reset hash is valid. Otherwise false.
506
     */
507 View Code Duplication
    public function getUserByResetHash($hash, $methodName = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
508
    {
509
        if($methodName === false)
510
        {
511
            $count = count($this->methods);
512
            for($i = 0; $i < $count; $i++)
513
            {
514
                if($this->methods[$i]->current === false) continue;
515
516
                $ret = $this->methods[$i]->getUserByResetHash($hash);
517
                if($ret !== false)
518
                {
519
                    return $ret;
520
                }
521
            }
522
            return false;
523
        }
524
        $auth = $this->getAuthenticator($methodName);
525
        if($auth === false)
526
        {
527
            return $this->getUserByResetHash($hash, false);
528
        }
529
        return $auth->getUserByResetHash($hash);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $auth->getUserByResetHash($hash); (boolean) is incompatible with the return type documented by AuthProvider::getUserByResetHash of type Auth\User|false.

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...
530
    }
531
532
    /**
533
     * Get the Auth\Authenticator by host name
534
     *
535
     * @param string $host The host name used by the supplemental authentication mechanism
536
     *
537
     * @return Auth\Authenticator|false The Authenticator if the host is supported by a loaded Authenticator. Otherwise false.
538
     */
539
    public function getSuplementalProviderByHost($host)
540
    {
541
        $count = count($this->methods);
542 View Code Duplication
        for($i = 0; $i < $count; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
543
        {
544
            if($this->methods[$i]->supplement === false) continue;
545
546
            if($this->methods[$i]->getHostName() === $host)
547
            {
548
                return $this->methods[$i];
549
            }
550
        }
551
        return false;
552
    }
553
554
    public function deletePendingUsersByFilter($filter, $methodName=false)
555
    {
556
        $users = $this->getPendingUsersByFilter($filter, false, false, false, false, $methodName);
0 ignored issues
show
Documentation introduced by
$methodName is of type boolean, but the function expects a false|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
557
        if($users === false)
558
        {
559
            return false;
560
        }
561
        $count = count($users);
562
        for($i = 0; $i < $count; $i++)
563
        {
564
            $users[$i]->delete();
565
        }
566
        return true;
567
    }
568
569 View Code Duplication
    public function getUserByAccessCode($key, $methodName=false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
570
    {
571
        if($methodName === false)
572
        {
573
            $count = count($this->methods);
574
            for($i = 0; $i < $count; $i++)
575
            {
576
                if($this->methods[$i]->current === false) continue;
577
578
                $ret = $this->methods[$i]->getUserByAccessCode($key);
579
                if($ret !== false)
580
                {
581
                    return $ret;
582
                }
583
            }
584
            return false;
585
        }
586
        $auth = $this->getAuthenticator($methodName);
0 ignored issues
show
Documentation introduced by
$methodName is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
587
        return $auth->getUserByAccessCode($key);
0 ignored issues
show
Bug introduced by
The method getUserByAccessCode() does not exist on Auth\Authenticator. Did you maybe mean getUser()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
588
    }
589
}
590
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
591
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
592