1 | <?php |
||
8 | class Configuration |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | public static $env; |
||
14 | |||
15 | /** |
||
16 | * @var PathCollection |
||
17 | */ |
||
18 | protected $paths; |
||
19 | |||
20 | /** |
||
21 | * @var Dotenv |
||
22 | */ |
||
23 | protected $dotenv; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $environment; |
||
29 | |||
30 | /** |
||
31 | * @param array|Configuration $config |
||
32 | */ |
||
33 | public function __construct($config) |
||
59 | |||
60 | /** |
||
61 | * @return PathCollection |
||
62 | */ |
||
63 | public function getPaths() |
||
67 | |||
68 | /** |
||
69 | * @param PathCollection $pathCollection |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function setPaths(PathCollection $pathCollection) |
||
77 | |||
78 | /** |
||
79 | * @param null|string $environment |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function setEnvironment($environment) |
||
92 | |||
93 | /** |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function removeEnvironment() |
||
106 | |||
107 | /** |
||
108 | * @return null|string |
||
109 | */ |
||
110 | public function getEnvironment() |
||
114 | |||
115 | /** |
||
116 | * @return Dotenv |
||
117 | */ |
||
118 | public function getDotenv() |
||
122 | |||
123 | /** |
||
124 | * @param Dotenv $dotenv |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setDotenv(Dotenv $dotenv) |
||
133 | |||
134 | private function loadDotenv() |
||
144 | } |
||
145 |
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: