1 | <?php |
||
14 | class Prefetcher |
||
15 | { |
||
16 | /** |
||
17 | * @param IO\IOInterface $io |
||
18 | * @param CopyRequest[] $requests |
||
19 | */ |
||
20 | 2 | public function fetchAll(IO\IOInterface $io, array $requests) |
|
21 | { |
||
22 | 2 | $successCnt = $failureCnt = 0; |
|
23 | 2 | $totalCnt = count($requests); |
|
24 | |||
25 | 2 | $multi = new CurlMulti; |
|
26 | 2 | $multi->setRequests($requests); |
|
27 | try { |
||
28 | do { |
||
29 | 2 | $multi->setupEventLoop(); |
|
30 | 2 | $multi->wait(); |
|
31 | |||
32 | 2 | $result = $multi->getFinishedResults(); |
|
33 | 2 | $successCnt += $result['successCnt']; |
|
34 | 2 | $failureCnt += $result['failureCnt']; |
|
35 | 2 | foreach ($result['urls'] as $url) { |
|
36 | 2 | if (isset($result['errors'][$url])) { |
|
37 | 1 | $io->writeError(" <warning>{$result['errors'][$url]}</warning>:\t$url", true, IO\IOInterface::NORMAL); |
|
38 | } else { |
||
39 | 1 | $io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, IO\IOInterface::NORMAL); |
|
40 | } |
||
41 | } |
||
42 | 2 | } while ($multi->remain()); |
|
43 | } catch (FetchException $e) { |
||
44 | // do nothing |
||
45 | } |
||
46 | |||
47 | 2 | $skippedCnt = $totalCnt - $successCnt - $failureCnt; |
|
48 | 2 | $io->writeError(" Finished: <comment>success: $successCnt, skipped: $skippedCnt, failure: $failureCnt, total: $totalCnt</comment>", true, IO\IOInterface::NORMAL); |
|
49 | 2 | } |
|
50 | |||
51 | /** |
||
52 | * @param IO\IOInterface $io |
||
53 | * @param Config $config |
||
54 | * @param Operation\OperationInterface[] $ops |
||
55 | */ |
||
56 | 4 | public function fetchAllFromOperations(IO\IOInterface $io, Config $config, array $ops) |
|
94 | |||
95 | 3 | private static function getUrlFromPackage(Package\PackageInterface $package) |
|
109 | } |
||
110 |
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: