1 | <?php |
||
25 | abstract class ReflectorFactory extends BaseFactory |
||
26 | { |
||
27 | const REFLECTION_OBJECT = null; |
||
28 | |||
29 | /** |
||
30 | * @var PHPNativeReflector |
||
31 | */ |
||
32 | protected $reflector; |
||
33 | |||
34 | /** |
||
35 | * @var RawPropertyAccessor |
||
36 | */ |
||
37 | protected $accessor; |
||
38 | |||
39 | /** |
||
40 | * @var SpaarkReflector |
||
41 | */ |
||
42 | protected $object; |
||
43 | |||
44 | /** |
||
45 | * Creates a new ReflectorFactory using the provided native PHP |
||
46 | * reflector as a basis |
||
47 | * |
||
48 | * @param PHPNativeReflector $reflector The reflector used to |
||
49 | * parse the item |
||
50 | */ |
||
51 | 21 | public function __construct(PHPNativeReflector $reflector) |
|
59 | |||
60 | /** |
||
61 | * Parses the docblock comment for this item and searches for |
||
62 | * annotations |
||
63 | */ |
||
64 | 21 | protected function parseDocComment(array $acceptedParams) |
|
65 | { |
||
66 | 21 | preg_match_all |
|
67 | ( |
||
68 | '/^' |
||
69 | . '[ \t]*\*[ \t]*' |
||
70 | . '@([a-zA-Z]+)' |
||
71 | . '(.*)' |
||
72 | 21 | . '$/m', |
|
73 | 21 | $this->reflector->getDocComment(), |
|
|
|||
74 | 21 | $matches |
|
75 | ); |
||
76 | |||
77 | 21 | foreach ($matches[0] as $key => $value) |
|
78 | { |
||
79 | 21 | $name = strtolower($matches[1][$key]); |
|
80 | 21 | $value = trim($matches[2][$key]); |
|
81 | |||
82 | 21 | if (isset($acceptedParams[$name])) |
|
83 | { |
||
84 | 21 | call_user_func |
|
85 | ( |
||
86 | 21 | array($this, $acceptedParams[$name]), |
|
87 | 21 | $name, $value |
|
88 | ); |
||
89 | } |
||
90 | } |
||
91 | 21 | } |
|
92 | |||
93 | /** |
||
94 | * Sets an annotation which has a boolean value |
||
95 | * |
||
96 | * @param string $name The name of the annotation |
||
97 | * @param string $value The value of the annotation |
||
98 | */ |
||
99 | 20 | protected function setBool($name, $value) |
|
118 | } |
||
119 | |||
120 |
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: