1 | <?php |
||
9 | class AttributeManager |
||
10 | { |
||
11 | /** @var array **/ |
||
12 | private $attributes; |
||
13 | |||
14 | /** |
||
15 | * @param array $attributes |
||
16 | */ |
||
17 | 23 | public function __construct($attributes) |
|
18 | { |
||
19 | 23 | $this->attributes = $attributes; |
|
20 | 23 | } |
|
21 | |||
22 | /** |
||
23 | * @param string $attributeId |
||
24 | * @return \PhpAbac\Model\AbstractAttribute |
||
25 | */ |
||
26 | 6 | public function getAttribute($attributeId) { |
|
39 | |||
40 | /** |
||
41 | * @param array $attributeData |
||
42 | * @param string $property |
||
43 | * @return \PhpAbac\Model\Attribute |
||
44 | */ |
||
45 | 4 | private function getClassicAttribute($attributeData, $property) { |
|
54 | |||
55 | /** |
||
56 | * @param array $attributeData |
||
57 | * @param string $key |
||
58 | * @return \PhpAbac\Model\EnvironmentAttribute |
||
59 | */ |
||
60 | 4 | private function getEnvironmentAttribute($attributeData, $key) { |
|
69 | |||
70 | /** |
||
71 | * @param AbstractAttribute $attribute |
||
72 | * @param string $attributeType |
||
73 | * @param object $user |
||
74 | * @param object $object |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 4 | public function retrieveAttribute(AbstractAttribute $attribute, $user, $object = null) |
|
88 | |||
89 | /** |
||
90 | * @param Attribute $attribute |
||
91 | * @param object $object |
||
92 | * @return mixed |
||
93 | */ |
||
94 | 3 | private function retrieveClassicAttribute(Attribute $attribute, $object) |
|
109 | |||
110 | /** |
||
111 | * |
||
112 | * @param \PhpAbac\Model\EnvironmentAttribute $attribute |
||
113 | * @return mixed |
||
114 | */ |
||
115 | 2 | private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) { |
|
118 | |||
119 | /* |
||
120 | * @param string $name |
||
121 | * @return string |
||
122 | */ |
||
123 | 6 | public function slugify($name) |
|
138 | } |
||
139 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: