Completed
Pull Request — master (#3)
by Andre
01:43
created

NicknameMapper::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\Nickname;
7
8
class NicknameMapper extends AbstractMapper
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $delimiters = [
14
        '[' => ']',
15
        '{' => '}',
16
        '(' => ')',
17
        '<' => '>',
18
        '"' => '"',
19
        '\'' => '\''
20
    ];
21
22
    public function __construct(array $delimiters = [])
23
    {
24
        if (!empty($delimiters)) {
25
            $this->delimiters = $delimiters;
26
        }
27
    }
28
29
    /**
30
     * map nicknames in the parts array
31
     *
32
     * @param array $parts the name parts
33
     * @return array the mapped parts
34
     */
35
    public function map(array $parts): array
36
    {
37
        $isEncapsulated = false;
38
39
        $regexp = $this->buildRegexp();
40
41
        foreach ($parts as $k => $part) {
42
            if ($part instanceof AbstractPart) {
43
                continue;
44
            }
45
46
            if (preg_match($regexp, $part, $matches)) {
47
                $isEncapsulated = true;
48
                $part = substr($part, 1);
49
                $closing = $this->delimiters[$matches[1]];
50
            }
51
52
            if (!$isEncapsulated) {
53
                continue;
54
            }
55
56
            if ($closing === substr($part, -1)) {
0 ignored issues
show
Bug introduced by
The variable $closing 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...
57
                $isEncapsulated = false;
58
                $part = substr($part, 0, -1);
59
            }
60
61
            $parts[$k] = new Nickname(str_replace(['"', '\''], '', $part));
62
        }
63
64
        return $parts;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    protected function buildRegexp()
71
    {
72
        $regexp = '/^([';
73
74
        foreach ($this->delimiters as $opening => $closing) {
75
            $regexp .= sprintf('\\%s', $opening);
76
        }
77
78
        $regexp .= '])/';
79
80
        return $regexp;
81
    }
82
}
83