Url::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Uploader\Adapters;
4
5
use Uploader\Uploader;
6
7
/**
8
 * Adapter to save a file from an url.
9
 */
10
class Url implements AdapterInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function check($original)
16
    {
17
        return is_string($original) && filter_var($original, FILTER_VALIDATE_URL);
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(parse_url($original, PHP_URL_PATH));
0 ignored issues
show
Security Bug introduced by
It seems like parse_url($original, PHP_URL_PATH) targeting parse_url() can also be of type false; however, Uploader\Uploader::parsePath() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
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 (!@rename($original['tmp_name'], $destination)) {
42
            throw new \RuntimeException("Unable to copy '{$original['tmp_name']}' to '{$destination}'");
43
        }
44
    }
45
}
46