1 | <?php |
||
16 | class ServicesManager { |
||
17 | |||
18 | /** |
||
19 | * @var ContainerBuilder |
||
20 | */ |
||
21 | private $containerBuilder; |
||
22 | |||
23 | /** |
||
24 | * @since 2.0 |
||
25 | * |
||
26 | * @param ContainerBuilder $containerBuilder |
||
27 | */ |
||
28 | 7 | public function __construct( ContainerBuilder $containerBuilder ) { |
|
31 | |||
32 | /** |
||
33 | * @since 2.0 |
||
34 | * |
||
35 | * @param string $serviceName |
||
36 | * @param mixed $service |
||
37 | * @param string|null $expectedReturnType |
||
38 | */ |
||
39 | 5 | public function add( $serviceName, $service, $expectedReturnType = null ) { |
|
53 | |||
54 | /** |
||
55 | * @since 2.0 |
||
56 | * |
||
57 | * @param string $serviceName |
||
58 | * |
||
59 | * @return boolean |
||
60 | */ |
||
61 | 5 | public function has( $serviceName ) { |
|
64 | |||
65 | /** |
||
66 | * @since 2.0 |
||
67 | * |
||
68 | * @param string $serviceName |
||
69 | * |
||
70 | * @return mixed |
||
71 | * @throws ServiceNotFoundException |
||
72 | */ |
||
73 | 5 | public function get( $serviceName ) { |
|
84 | |||
85 | /** |
||
86 | * @since 2.0 |
||
87 | * |
||
88 | * @param string $serviceName |
||
89 | */ |
||
90 | 1 | public function remove( $serviceName ) { |
|
93 | |||
94 | /** |
||
95 | * @since 2.0 |
||
96 | * |
||
97 | * @param string $serviceName |
||
98 | * @param mixed $service |
||
99 | */ |
||
100 | 2 | public function replace( $serviceName, $service ) { |
|
103 | |||
104 | } |
||
105 |
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: