Completed
Push — master ( 760626...81d11d )
by Carlos
02:46
created

AccessToken   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 39.13 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 18
loc 46
rs 10
c 0
b 0
f 0
ccs 0
cts 14
cp 0
wmc 2
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A getTokenFromServer() 18 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * AccessToken.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\OpenPlatform;
28
29
use EasyWeChat\Core\AccessToken as WechatAccessToken;
30
use EasyWeChat\Core\Exceptions\HttpException;
31
use EasyWeChat\OpenPlatform\Traits\VerifyTicket;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EasyWeChat\OpenPlatform\VerifyTicket.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
32
33
class AccessToken extends WechatAccessToken
34
{
35
    use VerifyTicket;
36
37
    /**
38
     * API.
39
     */
40
    const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
41
42
    /**
43
     * {@inheritdoc}.
44
     */
45
    protected $queryName = 'component_access_token';
46
47
    /**
48
     * {@inheritdoc}.
49
     */
50
    protected $tokenJsonKey = 'component_access_token';
51
52
    /**
53
     * {@inheritdoc}.
54
     */
55
    protected $prefix = 'easywechat.common.component_access_token.';
56
57
    /**
58
     * {@inheritdoc}.
59
     */
60 View Code Duplication
    public function getTokenFromServer()
61
    {
62
        $data = [
63
            'component_appid' => $this->appId,
64
            'component_appsecret' => $this->secret,
65
            'component_verify_ticket' => $this->verifyTicket->getTicket(),
66
        ];
67
68
        $http = $this->getHttp();
69
70
        $token = $http->parseJSON($http->json(self::API_TOKEN_GET, $data));
71
72
        if (empty($token[$this->tokenJsonKey])) {
73
            throw new HttpException('Request ComponentAccessToken fail. response: '.json_encode($token, JSON_UNESCAPED_UNICODE));
74
        }
75
76
        return $token;
77
    }
78
}
79