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.

SmsapiServiceProvider::boot()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 39
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 30
nc 2
nop 0
crap 30
1
<?php
2
3
namespace NotificationChannels\Smsapi;
4
5
use SMSApi\Client;
6
use Illuminate\Support\ServiceProvider;
7
8
class SmsapiServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap the application services.
12
     */
13
    public function boot()
14
    {
15
        $this->app->when(SmsapiChannel::class)
16
            ->needs(SmsapiClient::class)
17
            ->give(function () {
18
                $config = config('smsapi');
19
                $auth = $config['auth'];
20
                if ($auth['method'] === 'token') {
21
                    $client = Client::createFromToken($auth['credentials']['token']);
22
                } elseif ($auth['method'] === 'password') {
23
                    $client = new Client($auth['credentials']['username']);
24
                    $client->setPasswordHash($auth['credentials']['password']);
25
                }
26
                $defaults = $config['defaults'] + ['sms' => [], 'mms' => [], 'vms' => []];
27
                if (! empty($defaults['common'])) {
28
                    $defaults['common'] = array_only($defaults['common'], [
29
                        'notify_url', 'partner', 'test',
30
                    ]);
31
                    $defaults['sms'] = array_only($defaults['sms'] + $defaults['common'], [
32
                        'from', 'fast', 'flash', 'encoding', 'normalize', 'nounicode', 'single',
33
                    ]);
34
                    $defaults['mms'] = array_only($defaults['mms'] + $defaults['common'], [
35
                    ]);
36
                    $defaults['vms'] = array_only($defaults['vms'] + $defaults['common'], [
37
                        'from', 'tries', 'interval', 'tts_lector', 'skip_gsm',
38
                    ]);
39
                }
40
                $defaults = array_only($defaults, ['sms', 'mms', 'vms']);
41
                $defaults = array_map(function (array $defaults) {
42
                    return array_filter($defaults, function ($value) {
43
                        return $value !== null;
44
                    });
45
                }, $defaults);
46
47
                return new SmsapiClient($client, $defaults);
0 ignored issues
show
Bug introduced by
The variable $client 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...
48
            });
49
50
        if ($this->app->runningInConsole()) {
51
            $this->publishes([
52
                __DIR__.'/../config/smsapi.php' => config_path('smsapi.php'),
53
            ], 'config');
54
        }
55
    }
56
57
    /**
58
     * Register the application services.
59
     */
60
    public function register()
61
    {
62
        $this->mergeConfigFrom(__DIR__.'/../config/smsapi.php', 'smsapi');
63
    }
64
}
65