Completed
Push — master ( b9c485...94fed1 )
by Michał
13:51
created

src/Sylius/Component/Core/Model/AdminUser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Sylius\Component\Rbac\Model\RoleInterface;
16
use Sylius\Component\User\Model\User;
17
18
/**
19
 * @author Arkadiusz Krakowiak <[email protected]>
20
 */
21
class AdminUser extends User implements AdminUserInterface
22
{
23
    /**
24
     * @var ArrayCollection
25
     */
26
    protected $authorizationRoles;
27
28
    /**
29
     * @var string
30
     */
31
    protected $firstName;
32
33
    /**
34
     * @var string
35
     */
36
    protected $lastName;
37
38
    /**
39
     * @var array
40
     */
41
    protected $roles = [AdminUserInterface::DEFAULT_ADMIN_ROLE];
42
43
    public function __construct()
44
    {
45
        parent::__construct();
46
47
        $this->authorizationRoles = new ArrayCollection();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getAuthorizationRoles()
54
    {
55
        return $this->authorizationRoles;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->authorizationRoles; (Doctrine\Common\Collections\ArrayCollection) is incompatible with the return type declared by the interface Sylius\Component\Rbac\Mo...::getAuthorizationRoles of type Sylius\Component\Rbac\Model\RoleInterface[].

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...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function addAuthorizationRole(RoleInterface $role)
62
    {
63
        if (!$this->hasAuthorizationRole($role)) {
64
            $this->authorizationRoles->add($role);
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function removeAuthorizationRole(RoleInterface $role)
72
    {
73
        if ($this->hasAuthorizationRole($role)) {
74
            $this->authorizationRoles->removeElement($role);
75
        }
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function hasAuthorizationRole(RoleInterface $role)
82
    {
83
        return $this->authorizationRoles->contains($role);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getRoles()
90
    {
91
        $roles = parent::getRoles();
92
93
        foreach ($this->getAuthorizationRoles() as $role) {
94
            $roles = array_merge($roles, $role->getSecurityRoles());
95
        }
96
97
        return $roles;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getFirstName()
104
    {
105
        return $this->firstName;
106
    }
107
108
    /**
109
     * @param string $firstName
110
     */
111
    public function setFirstName($firstName)
112
    {
113
        $this->firstName = $firstName;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getLastName()
120
    {
121
        return $this->lastName;
122
    }
123
124
    /**
125
     * @param string $lastName
126
     */
127
    public function setLastName($lastName)
128
    {
129
        $this->lastName = $lastName;
130
    }
131
}
132