Completed
Push — master ( 0f12bf...94f146 )
by Oscar
02:51
created

Upload   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
c 5
b 1
f 0
lcom 0
cbo 1
dl 12
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 4 2
A fixDestination() 12 12 3
B save() 0 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Uploader\Adapters;
4
5
use Uploader\Uploader;
6
7
/**
8
 * Adapter to save a file from upload ($_FILES).
9
 */
10
class Upload implements AdapterInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function check($original)
16
    {
17
        return is_array($original) && isset($original['tmp_name']);
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 View Code Duplication
    public static function fixDestination(Uploader $uploader, $original)
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...
24
    {
25
        $path = Uploader::parsePath($original['name']);
26
27
        if (!$uploader->getFilename()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $uploader->getFilename() of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $uploader->setFilename($path['filename']);
29
        }
30
31
        if (!$uploader->getExtension()) {
32
            $uploader->setExtension($path['extension']);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function save($original, $destination)
40
    {
41
        if (empty($original['tmp_name']) || !empty($original['error'])) {
42
            throw new \RuntimeException('Unable to copy the uploaded file because has an error');
43
        }
44
45
        $moved = php_sapi_name() == 'cli' ? rename($original['tmp_name'], $destination) : move_uploaded_file($original['tmp_name'], $destination);
46
47
        if (!$moved) {
48
            throw new \RuntimeException("Unable to copy '{$original['tmp_name']}' to '{$destination}'");
49
        }
50
51
        chmod($destination, 0755);
52
    }
53
}
54