Completed
Push — master ( 574186...c63227 )
by Igor
05:18
created

Decoder::createFromRemotePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 15
nc 3
nop 6
crap 3
1
<?php
2
3
/**
4
 * @link https://github.com/rkit/filemanager-yii2
5
 * @copyright Copyright (c) 2015 Igor Romanov
6
 * @license [MIT](http://opensource.org/licenses/MIT)
7
 */
8
9
namespace rkit\filemanager;
10
11
use yii\base\InvalidValueException;
12
use rkit\filemanager\models\File;
13
14
/**
15
 * The Decoder for creating files
16
 *
17
 * @author Igor Romanov <[email protected]>
18
 * @since 1.0
19
 */
20
class Decoder
21
{
22
    /**
23
     * Create a file from the path
24
     *
25
     * @param Storage $storage;
0 ignored issues
show
Documentation introduced by
There is no parameter named $storage;. Did you maybe mean $storage?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
26
     * @param string $path Path to the file
27
     * @param int $ownerId The id of the owner
28
     * @param int $ownerType The type of the owner
29
     * @param bool $temporary The file is temporary
30
     * @param bool $protected The file is protected, not available from the web
31
     * @return \rkit\filemanager\models\File|bool
32
     * @throws InvalidValueException
33
     */
34 32
    public function createFromPath(
35
        $storage,
36
        $path,
37
        $ownerId = -1,
38
        $ownerType = -1,
39
        $temporary = false,
40
        $protected = false
41
    ) {
42 32
        $file = File::create($path, $ownerId, $ownerType, $temporary, $protected);
43 32
        if ($file) {
44 31
            $file->setStorage($storage);
45 31
            return $file->getStorage()->save($path);
46
        }
47
48 1
        throw new InvalidValueException('Unable to create from `' . $path . '`');
49
    }
50
51
    /**
52
     * Create a file from the remote path
53
     *
54
     * @param Storage $storage;
0 ignored issues
show
Documentation introduced by
There is no parameter named $storage;. Did you maybe mean $storage?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
55
     * @param string $path Path to the file or URL
56
     * @param int $ownerId The id of the owner
57
     * @param int $ownerType The type of the owner
58
     * @param bool $temporary The file is temporary
59
     * @param bool $protected The file is protected, not available from the web
60
     * @return \rkit\filemanager\models\File|bool
61
     * @throws InvalidValueException
62
     */
63 5
    public function createFromRemotePath(
64
        $storage,
65
        $path,
66
        $ownerId = -1,
67
        $ownerType = -1,
68
        $temporary = false,
69
        $protected = false
70
    ) {
71 5
        $filePath = tempnam(sys_get_temp_dir(), 'FMR');
72 5
        if ($fileContent = @file_get_contents($path)) {
73 4
            file_put_contents($filePath, $fileContent);
74 4
            $file = File::create($filePath, $ownerId, $ownerType, $temporary, $protected);
75 4
            if ($file) {
76 4
                $file->setStorage($storage);
77 4
                return $file->getStorage()->save($filePath, false);
0 ignored issues
show
Unused Code introduced by
The call to Storage::save() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
            }
79
        } // @codeCoverageIgnore
80
81 1
        throw new InvalidValueException('Unable to create from `' . $path . '`');
82
    }
83
}
84