1 | <?php declare(strict_types=1); |
||
16 | class Service extends AbstractService implements IdentityService |
||
17 | 1 | { |
|
18 | public static function factory(ClientInterface $client): self |
||
19 | 1 | { |
|
20 | return new static($client, new Api()); |
||
21 | 1 | } |
|
22 | |||
23 | 1 | public function authenticate(array $options = []): array |
|
24 | { |
||
25 | 1 | $definition = $this->api->postToken(); |
|
26 | 1 | ||
27 | 1 | $response = $this->execute($definition, array_intersect_key($options, $definition['params'])); |
|
28 | 1 | ||
29 | 1 | $token = $this->model(Token::class, $response); |
|
30 | 1 | ||
31 | $serviceUrl = $this->model(Catalog::class, $response)->getServiceUrl( |
||
|
|||
32 | 1 | $options['catalogName'], |
|
33 | $options['catalogType'], |
||
34 | $options['region'], |
||
35 | $options['urlType'] |
||
36 | ); |
||
37 | |||
38 | return [$token, $serviceUrl]; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | 1 | * Generates a new authentication token |
|
43 | * |
||
44 | 1 | * @param array $options {@see \OpenStack\Identity\v2\Api::postToken} |
|
45 | 1 | * |
|
46 | * @return Models\Token |
||
47 | */ |
||
48 | public function generateToken(array $options = []): Token |
||
53 | } |
||
54 |
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 implementation 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 interface: