UploadManager::putFile()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5.5069

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 5
eloc 36
c 3
b 1
f 0
nc 8
nop 10
dl 0
loc 55
ccs 24
cts 33
cp 0.7272
crap 5.5069
rs 9.0328

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Config;
5
use Qiniu\Http\HttpClient;
0 ignored issues
show
Bug introduced by
The type Qiniu\Http\HttpClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Qiniu\Http\RequestOptions;
7
use Qiniu\Storage\ResumeUploader;
8
use Qiniu\Storage\FormUploader;
9
10
/**
11
 * 主要涉及了资源上传接口的实现
12
 *
13
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/
14
 */
15
final class UploadManager
16
{
17
    private $config;
18 12
    /**
19
     * @var RequestOptions
20 12
     */
21 9
    private $reqOpt;
22 9
23 12
    /**
24 12
     * @param Config|null $config
25
     * @param RequestOptions|null $reqOpt
26
     */
27
    public function __construct(Config $config = null, RequestOptions $reqOpt = null)
28
    {
29
        if ($config === null) {
30
            $config = new Config();
31
        }
32
        $this->config = $config;
33
34
        if ($reqOpt === null) {
35
            $reqOpt = new RequestOptions();
36
        }
37
38
        $this->reqOpt = $reqOpt;
39
    }
40
41
    /**
42
     * 上传二进制流到七牛
43 3
     *
44
     * @param string $upToken 上传凭证
45
     * @param string $key 上传文件名
46
     * @param string $data 上传二进制流
47
     * @param array<string, string> $params 自定义变量,规格参考
48
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
49
     * @param string $mime 上传数据的mimeType
50
     * @param string $fname
51
     * @param RequestOptions $reqOpt
52 3
     * @return array    包含已上传文件的信息,类似:
53 3
     *                                              [
54 3
     *                                                  "hash" => "<Hash string>",
55 3
     *                                                  "key" => "<Key string>"
56 3
     *                                              ]
57 3
     */
58 3
    public function put(
59 3
        $upToken,
60
        $key,
61 3
        $data,
62
        $params = null,
63
        $mime = 'application/octet-stream',
64
        $fname = "default_filename",
65
        $reqOpt = null
66
    ) {
67
        $reqOpt = $reqOpt === null ? $this->reqOpt : $reqOpt;
68
69
        $params = self::trimParams($params);
70
        return FormUploader::put(
71
            $upToken,
72
            $key,
73
            $data,
74
            $this->config,
75
            $params,
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type array and array; however, parameter $params of Qiniu\Storage\FormUploader::put() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            /** @scrutinizer ignore-type */ $params,
Loading history...
76
            $mime,
77
            $fname,
78
            $reqOpt
79
        );
80
    }
81
82 9
83
    /**
84
     * 上传文件到七牛
85
     *
86
     * @param string $upToken 上传凭证
87
     * @param string $key 上传文件名
88
     * @param string $filePath 上传文件的路径
89
     * @param array<string, mixed> $params 定义变量,规格参考
90
     *                                     http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
91 9
     * @param boolean $mime 上传数据的mimeType
92 9
     * @param string $checkCrc 是否校验crc32
93
     * @param string $resumeRecordFile 断点续传文件路径 默认为null
94
     * @param string $version 分片上传版本 目前支持v1/v2版本 默认v1
95 9
     * @param int $partSize 分片上传v2字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
96 9
     *
97 9
     * @return array<string, mixed> 包含已上传文件的信息,类似:
98 9
     *                                              [
99 3
     *                                                  "hash" => "<Hash string>",
100 3
     *                                                  "key" => "<Key string>"
101 3
     *                                              ]
102
     * @throws \Exception
103
     */
104 3
    public function putFile(
105 3
        $upToken,
106 3
        $key,
107 3
        $filePath,
108 3
        $params = null,
109 3
        $mime = 'application/octet-stream',
110 3
        $checkCrc = false,
0 ignored issues
show
Unused Code introduced by
The parameter $checkCrc is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
        /** @scrutinizer ignore-unused */ $checkCrc = false,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111 3
        $resumeRecordFile = null,
112 3
        $version = 'v1',
113
        $partSize = config::BLOCK_SIZE,
114
        $reqOpt = null
115 6
    ) {
116 6
        $reqOpt = $reqOpt === null ? $this->reqOpt : $reqOpt;
117 6
118 6
        $file = fopen($filePath, 'rb');
119 6
        if ($file === false) {
120 6
            throw new \Exception("file can not open", 1);
121 6
        }
122 6
        $params = self::trimParams($params);
123 6
        $stat = fstat($file);
124 6
        $size = $stat['size'];
125 6
        if ($size <= Config::BLOCK_SIZE) {
126 6
            $data = fread($file, $size);
127
            fclose($file);
128
            if ($data === false) {
129 12
                throw new \Exception("file can not read", 1);
130
            }
131 12
            return FormUploader::put(
132 12
                $upToken,
133
                $key,
134
                $data,
135
                $this->config,
136
                $params,
0 ignored issues
show
Bug introduced by
It seems like $params can also be of type array and array; however, parameter $params of Qiniu\Storage\FormUploader::put() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
                /** @scrutinizer ignore-type */ $params,
Loading history...
137
                $mime,
138
                basename($filePath),
139
                $reqOpt
140
            );
141
        }
142
143
        $up = new ResumeUploader(
144
            $upToken,
145
            $key,
146
            $file,
147
            $size,
148
            $params,
149
            $mime,
150
            $this->config,
151
            $resumeRecordFile,
152
            $version,
153
            $partSize,
154
            $reqOpt
155
        );
156
        $ret = $up->upload(basename($filePath));
157
        fclose($file);
158
        return $ret;
159
    }
160
161
    public static function trimParams($params)
162
    {
163
        if ($params === null) {
164
            return null;
165
        }
166
        $ret = array();
167
        foreach ($params as $k => $v) {
168
            $pos1 = strpos($k, 'x:');
169
            $pos2 = strpos($k, 'x-qn-meta-');
170
            if (($pos1 === 0 || $pos2 === 0) && !empty($v)) {
171
                $ret[$k] = $v;
172
            }
173
        }
174
        return $ret;
175
    }
176
}
177