Test Failed
Pull Request — master (#6)
by mon
12:48
created

Parser   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 95
Duplicated Lines 29.47 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
lcom 0
cbo 1
dl 28
loc 95
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D parseStringPart() 28 76 24

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace FileEye\MimeMap;
4
5
/**
6
 * Class for parsing RFC 2045 Content-Type Header Fields.
7
 */
8
class Parser
9
{
10
    /**
11
     * Parses a part of the content MIME type string.
12
     *
13
     * Splits string and comment until a delimiter is found.
14
     *
15
     * @param string $string     Input string.
16
     * @param int $offset        Offset to start parsing from.
17
     * @param string $delimiter  Stop parsing when delimiter found.
18
     *
19
     * @return array An array with the following keys:
20
     *   'string' - the uncommented part of $string
21
     *   'comment' - the comment part of $string
22
     *   'delimiter_matched' - true if a $delimiter stopped the parsing, false
23
     *                         otherwise
24
     *   'end_offset' - the last position parsed in $string.
25
     */
26 50
    public static function parseStringPart($string, $offset, $delimiter)
27
    {
28 50
        $inquote   = false;
29 50
        $escaped   = false;
30 50
        $incomment = 0;
31 50
        $newstring = '';
32 50
        $comment = '';
33
34 50
        for ($n = $offset; $n < strlen($string); $n++) {
35 48
            if ($string[$n] === $delimiter && !$escaped && !$inquote && $incomment === 0) {
36 46
                break;
37
            }
38
39 47 View Code Duplication
            if ($escaped) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40 4
                if ($incomment == 0) {
41 2
                    $newstring .= $string[$n];
42
                } else {
43 2
                    $comment .= $string[$n];
44
                }
45 4
                $escaped = false;
46 4
                continue;
47
            }
48
49 47 View Code Duplication
            if ($string[$n] == '\\') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 4
                if ($incomment > 0) {
51 2
                    $comment .= $string[$n];
52
                }
53 4
                $escaped = true;
54 4
                continue;
55
            }
56
57 47
            if (!$inquote && $incomment > 0 && $string[$n] == ')') {
58 20
                $incomment--;
59 20
                if ($incomment == 0) {
60 18
                    $comment .= ' ';
61
                }
62 20
                continue;
63
            }
64
65 47
            if (!$inquote && $string[$n] == '(') {
66 20
                $incomment++;
67 20
                continue;
68
            }
69
70 47 View Code Duplication
            if ($string[$n] == '"') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71 9
                if ($incomment > 0) {
72 1
                    $comment .= $string[$n];
73
                } else {
74 8
                    if ($inquote) {
75 8
                        $inquote = false;
76
                    } else {
77 8
                        $inquote = true;
78
                    }
79
                }
80 9
                continue;
81
            }
82
83 47
            if ($incomment == 0) {
84 47
                $newstring .= $string[$n];
85 47
                continue;
86
            }
87
88 20
            $comment .= $string[$n];
89
        }
90
91 50
        if ($incomment > 0) {
92 2
            throw new MalformedTypeException('Comment closing bracket missing: ' . $comment);
93
        }
94
95
        return [
96 49
          'string' => empty($newstring) ? null : trim($newstring),
97 49
          'comment' => empty($comment) ? null : trim($comment),
98 49
          'delimiter_matched' => isset($string[$n]) ? ($string[$n] === $delimiter) : false,
99 49
          'end_offset' => $n,
100
        ];
101
    }
102
}
103