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
|
|||
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 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.