bootstrap.php ➔ getCredential()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * 案例演示.
4
 */
5
use Slince\SmartQQ\Credential;
6
7
include __DIR__.'/../vendor/autoload.php';
8
9
date_default_timezone_set('Prc');
10
11
//二维码图片
12
define('LOGIN_QR_IMAGE', getcwd().'/smartqq-login-qr.png');
13
14
//登录凭证信息保存
15
define('CREDENTIAL_JSON', getcwd().'/credential.json');
16
17
/**
18
 * 打印结果到屏幕.
19
 *
20
 * @param $data
21
 */
22
function printPrettyScreen($data)
23
{
24
//    printR($data);
25
    @file_put_contents(getcwd().'/result.log', print_r($data, true)."\r\n", FILE_APPEND);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
26
}
27
28
/**
29
 * 有效输出.
30
 *
31
 * @param $data
32
 */
33
function printR($data)
34
{
35
    //windows中文cmd使用的是gbk编码故需要转换
36
    print_r($data);
37
//    echo mb_convert_encoding(print_r($data, true), 'gbk', 'utf-8');
38
}
39
40
/**
41
 * 获取登录凭证
42
 *
43
 * @return Credential
44
 */
45
function getCredential()
46
{
47
    @$credentialParameters = json_decode(file_get_contents(CREDENTIAL_JSON), true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
    if (!$credentialParameters) {
49
        exit('Please execute login first');
0 ignored issues
show
Coding Style Compatibility introduced by
The function getCredential() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
50
    }
51
52
    return Credential::fromArray($credentialParameters);
53
}
54
55
/**
56
 * 保存登录凭证
57
 *
58
 * @param Credential $credential
59
 */
60
function saveCredential(Credential $credential)
61
{
62
    @file_put_contents(CREDENTIAL_JSON, json_encode($credential->toArray()));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
}