1 | <?php |
||
19 | class PuliBetaStrategy implements DiscoveryStrategy |
||
20 | { |
||
21 | /** |
||
22 | * @var GeneratedPuliFactory |
||
23 | */ |
||
24 | protected static $puliFactory; |
||
25 | |||
26 | /** |
||
27 | * @var Discovery |
||
28 | */ |
||
29 | protected static $puliDiscovery; |
||
30 | |||
31 | /** |
||
32 | * @return GeneratedPuliFactory |
||
33 | * |
||
34 | * @throws PuliUnavailableException |
||
35 | */ |
||
36 | 2 | private static function getPuliFactory() |
|
37 | { |
||
38 | 2 | if (null === self::$puliFactory) { |
|
39 | if (!defined('PULI_FACTORY_CLASS')) { |
||
40 | throw new PuliUnavailableException('Puli Factory is not available'); |
||
41 | } |
||
42 | |||
43 | $puliFactoryClass = PULI_FACTORY_CLASS; |
||
44 | |||
45 | if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { |
||
46 | throw new PuliUnavailableException('Puli Factory class does not exist'); |
||
47 | } |
||
48 | |||
49 | self::$puliFactory = new $puliFactoryClass(); |
||
50 | } |
||
51 | |||
52 | 2 | return self::$puliFactory; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Returns the Puli discovery layer. |
||
57 | * |
||
58 | * @return Discovery |
||
59 | * |
||
60 | * @throws PuliUnavailableException |
||
61 | */ |
||
62 | 2 | private static function getPuliDiscovery() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 2 | public static function getCandidates($type) |
|
92 | } |
||
93 |
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: