Configuration::getTimezone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Server;
13
14
use Slince\Config\Config;
15
use Spike\Common\Authentication\AuthenticationInterface;
16
use Spike\Common\Authentication;
17
18
class Configuration  extends Config
19
{
20
    /**
21
     * Gets the current timezone.
22
     *
23
     * @return string
24
     */
25
    public function getTimezone()
26
    {
27
        return $this->get('timezone', 'Asia/shanghai');
28
    }
29
30
    /**
31
     * Gets the log file.
32
     *
33
     * @return string
34
     */
35
    public function getLogFile()
36
    {
37
        return isset($this['log']['file']) ? $this['log']['file'] : getcwd().'/access.log';
38
    }
39
40
    /**
41
     * Gets the log level.
42
     *
43
     * @return int
44
     */
45
    public function getLogLevel()
46
    {
47
        return  isset($this['log']['level']) ? $this['log']['level'] : 'info';
48
    }
49
50
    /**
51
     * Gets the server address to bind.
52
     *
53
     * @return string
54
     */
55
    public function getAddress()
56
    {
57
        $address = $this->get('address', '0.0.0.0:8090');
58
59
        return $address;
60
    }
61
62
    /**
63
     * Gets the config file.
64
     *
65
     * @return string
66
     */
67
    public function getDefaultConfigFile()
68
    {
69
        return getcwd().'/'.'spiked.json';
70
    }
71
72
    /**
73
     * Gets the authentication.
74
     *
75
     * @return AuthenticationInterface|null
76
     */
77
    public function getAuthentication()
78
    {
79
        $auth = $this->get('auth', []);
80
        $type = isset($auth['type']) ? $auth['type'] : 'simple_password';
81
        unset($auth['type']);
82
        if ($auth) {
83
            switch ($type) {
84
                default:
0 ignored issues
show
Unused Code introduced by
default: $authentica...tion($auth); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
85
                    $authentication = new Authentication\PasswordAuthentication($auth);
86
                    break;
87
            }
88
        } else {
89
            $authentication = null;
90
        }
91
92
        return $authentication;
0 ignored issues
show
Bug introduced by
The variable $authentication 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...
93
    }
94
}