Conditions | 5 |
Paths | 7 |
Total Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
15 | public function validate(ResponseInterface $response) |
||
16 | { |
||
17 | if ($response->getContentType() !== 'text/html') { |
||
|
|||
18 | return; |
||
19 | } |
||
20 | |||
21 | $crawler = new Crawler((string)$response->getBody()); |
||
22 | |||
23 | $idList = $crawler->filterXPath('//*[@id!=""]'); |
||
24 | |||
25 | $foundIds = array(); |
||
26 | $duplicatedIds = array(); |
||
27 | |||
28 | foreach ($idList as $idElement) { |
||
29 | $id = $idElement->getAttribute('id'); |
||
30 | if (array_key_exists($id, $foundIds)) { |
||
31 | $duplicatedIds[$id] = true; |
||
32 | } else { |
||
33 | $foundIds[$id] = true; |
||
34 | } |
||
35 | } |
||
36 | |||
37 | if (count($duplicatedIds) > 0) { |
||
38 | unset($duplicatedIds['']); |
||
39 | throw new ValidationFailedException('Duplicate ids found (' . implode(', ', array_keys($duplicatedIds)) . ')'); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 |
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: