Completed
Push — master ( aee3a6...824acf )
by Ben
23:56
created

Locale::withoutRegion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale\Values;
4
5
final class Locale
6
{
7
    /**
8
     * @var string Locale key identifier
9
     */
10
    private $value;
11
12 82
    private function __construct(string $value)
13
    {
14 82
        $this->value = $value;
15 82
    }
16
17 82
    public static function from(string $value)
18
    {
19 82
        return new static($value);
20
    }
21
22 2
    public function withoutRegion()
23
    {
24 2
        if(false !== strpos($this->value, '-')){
25 2
            $value = substr($this->value,0,strpos($this->value, '-'));
26 1
        }else if(false !== strpos($this->value, '_')){
27 1
            $value = substr($this->value,0,strpos($this->value, '_'));
28
        }
29
30 2
        return new static($value);
0 ignored issues
show
Bug introduced by
The variable $value 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...
31
    }
32
33 69
    public function get(): string
34
    {
35 69
        return $this->value;
36
    }
37
38 1
    public function equals(self $other): bool
39
    {
40 1
        return get_class($this) === get_class($other) && (string) $this === (string) $other;
41
    }
42
43 69
    public function __toString(): string
44
    {
45 69
        return $this->get();
46
    }
47
}
48