Completed
Pull Request — master (#12)
by Frederik
12:11
created

HeaderValueParameter::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 4
eloc 7
nc 3
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Header;
5
6
/**
7
 * Class HeaderValueParameter
8
 * @package Genkgo\Mail\Header
9
 */
10
final class HeaderValueParameter
11
{
12
    /**
13
     *
14
     */
15
    public CONST RFC_822_T_SPECIAL = "\x28\x29\x3C\x3E\x40\x2C\x3B\x3A\x5C\x22\x5B\x5D\x2E";
16
17
    /**
18
     *
19
     */
20
    private CONST ANY_ASCII_EXCEPT_SPACE_CTL = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
21
22
    /**
23
     * @var string
24
     */
25
    private $name;
26
27
    /**
28
     * @var string
29
     */
30
    private $value;
31
32 28
    /**
33
     * @var string
34 28
     */
35
    private $specials = self::RFC_822_T_SPECIAL;
36
37
    /**
38 28
     * HeaderValue constructor.
39
     * @param string $name
40
     * @param string $value
41
     */
42 28
    public function __construct(string $name, string $value)
43 28
    {
44 28
        if ($name === '' || strcspn($name, self::ANY_ASCII_EXCEPT_SPACE_CTL) !== strlen($name)) {
45
            throw new \InvalidArgumentException('Incorrect name');
46
        }
47
48
        if (strcspn($value, self::ANY_ASCII_EXCEPT_SPACE_CTL) !== strlen($value)) {
49 28
            throw new \InvalidArgumentException('Incorrect value');
50
        }
51 28
52
        $this->name = $name;
53
        $this->value = $value;
54
    }
55
56
    /**
57 7
     * @param string $specials
58
     * @return HeaderValueParameter
59 7
     */
60
    public function withSpecials(string $specials)
61
    {
62
        $clone = clone $this;
63
        $clone->specials = $specials;
64
        return $clone;
65 28
    }
66
67 28
    /**
68
     * @return string
69
     */
70
    public function getName(): string
71
    {
72
        return $this->name;
73
    }
74 6
75
    /**
76 6
     * @return string
77 6
     */
78 1
    public function getValue(): string
79 1
    {
80
        return $this->value;
81
    }
82
83 5
    /**
84
     * @return string
85 5
     */
86 3
    public function __toString(): string
87
    {
88
        if (strcspn($this->value, $this->specials) !== strlen($this->value)) {
89 5
            return sprintf('%s="%s"', $this->name, $this->value);
90
        }
91
92
        return sprintf('%s=%s', $this->name, $this->value);
93
    }
94
95
    /**
96
     * @param string $parameterString
97
     * @return HeaderValueParameter
98
     */
99
    public static function fromString(string $parameterString): HeaderValueParameter
100
    {
101
        $nameValue = explode('=', $parameterString);
102
        if (count($nameValue) !== 2) {
103
            throw new \InvalidArgumentException(
104
                sprintf('Invalid parameter string value %s', $parameterString)
105
            );
106
        }
107
108
        [$name, $value] = $nameValue;
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
109
110
        if ($value[0] === '"' && $value[-1] === '"') {
0 ignored issues
show
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
111
            $value = substr($value, 1, -1);
0 ignored issues
show
Bug introduced by
The variable $value seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
112
        }
113
114
        return new self($name, $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...
115
    }
116
}