Conditions | 5 |
Paths | 3 |
Total Lines | 22 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
35 | public function upload(ImageInterface $image) |
||
36 | { |
||
37 | if (!$image->hasFile()) { |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | if (null !== $image->getPath() && $this->has($image->getPath())) { |
||
42 | $this->remove($image->getPath()); |
||
43 | } |
||
44 | |||
45 | do { |
||
46 | $hash = md5(uniqid(mt_rand(), true)); |
||
47 | $path = $this->expandPath($hash.'.'.$image->getFile()->guessExtension()); |
||
|
|||
48 | } while ($this->filesystem->has($path)); |
||
49 | |||
50 | $image->setPath($path); |
||
51 | |||
52 | $this->filesystem->write( |
||
53 | $image->getPath(), |
||
54 | file_get_contents($image->getFile()->getPathname()) |
||
55 | ); |
||
56 | } |
||
57 | |||
91 |
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: