Completed
Push — master ( ffb468...9f4772 )
by vistart
03:55
created

Username   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 42
rs 10
ccs 0
cts 19
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 18 4
A validate() 0 12 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\models\LoginMethod;
14
15
use rhosocial\user\User;
16
use Yii;
17
18
/**
19
 * Class Username
20
 * @package rhosocial\user\models\LoginMethod
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class Username implements MethodInterface
25
{
26
    /**
27
     * @param mixed $attribute
28
     * @return User|null
29
     */
30
    public static function getUser($attribute)
31
    {
32
        if (!static::validate($attribute)) {
33
            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 rhosocial\user\models\Lo...ethodInterface::getUser of type rhosocial\user\User|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...
34
        }
35
        $userClass = Yii::$app->user->identityClass;
36
        $noInit = $userClass::buildNoInitModel();
37
        /* @var $noInit User */
38
        if (class_exists($noInit->usernameClass)) {
39
            $class = $noInit->usernameClass;
40
            try {
41
                return $class::find()->content($attribute)->one()->host;
42
            } catch (\Exception $ex) {
43
                return null;
44
            }
45
        }
46
        return null;
47
    }
48
49
    /**
50
     * @param mixed $attribute
51
     * @return bool
52
     */
53
    public static function validate($attribute)
54
    {
55
        $userClass = Yii::$app->user->identityClass;
56
        $noInit = $userClass::buildNoInitModel();
57
        /* @var $noInit User */
58
        if (class_exists($noInit->usernameClass)) {
59
            $class = $noInit->usernameClass;
60
            $result = preg_match($class::$regex, $attribute);
61
            return is_int($result) && $result > 0;
62
        }
63
        return false;
64
    }
65
}
66