Completed
Pull Request — master (#5)
by reallyli
01:31
created

notification.php ➔ sendHttpRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace Deployer;
4
5
function sendHttpRequest($url, $formParams)
6
{
7
    $formParams = json_encode($formParams);
8
9
    $ch = curl_init($url);
10
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
11
    curl_setopt($ch, CURLOPT_POSTFIELDS, $formParams);
12
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
13
    curl_setopt(
14
        $ch,
15
        CURLOPT_HTTPHEADER,
16
        [
17
            'Content-Type: application/json',
18
            'Content-Length: '.strlen($formParams),
19
        ]
20
    );
21
22
    return curl_exec($ch);
23
}
24
25
function sendGroupMessage($subject)
26
{
27
    $url = get('notify_channel_url');
28
29
30
    if (! $url) {
31
        throw new \InvalidArgumentException('[Laravel-Deployer]Notification is on but channel url is not set!');
32
    }
33
34
    $notifyBy = get('notify_by', 'webhook');
35
36
    switch ($notifyBy) {
37
        case 'wechat_bot':
38
            $formParams = [
39
                'msgtype' => 'news',
40
                'news' => [
41
                    'articles' => [
42
                        [
43
                            'title' => get('user') . ' ' . $subject,
44
                            'description' =>  '在 ' . get('environment') . ' 环境更新 ' . get('branch') . ' 分支 ',
45
                            'url' => get('app_repo_url', 'https://github.com'),
46
                            'picurl' => get('pic_url', 'https://picsum.photos/id/' . rand(1, 1000) . '/800/600')
47
                        ]
48
                    ]
49
                ]
50
            ];
51
            break;
52
        default:
53
            $content = implode("\n", [
54
                $subject,
55
                '应用名称: '.get('application'),
56
                '发布者: '.get('user'),
57
                '分支名: '.get('branch'),
58
                '环境: '.get('environment'),
59
            ]);
60
            $formParams =  ['text' => $content];
61
            break;
62
    }
63
64
    return get('group_notify') ? sendHttpRequest($url, $formParams) : writeln($content);
0 ignored issues
show
Bug introduced by
The variable $content does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
65
}
66
67
task('success:notify', function () {
68
    return sendGroupMessage('成功发布新版本!');
69
})->local();
70
71
task('failed:notify', function () {
72
    return sendGroupMessage('发布新版本失败!');
73
})->local();
74