1 | <?php |
||
15 | trait AssertRequestAgainstSchema |
||
16 | { |
||
17 | /** |
||
18 | * @var Schema |
||
19 | */ |
||
20 | protected $schema; |
||
21 | |||
22 | /** |
||
23 | * configure the schema to use for requests |
||
24 | * |
||
25 | * When set, all requests without an own schema use this one instead. |
||
26 | * |
||
27 | * @param Schema|null $schema |
||
28 | */ |
||
29 | public function setSchema($schema) |
||
33 | |||
34 | /** |
||
35 | * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc |
||
36 | * @param string $path The REST path call |
||
37 | * @param int $statusExpected |
||
38 | * @param array|null $query |
||
39 | * @param array|null $requestBody |
||
40 | * @param array $requestHeader |
||
41 | * @return mixed |
||
42 | * @throws DefinitionNotFoundException |
||
43 | * @throws GenericSwaggerException |
||
44 | * @throws HttpMethodNotFoundException |
||
45 | * @throws InvalidDefinitionException |
||
46 | * @throws NotMatchedException |
||
47 | * @throws PathNotFoundException |
||
48 | * @throws StatusCodeNotMatchedException |
||
49 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
50 | * @deprecated Use assertRequest instead |
||
51 | */ |
||
52 | protected function makeRequest( |
||
82 | |||
83 | /** |
||
84 | * @param AbstractRequester $request |
||
85 | * @return mixed |
||
86 | * @throws DefinitionNotFoundException |
||
87 | * @throws GenericSwaggerException |
||
88 | * @throws HttpMethodNotFoundException |
||
89 | * @throws InvalidDefinitionException |
||
90 | * @throws NotMatchedException |
||
91 | * @throws PathNotFoundException |
||
92 | * @throws StatusCodeNotMatchedException |
||
93 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
94 | */ |
||
95 | public function assertRequest(AbstractRequester $request) |
||
116 | |||
117 | /** |
||
118 | * @throws GenericSwaggerException |
||
119 | */ |
||
120 | protected function checkSchema() |
||
126 | } |
||
127 |
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: