Badge::setFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Entity;
4
5
use Badger\Component\Game\Model\BadgeInterface;
6
use Badger\Component\Game\Model\TagInterface;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use JsonSerializable;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
/**
12
 * Badge entity.
13
 *
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class Badge implements BadgeInterface, JsonSerializable
17
{
18
    /** @var string */
19
    protected $file;
20
21
    /** @var string */
22
    protected $id;
23
24
    /** @var string */
25
    protected $title;
26
27
    /** @var string */
28
    protected $description;
29
30
    /** @var string */
31
    protected $imagePath;
32
33
    /** @var ArrayCollection */
34
    protected $tags;
35
36
    /** @var ArrayCollection */
37
    protected $completions;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getId()
43
    {
44
        return $this->id;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getTitle()
51
    {
52
        return $this->title;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function setTitle($title)
59
    {
60
        $this->title = $title;
61
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getDescription()
69
    {
70
        return $this->description;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setDescription($description)
77
    {
78
        $this->description = $description;
79
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getImagePath()
87
    {
88
        return $this->imagePath;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function setImagePath($imagePath)
95
    {
96
        $this->imagePath = $imagePath;
97
98
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function setFile(UploadedFile $file = null)
105
    {
106
        $this->file = $file;
0 ignored issues
show
Documentation Bug introduced by
It seems like $file can also be of type object<Symfony\Component...tion\File\UploadedFile>. However, the property $file is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getFile()
113
    {
114
        return $this->file;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->file; (string) is incompatible with the return type declared by the interface Badger\Component\Game\Mo...BadgeInterface::getFile of type Symfony\Component\HttpFoundation\File\UploadedFile.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function upload()
121
    {
122
        // the file property can be empty if the field is not required
123
        if (null === $this->getFile()) {
124
            return;
125
        }
126
127
        // use the original file name here but you should
128
        // sanitize it at least to avoid any security issues
129
130
        // move takes the target directory and then the
131
        // target filename to move to
132
        $this->getFile()->move(
0 ignored issues
show
Bug introduced by
The method move cannot be called on $this->getFile() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
133
            $this->getUploadRootDir(),
134
            $this->getFile()->getClientOriginalName()
0 ignored issues
show
Bug introduced by
The method getClientOriginalName cannot be called on $this->getFile() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
135
        );
136
137
        // set the path property to the filename where you've saved the file
138
        $this->imagePath = $this->getFile()->getClientOriginalName();
0 ignored issues
show
Bug introduced by
The method getClientOriginalName cannot be called on $this->getFile() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
140
        // clean up the file property as you won't need it anymore
141
        $this->file = null;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function addTag(TagInterface $tag)
148
    {
149
        $this->tags[] = $tag;
150
151
        return $this;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function setTags(ArrayCollection $tags)
158
    {
159
        $this->tags = $tags;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getTags()
166
    {
167
        return $this->tags;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getCompletions()
174
    {
175
        return $this->completions;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function setCompletions($completions)
182
    {
183
        $this->completions = $completions;
184
    }
185
186
    /**
187
     * Returns the absolute path of the image, null if no image
188
     *
189
     * @return null|string
190
     */
191
    public function getImageAbsolutePath()
192
    {
193
        return null === $this->imagePath
194
            ? null
195
            : $this->getUploadRootDir() . '/' . $this->imagePath;
196
    }
197
198
    /**
199
     * Returns the path of the image for the web, null if no image
200
     *
201
     * @return null|string
202
     */
203
    public function getImageWebPath()
204
    {
205
        return null === $this->imagePath
206
            ? null
207
            : '/' . $this->getUploadDir() . '/' . $this->imagePath;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function jsonSerialize()
214
    {
215
        return [
216
            'id'           => $this->id,
217
            'title'        => $this->title,
218
            'description'  => $this->description,
219
            'imageWebPath' => $this->getImageWebPath(),
220
        ];
221
    }
222
223
    /**
224
     * Returns the absolute directory path where uploaded documents should be saved
225
     *
226
     * @return string
227
     */
228
    protected function getUploadRootDir()
229
    {
230
        return __DIR__ . '/../../../../../web/' . $this->getUploadDir();
231
    }
232
233
    /**
234
     * Returns the path where upload files.
235
     *
236
     * Get rid of the __DIR__ so it doesn't screw up when displaying uploaded doc/image in the view.
237
     *
238
     * @return string
239
     */
240
    protected function getUploadDir()
241
    {
242
        return 'uploads/game';
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function __toString()
249
    {
250
        return $this->title;
251
    }
252
}
253