Completed
Pull Request — master (#64)
by Frederik
03:38 queued 01:09
created

HeaderLine::fromString()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.0163

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
rs 8.9137
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6.0163
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Header;
5
6
use Genkgo\Mail\HeaderInterface;
7
8
final class HeaderLine
9
{
10
    /**
11
     * @var HeaderInterface
12
     */
13
    private $header;
14
15
    /**
16
     * @param HeaderInterface $header
17
     */
18 76
    public function __construct(HeaderInterface $header)
19
    {
20 76
        $this->header = $header;
21 76
    }
22
23
    /**
24
     * @return HeaderInterface
25
     */
26 47
    public function getHeader(): HeaderInterface
27
    {
28 47
        return $this->header;
29
    }
30
31
    /**
32
     * @return string
33
     */
34 62
    public function __toString(): string
35
    {
36 62
        $headerName = (string)$this->header->getName();
37 62
        $headerValue = (string)$this->header->getValue();
38
39 62
        $firstFoldingAt = \strpos($headerValue, "\r\n");
40 62
        if ($firstFoldingAt === false) {
41 62
            $firstFoldingAt = \strlen($headerValue);
42
        }
43
44 62
        if (\strlen($headerName) + $firstFoldingAt > 76) {
45 3
            return \sprintf("%s:\r\n %s", $headerName, $headerValue);
46
        }
47
48 61
        return \sprintf("%s: %s", $headerName, $headerValue);
49
    }
50
51
    /**
52
     * @param string $line
53
     * @return HeaderLine
54
     */
55 49
    public static function fromString(string $line): HeaderLine
56
    {
57 49
        $parts = \preg_split('/\s*\:\s*/', $line, 2);
58
59 49
        if ($parts === false || \count($parts) !== 2) {
60 1
            throw new \InvalidArgumentException('Invalid header line');
61
        }
62
63 48
        [$name, $value] = $parts;
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...
64
65 48
        if (\substr($value, 0, 2) === '=?' && \substr($value, -2, 2) === '?=') {
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...
66 2
            $value = \iconv_mime_decode($value);
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...
67 2
            if ($value === false) {
68
                throw new \UnexpectedValueException('Cannot iconv decode header line');
69
            }
70
        }
71
72 48
        return new self(
73 48
            new ParsedHeader(
74 48
                new HeaderName($name),
75 47
                HeaderValue::fromString($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...
76
            )
77
        );
78
    }
79
}
80