GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 295927...ec8326 )
by Carlos
02:26
created

Messenger   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 91
c 0
b 0
f 0
wmc 13
lcom 1
cbo 6
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 35 5
B formatGateways() 0 20 7
1
<?php
2
3
/*
4
 * This file is part of the overtrue/easy-sms.
5
 * (c) overtrue <[email protected]>
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Overtrue\EasySms;
11
12
use Overtrue\EasySms\Contracts\MessageInterface;
13
14
/**
15
 * Class Messenger.
16
 */
17
class Messenger
18
{
19
    const STATUS_SUCCESS = 'success';
20
    const STATUS_ERRED = 'erred';
21
22
    /**
23
     * @var \Overtrue\EasySms\EasySms
24
     */
25
    protected $easySms;
26
27
    /**
28
     * Messenger constructor.
29
     *
30
     * @param \Overtrue\EasySms\EasySms $easySms
31
     */
32
    public function __construct(EasySms $easySms)
33
    {
34
        $this->easySms = $easySms;
35
    }
36
37
    /**
38
     * Send a message.
39
     *
40
     * @param string                                       $to
41
     * @param \Overtrue\EasySms\Contracts\MessageInterface $message
42
     * @param array                                        $gateways
43
     *
44
     * @return array
45
     */
46
    public function send($to, MessageInterface $message, array $gateways = [])
47
    {
48
        if (!($message instanceof MessageInterface)) {
49
            $message = new Message([
50
                'content' => $message,
51
                'template' => $message,
52
            ]);
53
        }
54
55
        $results = [];
56
57
        if (empty($gateways)) {
58
            $gateways = $message->getGateways();
59
        }
60
61
        $gateways = $this->formatGateways($gateways);
62
        $strategyAppliedGateways = $this->easySms->strategy()->apply($gateways);
63
64
        foreach ($strategyAppliedGateways as $gateway) {
65
            try {
66
                $result[$gateway] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
                        'status' => self::STATUS_SUCCESS,
68
                        'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($gateways[$gateway])),
69
                    ];
70
            } catch (GatewayErrorException $e) {
0 ignored issues
show
Bug introduced by
The class Overtrue\EasySms\GatewayErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
71
                $result[$gateway] = [
0 ignored issues
show
Bug introduced by
The variable $result 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...
72
                    'status' => self::STATUS_ERRED,
73
                    'exception' => $e,
74
                ];
75
                continue;
76
            }
77
        }
78
79
        return $results;
80
    }
81
82
    /**
83
     * @param array $gateways
84
     *
85
     * @return array
86
     */
87
    protected function formatGateways(array $gateways)
88
    {
89
        $formatted = [];
90
        $config = $this->easySms->getConfig();
91
92
        foreach ($gateways as $gateway => $setting) {
93
            if (is_integer($gateway) && is_string($setting)) {
94
                $gateway = $setting;
95
                $setting = [];
96
            }
97
98
            $globalSetting = $config->get("gateways.{$gateway}", []);
99
100
            if (is_string($gateway) && !empty($globalSetting) && is_array($setting)) {
101
                $formatted[$gateway] = array_merge($globalSetting, $setting);
102
            }
103
        }
104
105
        return $formatted;
106
    }
107
}
108