Completed
Pull Request — master (#78)
by
unknown
02:02
created

BaseUserDataTransferObject::getRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\DataTransferObject;
4
5
use Doctrine\Common\Collections\Collection;
6
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
7
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
8
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser;
9
10
class BaseUserDataTransferObject implements UserDataTransferObject
11
{
12
    /** @var int|null */
13
    public $id;
14
15
    /** @var string|null */
16
    public $userName;
17
18
    /** @var string|null */
19
    public $displayName;
20
21
    /** @var string|null */
22
    public $email;
23
24
    /** @var string|null */
25
    public $plainPassword;
26
27
    /** @var User */
28
    protected $user;
29
30
    /** @var array */
31
    public $roles;
32
33
    public static function fromUser(User $user): UserDataTransferObject
34
    {
35
        $baseUserTransferObject = new static();
36
        $baseUserTransferObject->user = $user;
37
        $baseUserTransferObject->id = $user->getId();
38
        $baseUserTransferObject->userName = $user->getUsername();
39
        $baseUserTransferObject->displayName = $user->getDisplayName();
40
        $baseUserTransferObject->email = $user->getEmail();
41
        $baseUserTransferObject->roles = $user->getRoles();
42
        if ($user->hasPlainPassword()) {
43
            $baseUserTransferObject->plainPassword = $user->getPlainPassword();
44
        }
45
46
        return $baseUserTransferObject;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $baseUserTransferObject; (SumoCoders\FrameworkMult...eUserDataTransferObject) is incompatible with the return type declared by the interface SumoCoders\FrameworkMult...ransferObject::fromUser of type self.

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...
47
    }
48
49
    public function getEntity(): User
50
    {
51
        if ($this->user) {
52
            $this->user->change($this);
53
54
            return $this->user;
55
        }
56
57
        return new BaseUser(
0 ignored issues
show
Bug introduced by
The call to BaseUser::__construct() misses a required argument $roles.

This check looks for function calls that miss required arguments.

Loading history...
58
            $this->userName,
59
            $this->plainPassword,
60
            $this->displayName,
61
            $this->email,
62
            $this->roles
0 ignored issues
show
Documentation introduced by
$this->roles is of type array, but the function expects a null|integer.

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...
63
        );
64
    }
65
66
    public function getId(): ?int
67
    {
68
        return $this->id;
69
    }
70
71
    public function getUserName(): ?string
72
    {
73
        return $this->userName;
74
    }
75
76
    public function getDisplayName(): ?string
77
    {
78
        return $this->displayName;
79
    }
80
81
    public function getEmail(): ?string
82
    {
83
        return $this->email;
84
    }
85
86
    public function getPlainPassword(): ?string
87
    {
88
        return $this->plainPassword;
89
    }
90
91
    public function getRoles(): array
92
    {
93
        return $this->roles;
94
    }
95
}
96