Completed
Push — master ( 92c926...5f9d64 )
by Łukasz
37s
created

ImageUploader::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Uploader;
13
14
use Gaufrette\Filesystem;
15
use Sylius\Component\Core\Model\ImageInterface;
16
17
class ImageUploader implements ImageUploaderInterface
18
{
19
    /**
20
     * @var Filesystem
21
     */
22
    protected $filesystem;
23
24
    /**
25
     * @param Filesystem $filesystem
26
     */
27
    public function __construct(Filesystem $filesystem)
28
    {
29
        $this->filesystem = $filesystem;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
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());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SplFileInfo as the method guessExtension() does only exist in the following sub-classes of SplFileInfo: Symfony\Component\HttpFoundation\File\File, Symfony\Component\HttpFoundation\File\UploadedFile, Symfony\Component\HttpFo...ion\Tests\File\FakeFile. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function remove($path)
62
    {
63
        return $this->filesystem->delete($path);
64
    }
65
66
    /**
67
     * @param string $path
68
     *
69
     * @return string
70
     */
71
    private function expandPath($path)
72
    {
73
        return sprintf(
74
            '%s/%s/%s',
75
            substr($path, 0, 2),
76
            substr($path, 2, 2),
77
            substr($path, 4)
78
        );
79
    }
80
81
    /**
82
     * @param string $path
83
     *
84
     * @return bool
85
     */
86
    private function has($path)
87
    {
88
        return $this->filesystem->has($path);
89
    }
90
}
91