ShortApi::current()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
rs 8.8571
cc 5
eloc 9
nc 12
nop 0
1
<?php
2
namespace Wechat\API;
3
4
/**
5
 * 微信Url相关接口.
6
 *
7
 * @author Tian.
8
 */
9
class ShortApi extends BaseApi
10
{
11
    /**
12
     *  生成短连接
13
     *
14
     * @param string $long_url 需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url
15
     * @param string $action   此处填long2short,代表长链接转短链接
16
     *
17
     * @return bool.  成功返回ok
0 ignored issues
show
Documentation introduced by
The doc-type bool. could not be parsed: Unknown type name "bool." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
18
     */
19
    public function url($long_url, $action = 'long2short')
20
    {
21
        if (!is_string($long_url)) {
22
            $this->setError('参数错误');
23
24
            return false;
25
        }
26
27
        $queryStr = [
28
            "action"   => $action,
29
            "long_url" => $long_url,
30
        ];
31
32
        $this->module = 'shorturl';
33
34
        $res = $this->_post('', $queryStr);
35
36
        return $res;
37
    }
38
39
    /**
40
     * 获取当前URL
41
     *
42
     * @return string
43
     */
44 View Code Duplication
    public static function current()
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...
Coding Style introduced by
current uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
45
    {
46
        $protocol = (!empty($_SERVER['HTTPS'])
47
            && $_SERVER['HTTPS'] !== 'off'
48
            || $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
49
50
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
51
            $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
52
        } else {
53
            $host = $_SERVER['HTTP_HOST'];
54
        }
55
56
        return $protocol . $host . $_SERVER['REQUEST_URI'];
57
    }
58
}
59