GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( b704c5...ad0cea )
by Borut
02:41
created

AbstractImageUpload::imageUpload()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 17

Duplication

Lines 32
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 32
loc 32
rs 8.5806
cc 4
eloc 17
nc 3
nop 0
1
<?php
2
3
namespace Application\Entity;
4
5
use Symfony\Component\HttpFoundation\File\File;
6
7
/**
8
 * Abstract image upload
9
 *
10
 * Some default methods for the abstract image upload
11
 *
12
 * @author Borut Balažek <[email protected]>
13
 */
14
class AbstractImageUpload
15
{
16
    /**
17
     * @var File
18
     */
19
    protected $image;
20
21
    /**
22
     * @var string
23
     */
24
    protected $imageUploadPath;
25
26
    /**
27
     * @var string
28
     */
29
    protected $imageUploadDir;
30
31
    /*** Image ***/
32
    /**
33
     * @return File
34
     */
35
    public function getImage()
36
    {
37
        return $this->image;
38
    }
39
40
    /**
41
     * @param File $image
42
     *
43
     * @return AbstractImageUpload
44
     */
45
    public function setImage(File $image = null)
46
    {
47
        $this->image = $image;
48
49
        return $this;
50
    }
51
52
    /*** Image path ***/
53
    /**
54
     * @return string
55
     */
56
    public function getImageUploadPath()
57
    {
58
        return $this->imageUploadPath;
59
    }
60
61
    /**
62
     * @param string $imageUploadPath
63
     *
64
     * @return AbstractImageUpload
65
     */
66
    public function setImageUploadPath($imageUploadPath)
67
    {
68
        $this->imageUploadPath = $imageUploadPath;
69
70
        return $this;
71
    }
72
73
    /*** Image upload dir ***/
74
    /**
75
     * @return string
76
     */
77
    public function getImageUploadDir()
78
    {
79
        return $this->imageUploadDir;
80
    }
81
82
    /**
83
     * @param string $imageUploadDir
84
     *
85
     * @return AbstractImageUpload
86
     */
87
    public function setImageUploadDir($imageUploadDir)
88
    {
89
        $this->imageUploadDir = $imageUploadDir;
90
91
        return $this;
92
    }
93
94
    /*** Image URL ***/
95
    /**
96
     * @return string
97
     */
98
    public function getImageUrl()
99
    {
100
        return $this->imageUrl;
0 ignored issues
show
Bug introduced by
The property imageUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
101
    }
102
103
    /**
104
     * @param string $imageUrl
105
     */
106
    public function setImageUrl($imageUrl)
107
    {
108
        $this->imageUrl = $imageUrl;
109
110
        return $this;
111
    }
112
113
    /*** Image upload ***/
114
    /**
115
     * @return AbstractImageUpload
116
     *
117
     * @throws \Exception If upload dir and path are not set
118
     */
119 View Code Duplication
    public function imageUpload()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        if (null !== $this->getImage()) {
122
            $uploadDir = $this->getImageUploadDir();
123
            $uploadPath = $this->getImageUploadPath();
124
125
            if (!($uploadDir && $uploadPath)) {
126
                throw new \Exception('You must define the image upload dir and path!');
127
            }
128
129
            $slugify = new Slugify();
130
131
            $filename = $slugify->slugify(
132
                $this->getImage()->getClientOriginalName()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\HttpFoundation\File\File as the method getClientOriginalName() does only exist in the following sub-classes of Symfony\Component\HttpFoundation\File\File: Symfony\Component\HttpFoundation\File\UploadedFile. 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...
133
            );
134
135
            $filename .= '_'.sha1(uniqid(mt_rand(), true)).'.'.
136
                $this->getImage()->guessExtension()
137
            ;
138
139
            $this->getImage()->move(
140
                $uploadDir,
141
                $filename
142
            );
143
144
            $this->setImageUrl($uploadPath.$filename);
145
146
            $this->setImage(null);
147
        }
148
149
        return $this;
150
    }
151
}
152