Completed
Push — master ( 979671...07c2ec )
by reallyli
11:13 queued 09:43
created

notification.php ➔ sendGroupMessage()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 40
rs 9.28
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
    if (! $url) {
30
        throw new \InvalidArgumentException('[Laravel-Deployer]Notification is on but channel url is not set!');
31
    }
32
33
    $notifyBy = get('notify_by', 'webhook');
34
35
    switch ($notifyBy) {
36
        case 'wechat_bot':
37
            $formParams = [
38
                'msgtype' => 'news',
39
                'news' => [
40
                    'articles' => [
41
                        [
42
                            'title' => get('user').' '.$subject,
43
                            'description' =>  '在 '.get('environment').' 环境更新 '.get('branch').' 分支 ',
44
                            'url' => get('app_repo_url', 'https://github.com'),
45
                            'picurl' => get('pic_url', 'https://picsum.photos/id/'.rand(1, 1000).'/800/600'),
46
                        ],
47
                    ],
48
                ],
49
            ];
50
            break;
51
        default:
52
            $content = implode("\n", [
53
                $subject,
54
                '应用名称: '.get('application'),
55
                '发布者: '.get('user'),
56
                '分支名: '.get('branch'),
57
                '环境: '.get('environment'),
58
            ]);
59
            $formParams = ['text' => $content];
60
            break;
61
    }
62
63
    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...
64
}
65
66
task('success:notify', function () {
67
    return sendGroupMessage('成功发布新版本!');
68
})->local();
69
70
task('failed:notify', function () {
71
    return sendGroupMessage('发布新版本失败!');
72
})->local();
73