1 | <?php |
||
18 | class Route implements RouteInterface { |
||
19 | use HasMiddlewareTrait; |
||
20 | |||
21 | /** |
||
22 | * Allowed methods |
||
23 | * |
||
24 | * @var string[] |
||
25 | */ |
||
26 | protected $methods = []; |
||
27 | |||
28 | /** |
||
29 | * Route target |
||
30 | * |
||
31 | * @var ConditionInterface |
||
32 | */ |
||
33 | protected $target = null; |
||
34 | |||
35 | /** |
||
36 | * Route handler |
||
37 | * |
||
38 | * @var Handler|null |
||
39 | */ |
||
40 | protected $handler = null; |
||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * |
||
45 | * @param string[] $methods |
||
46 | * @param mixed $target |
||
47 | * @param string|\Closure $handler |
||
48 | */ |
||
49 | public function __construct( $methods, $target, $handler ) { |
||
70 | |||
71 | /** |
||
72 | * Create and return a new condition |
||
73 | * |
||
74 | * @param array $options |
||
75 | * @return ConditionInterface |
||
76 | */ |
||
77 | protected function condition( $options ) { |
||
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | public function satisfied( Request $request ) { |
||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | public function handle( Request $request ) { |
||
114 | |||
115 | /** |
||
116 | * Add a rewrite rule to WordPress for url-based routes |
||
117 | * |
||
118 | * @param string $rewrite_to |
||
119 | * @return RouteInterface |
||
120 | */ |
||
121 | public function rewrite( $rewrite_to ) { |
||
136 | } |
||
137 |
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: