1 | <?php |
||
16 | class Route implements RouteInterface { |
||
17 | use HasMiddlewareTrait; |
||
18 | |||
19 | /** |
||
20 | * Allowed methods |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | protected $methods = []; |
||
25 | |||
26 | /** |
||
27 | * Route target |
||
28 | * |
||
29 | * @var ConditionInterface |
||
30 | */ |
||
31 | protected $target = null; |
||
32 | |||
33 | /** |
||
34 | * Route handler |
||
35 | * |
||
36 | * @var Handler|null |
||
37 | */ |
||
38 | protected $handler = null; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param string[] $methods |
||
44 | * @param mixed $target |
||
45 | * @param string|\Closure $handler |
||
46 | */ |
||
47 | public function __construct( $methods, $target, $handler ) { |
||
60 | |||
61 | /** |
||
62 | * {@inheritDoc} |
||
63 | */ |
||
64 | public function satisfied( Request $request ) { |
||
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | */ |
||
74 | public function handle( Request $request, $template ) { |
||
80 | |||
81 | /** |
||
82 | * Add a rewrite rule to WordPress for url-based routes |
||
83 | * |
||
84 | * @param string $rewrite_to |
||
85 | * @return RouteInterface |
||
86 | */ |
||
87 | public function rewrite( $rewrite_to ) { |
||
102 | } |
||
103 |
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: