1 | <?php |
||
13 | abstract class ClassDiscovery |
||
14 | { |
||
15 | /** |
||
16 | * @var GeneratedPuliFactory |
||
17 | */ |
||
18 | private static $puliFactory; |
||
19 | |||
20 | /** |
||
21 | * @var Discovery |
||
22 | */ |
||
23 | private static $puliDiscovery; |
||
24 | |||
25 | /** |
||
26 | * @return GeneratedPuliFactory |
||
27 | */ |
||
28 | 10 | public static function getPuliFactory() |
|
29 | { |
||
30 | 10 | if (null === self::$puliFactory) { |
|
31 | if (!defined('PULI_FACTORY_CLASS')) { |
||
32 | throw new \RuntimeException('Puli Factory is not available'); |
||
33 | } |
||
34 | |||
35 | $puliFactoryClass = PULI_FACTORY_CLASS; |
||
36 | |||
37 | if (!class_exists($puliFactoryClass)) { |
||
38 | throw new \RuntimeException('Puli Factory class does not exist'); |
||
39 | } |
||
40 | |||
41 | self::$puliFactory = new $puliFactoryClass(); |
||
42 | } |
||
43 | |||
44 | 10 | return self::$puliFactory; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Sets the Puli factory. |
||
49 | * |
||
50 | * @param object $puliFactory |
||
51 | */ |
||
52 | 21 | public static function setPuliFactory($puliFactory) |
|
53 | { |
||
54 | 21 | if (!is_callable([$puliFactory, 'createRepository']) || !is_callable([$puliFactory, 'createDiscovery'])) { |
|
55 | throw new \InvalidArgumentException('The Puli Factory must expose a repository and a discovery'); |
||
56 | } |
||
57 | |||
58 | 21 | self::$puliFactory = $puliFactory; |
|
59 | 21 | self::$puliDiscovery = null; |
|
60 | 21 | } |
|
61 | |||
62 | /** |
||
63 | * Resets the factory. |
||
64 | */ |
||
65 | 21 | public static function resetPuliFactory() |
|
70 | |||
71 | /** |
||
72 | * Returns the Puli discovery layer. |
||
73 | * |
||
74 | * @return Discovery |
||
75 | */ |
||
76 | 9 | public static function getPuliDiscovery() |
|
87 | |||
88 | /** |
||
89 | * Finds a class. |
||
90 | * |
||
91 | * @param $type |
||
92 | * |
||
93 | * @return string |
||
94 | * |
||
95 | * @throws NotFoundException |
||
96 | */ |
||
97 | 8 | public static function findOneByType($type) |
|
116 | |||
117 | /** |
||
118 | * Finds a resource. |
||
119 | * |
||
120 | * @return object |
||
121 | */ |
||
122 | public static function find() |
||
126 | |||
127 | /** |
||
128 | * Evaulates conditions to boolean. |
||
129 | * |
||
130 | * @param mixed $condition |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 1 | protected static function evaluateCondition($condition) |
|
156 | } |
||
157 |
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: