1 | <?php |
||
10 | abstract class ServerResourceCommand extends ServerCommand |
||
11 | { |
||
12 | /** |
||
13 | * Item ID. |
||
14 | */ |
||
15 | protected $itemId; |
||
16 | |||
17 | /** |
||
18 | * Server resource path. |
||
19 | * |
||
20 | * @return string |
||
21 | */ |
||
22 | abstract public function resourcePath(); |
||
23 | |||
24 | /** |
||
25 | * Server resource class name. |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | abstract public function resourceClass(); |
||
30 | |||
31 | /** |
||
32 | * Determines if current command is list command. |
||
33 | * |
||
34 | * @return bool |
||
35 | */ |
||
36 | protected function isListCommand(): bool |
||
40 | |||
41 | /** |
||
42 | * Command name. |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function command() |
||
50 | |||
51 | /** |
||
52 | * HTTP request method. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function requestMethod() |
||
64 | |||
65 | /** |
||
66 | * HTTP request URL. |
||
67 | * |
||
68 | * @param \Laravel\Forge\Server |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function requestUrl(Server $server) |
||
82 | |||
83 | /** |
||
84 | * Set Item ID. |
||
85 | * |
||
86 | * @param int|string $itemId |
||
87 | * |
||
88 | * @return static |
||
89 | */ |
||
90 | public function setItemId($itemId) |
||
96 | |||
97 | /** |
||
98 | * Get Item ID. |
||
99 | * |
||
100 | * @return int|string|null |
||
101 | */ |
||
102 | public function getItemId() |
||
106 | |||
107 | /** |
||
108 | * Processes new response item. |
||
109 | * |
||
110 | * @param mixed $item |
||
111 | * |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function processResponseItem($item) |
||
118 | |||
119 | /** |
||
120 | * Handle command response. |
||
121 | * |
||
122 | * @param \Psr\Http\Message\ResponseInterface $response |
||
123 | * @param \Laravel\Forge\Server $server |
||
124 | * |
||
125 | * @return \Laravel\Forge\ServerResources\ServerResource|array|bool|string |
||
126 | */ |
||
127 | public function handleResponse(ResponseInterface $response, Server $server) |
||
139 | |||
140 | /** |
||
141 | * List response handler. |
||
142 | * |
||
143 | * @param \Psr\Http\Message\ResponseInterface $response |
||
144 | * @param \Laravel\Forge\Server $server |
||
145 | * |
||
146 | * @throws \InvalidArgumentException |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function handleListCommandResponse(ResponseInterface $response, Server $server) |
||
169 | } |
||
170 |
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 sub-classes 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 parent class: