thumbnail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

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

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 string 待计算校验码的字符串
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
     * 二维数组根据某个字段排序
69
     * @param array $array 要排序的数组
70
     * @param string $key 要排序的键
71
     * @param string $sort  排序类型 SORT_ASC SORT_DESC
72
     * return array 排序后的数组
73
     */
74
    function arraySort($array, $key, $sort = SORT_ASC)
75
    {
76
        $keysValue = array();
77
        foreach ($array as $k => $v) {
78
            $keysValue[$k] = $v[$key];
79
        }
80
        array_multisort($keysValue, $sort, $array);
81
        return $array;
82
    }
83
84
    /**
85
     * Wrapper for JSON decode that implements error detection with helpful
86
     * error messages.
87
     *
88 90
     * @param string $json JSON data to parse
89
     * @param bool $assoc When true, returned objects will be converted
90 90
     *                        into associative arrays.
91 18
     * @param int $depth User specified recursion depth.
92
     *
93 84
     * @return mixed
94
     * @throws \InvalidArgumentException if the JSON cannot be parsed.
95 84
     * @link http://www.php.net/manual/en/function.json-decode.php
96
     */
97
    function json_decode($json, $assoc = false, $depth = 512)
98
    {
99
        static $jsonErrors = array(
100
            JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
101
            JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
102
            JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
103
            JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
104
            JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'
105 84
        );
106
107
        if (empty($json)) {
108
            return null;
109
        }
110
        $data = \json_decode($json, $assoc, $depth);
111
112
        if (JSON_ERROR_NONE !== json_last_error()) {
113
            $last = json_last_error();
114
            throw new \InvalidArgumentException(
115
                'Unable to parse JSON data: '
116
                . (isset($jsonErrors[$last])
117
                    ? $jsonErrors[$last]
118
                    : 'Unknown error')
119 36
            );
120 36
        }
121 36
122 36
        return $data;
123 36
    }
124
125
    /**
126
     * 计算七牛API中的数据格式
127
     *
128
     * @param string $bucket 待操作的空间名
129
     * @param string $key 待操作的文件名
130
     *
131
     * @return string  符合七牛API规格的数据格式
132
     * @link https://developer.qiniu.com/kodo/api/data-format
133
     */
134
    function entry($bucket, $key = null)
135
    {
136
        $en = $bucket;
137 12
        if ($key !== null) {
138 3
            $en = $bucket . ':' . $key;
139 3
        }
140 12
        return base64_urlSafeEncode($en);
141
    }
142
143
    function decodeEntry($entry)
144
    {
145
        $en = base64_urlSafeDecode($entry);
146
        $en = explode(':', $en);
147
        if (count($en) == 1) {
148
            return array($en[0], null);
149
        }
150
        return array($en[0], $en[1]);
151
    }
152
153
    /**
154
     * array 辅助方法,无值时不set
155
     *
156
     * @param array $array 待操作array
157
     * @param string $key key
158
     * @param string $value value 为null时 不设置
159
     *
160
     * @return array 原来的array,便于连续操作
161
     */
162
    function setWithoutEmpty(&$array, $key, $value)
163
    {
164
        if (!empty($value)) {
165
            $array[$key] = $value;
166
        }
167
        return $array;
168
    }
169 3
170 3
    /**
171 3
     * 缩略图链接拼接
172 3
     *
173
     * @param  string $url 图片链接
174 3
     * @param  int $mode 缩略模式
175
     * @param  int $width 宽度
176
     * @param  int $height 长度
177
     * @param  string $format 输出类型
178
     * @param  int $quality 图片质量
179
     * @param  int $interlace 是否支持渐进显示
180
     * @param  int $ignoreError 忽略结果
181
     * @return string
182
     * @link http://developer.qiniu.com/code/v6/api/kodo-api/image/imageview2.html
183
     * @author Sherlock Ren <[email protected]>
184
     */
185
    function thumbnail(
186
        $url,
187
        $mode,
188
        $width,
189
        $height,
190
        $format = null,
191
        $quality = null,
192
        $interlace = null,
193
        $ignoreError = 1
194
    ) {
195
196
        static $imageUrlBuilder = null;
197
        if (is_null($imageUrlBuilder)) {
198
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
199
        }
200
201 3
        return call_user_func_array(array($imageUrlBuilder, 'thumbnail'), func_get_args());
202 3
    }
203 3
204 3
    /**
205
     * 图片水印
206 3
     *
207
     * @param  string $url 图片链接
208
     * @param  string $image 水印图片链接
209
     * @param  numeric $dissolve 透明度
210
     * @param  string $gravity 水印位置
211
     * @param  numeric $dx 横轴边距
212
     * @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...
213
     * @param  numeric $watermarkScale 自适应原图的短边比例
214
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html
215
     * @return string
216
     * @author Sherlock Ren <[email protected]>
217
     */
218
    function waterImg(
219
        $url,
220
        $image,
221
        $dissolve = 100,
222
        $gravity = 'SouthEast',
223
        $dx = null,
224
        $dy = null,
225
        $watermarkScale = null
226
    ) {
227
228
        static $imageUrlBuilder = null;
229
        if (is_null($imageUrlBuilder)) {
230
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
231
        }
232
233
        return call_user_func_array(array($imageUrlBuilder, 'waterImg'), func_get_args());
234
    }
235
236
    /**
237 3
     * 文字水印
238 3
     *
239 3
     * @param  string $url 图片链接
240 3
     * @param  string $text 文字
241
     * @param  string $font 文字字体
242 3
     * @param  string $fontSize 文字字号
243
     * @param  string $fontColor 文字颜色
244
     * @param  numeric $dissolve 透明度
245
     * @param  string $gravity 水印位置
246
     * @param  numeric $dx 横轴边距
247
     * @param  numeric $dy 纵轴边距
248
     * @link   http://developer.qiniu.com/code/v6/api/kodo-api/image/watermark.html#text-watermark
249
     * @return string
250
     * @author Sherlock Ren <[email protected]>
251
     */
252
    function waterText(
253 18
        $url,
254 18
        $text,
255
        $font = '黑体',
256
        $fontSize = 0,
257 18
        $fontColor = null,
258 18
        $dissolve = 100,
259 18
        $gravity = 'SouthEast',
260 18
        $dx = null,
261 18
        $dy = null
262 18
    ) {
263
264
        static $imageUrlBuilder = null;
265
        if (is_null($imageUrlBuilder)) {
266
            $imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder;
267
        }
268
269
        return call_user_func_array(array($imageUrlBuilder, 'waterText'), func_get_args());
270
    }
271
272
    /**
273
     *  从uptoken解析accessKey和bucket
274
     *
275
     * @param $upToken
276
     * @return array(ak,bucket,err=null)
277
     */
278
    function explodeUpToken($upToken)
279
    {
280
        $items = explode(':', $upToken);
281
        if (count($items) != 3) {
282
            return array(null, null, "invalid uptoken");
283
        }
284
        $accessKey = $items[0];
285
        $putPolicy = json_decode(base64_urlSafeDecode($items[2]));
286
        $scope = $putPolicy->scope;
287
        $scopeItems = explode(':', $scope);
288
        $bucket = $scopeItems[0];
289
        return array($accessKey, $bucket, null);
290
    }
291
292
    // polyfill ucwords for `php version < 5.4.32` or `5.5.0 <= php version < 5.5.16`
293
    if (version_compare(phpversion(), "5.4.32") < 0 ||
294
        (
295
            version_compare(phpversion(), "5.5.0") >= 0 &&
296
            version_compare(phpversion(), "5.5.16") < 0
297
        )
298
    ) {
299
        function ucwords($str, $delimiters = " \t\r\n\f\v")
300
        {
301
            $delims = preg_split('//u', $delimiters, -1, PREG_SPLIT_NO_EMPTY);
302
303
            foreach ($delims as $delim) {
304
                $str = implode($delim, array_map('ucfirst', explode($delim, $str)));
305
            }
306
307
            return $str;
308
        }
309
    } else {
310
        function ucwords($str, $delimiters)
311
        {
312
            return \ucwords($str, $delimiters);
313
        }
314
    }
315
316
    /**
317
     * 将 parse_url 的结果转换回字符串
318
     * TODO: add unit test
319
     *
320
     * @param $parsed_url - parse_url 的结果
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...
321
     * @return string
322
     */
323
    function unparse_url($parsed_url)
324
    {
325
326
        $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
327
328
        $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
329
330
        $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
331
332
        $user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';
333
334
        $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';
335
336
        $pass     = ($user || $pass) ? "$pass@" : '';
337
338
        $path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';
339
340
        $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
341
342
        $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
343
344
        return "$scheme$user$pass$host$port$path$query$fragment";
345
    }
346
}
347