Passed
Push — master ( d12f46...58955b )
by Ben
01:03
created

src/Values/Locale.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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