Passed
Pull Request — master (#346)
by huang
43:36 queued 18:42
created

thumbnail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 8
dl 0
loc 17
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

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
3
namespace Qiniu;
4
5
use Qiniu\Config;
6
7
if (!defined('QINIU_FUNCTIONS_VERSION')) {
8
    define('QINIU_FUNCTIONS_VERSION', Config::SDK_VER);
9
10
    /**
11
     * 计算文件的crc32检验码:
12
     *
13
     * @param $file string  待计算校验码的文件路径
14
     *
15
     * @return string 文件内容的crc32校验码
16
     */
17
    function crc32_file($file)
18
    {
19 6
        $hash = hash_file('crc32b', $file);
20 6
        $array = unpack('N', pack('H*', $hash));
21 6
        return sprintf('%u', $array[1]);
22
    }
23
24
    /**
25
     * 计算输入流的crc32检验码
26
     *
27
     * @param $data 待计算校验码的字符串
0 ignored issues
show
Documentation Bug introduced by
The doc comment 待计算校验码的字符串 at position 0 could not be parsed: Unknown type name '待计算校验码的字符串' at position 0 in 待计算校验码的字符串.
Loading history...
28
     *
29
     * @return string 输入字符串的crc32校验码
30
     */
31
    function crc32_data($data)
32
    {
33 18
        $hash = hash('crc32b', $data);
34 18
        $array = unpack('N', pack('H*', $hash));
35 18
        return sprintf('%u', $array[1]);
36
    }
37
38
    /**
39
     * 对提供的数据进行urlsafe的base64编码。
40
     *
41
     * @param string $data 待编码的数据,一般为字符串
42
     *
43
     * @return string 编码后的字符串
44
     * @link http://developer.qiniu.com/docs/v6/api/overview/appendix.html#urlsafe-base64
45
     */
46
    function base64_urlSafeEncode($data)
47
    {
48 114
        $find = array('+', '/');
49 114
        $replace = array('-', '_');
50 114
        return str_replace($find, $replace, base64_encode($data));
51
    }
52
53
    /**
54
     * 对提供的urlsafe的base64编码的数据进行解码
55
     *
56
     * @param string $str 待解码的数据,一般为字符串
57
     *
58
     * @return string 解码后的字符串
59
     */
60
    function base64_urlSafeDecode($str)
61
    {
62 21
        $find = array('-', '_');
63 21
        $replace = array('+', '/');
64 21
        return base64_decode(str_replace($find, $replace, $str));
65
    }
66
67
    /**
68
     * Wrapper for JSON decode that implements error detection with helpful
69
     * error messages.
70
     *
71
     * @param string $json JSON data to parse
72
     * @param bool $assoc When true, returned objects will be converted
73
     *                        into associative arrays.
74
     * @param int $depth User specified recursion depth.
75
     *
76
     * @return mixed
77
     * @throws \InvalidArgumentException if the JSON cannot be parsed.
78
     * @link http://www.php.net/manual/en/function.json-decode.php
79
     */
80
    function json_decode($json, $assoc = false, $depth = 512)
81
    {
82
        static $jsonErrors = array(
83
            JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
84
            JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
85
            JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
86
            JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
87
            JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
88 90
        );
89
90 90
        if (empty($json)) {
91 18
            return null;
92
        }
93 84
        $data = \json_decode($json, $assoc, $depth);
94
95 84
        if (JSON_ERROR_NONE !== json_last_error()) {
96
            $last = json_last_error();
97
            throw new \InvalidArgumentException(
98
                'Unable to parse JSON data: '
99
                . (isset($jsonErrors[$last])
100
                    ? $jsonErrors[$last]
101
                    : 'Unknown error')
102
            );
103
        }
104
105 84
        return $data;
106
    }
107
108
    /**
109
     * 计算七牛API中的数据格式
110
     *
111
     * @param string $bucket 待操作的空间名
112
     * @param string $key 待操作的文件名
113
     *
114
     * @return string  符合七牛API规格的数据格式
115
     * @link http://developer.qiniu.com/docs/v6/api/reference/data-formats.html
116
     */
117
    function entry($bucket, $key)
118
    {
119 36
        $en = $bucket;
120 36
        if (!empty($key)) {
121 36
            $en = $bucket . ':' . $key;
122 36
        }
123 36
        return base64_urlSafeEncode($en);
124
    }
125
126
    /**
127
     * array 辅助方法,无值时不set
128
     *
129
     * @param array $array 待操作array
130
     * @param string $key key
131
     * @param string $value value 为null时 不设置
132
     *
133
     * @return array 原来的array,便于连续操作
134
     */
135
    function setWithoutEmpty(&$array, $key, $value)
136
    {
137 12
        if (!empty($value)) {
138 3
            $array[$key] = $value;
139 3
        }
140 12
        return $array;
141
    }
142
143
    /**
144
     * 缩略图链接拼接
145
     *
146
     * @param  string $url 图片链接
147
     * @param  int $mode 缩略模式
148
     * @param  int $width 宽度
149
     * @param  int $height 长度
150
     * @param  string $format 输出类型
151
     * @param  int $quality 图片质量
152
     * @param  int $interlace 是否支持渐进显示
153
     * @param  int $ignoreError 忽略结果
154
     * @return string
155
     * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
156
     * @author Sherlock Ren <[email protected]>
157
     */
158
    function thumbnail(
159
        $url,
160
        $mode,
161
        $width,
162
        $height,
163
        $format = null,
164
        $quality = null,
165
        $interlace = null,
166
        $ignoreError = 1
167
    ) {
168
169 3
        static $imageUrlBuilder = null;
170 3
        if (is_null($imageUrlBuilder)) {
171 3
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
172 3
        }
173
174 3
        return call_user_func_array(array($imageUrlBuilder, 'thumbnail'), func_get_args());
175
    }
176
177
    /**
178
     * 图片水印
179
     *
180
     * @param  string $url 图片链接
181
     * @param  string $image 水印图片链接
182
     * @param  numeric $dissolve 透明度
183
     * @param  string $gravity 水印位置
184
     * @param  numeric $dx 横轴边距
185
     * @param  numeric $dy 纵轴边距
0 ignored issues
show
Bug introduced by
The type Qiniu\numeric 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...
186
     * @param  numeric $watermarkScale 自适应原图的短边比例
187
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
188
     * @return string
189
     * @author Sherlock Ren <[email protected]>
190
     */
191
    function waterImg(
192
        $url,
193
        $image,
194
        $dissolve = 100,
195
        $gravity = 'SouthEast',
196
        $dx = null,
197
        $dy = null,
198
        $watermarkScale = null
199
    ) {
200
201 3
        static $imageUrlBuilder = null;
202 3
        if (is_null($imageUrlBuilder)) {
203 3
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
204 3
        }
205
206 3
        return call_user_func_array(array($imageUrlBuilder, 'waterImg'), func_get_args());
207
    }
208
209
    /**
210
     * 文字水印
211
     *
212
     * @param  string $url 图片链接
213
     * @param  string $text 文字
214
     * @param  string $font 文字字体
215
     * @param  string $fontSize 文字字号
216
     * @param  string $fontColor 文字颜色
217
     * @param  numeric $dissolve 透明度
218
     * @param  string $gravity 水印位置
219
     * @param  numeric $dx 横轴边距
220
     * @param  numeric $dy 纵轴边距
221
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
222
     * @return string
223
     * @author Sherlock Ren <[email protected]>
224
     */
225
    function waterText(
226
        $url,
227
        $text,
228
        $font = '黑体',
229
        $fontSize = 0,
230
        $fontColor = null,
231
        $dissolve = 100,
232
        $gravity = 'SouthEast',
233
        $dx = null,
234
        $dy = null
235
    ) {
236
237 3
        static $imageUrlBuilder = null;
238 3
        if (is_null($imageUrlBuilder)) {
239 3
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
240 3
        }
241
242 3
        return call_user_func_array(array($imageUrlBuilder, 'waterText'), func_get_args());
243
    }
244
245
    /**
246
     *  从uptoken解析accessKey和bucket
247
     *
248
     * @param $upToken
249
     * @return array(ak,bucket,err=null)
250
     */
251
    function explodeUpToken($upToken)
252
    {
253 18
        $items = explode(':', $upToken);
254 18
        if (count($items) != 3) {
255
            return array(null, null, "invalid uptoken");
256
        }
257 18
        $accessKey = $items[0];
258 18
        $putPolicy = json_decode(base64_urlSafeDecode($items[2]));
259 18
        $scope = $putPolicy->scope;
260 18
        $scopeItems = explode(':', $scope);
261 18
        $bucket = $scopeItems[0];
262 18
        return array($accessKey, $bucket, null);
263
    }
264
}
265