QuotedLiteralPart::filterIgnoredSpaces()   B
last analyzed

Complexity

Conditions 10
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.0203

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 20
ccs 16
cts 17
cp 0.9412
rs 7.6666
cc 10
nc 1
nop 1
crap 10.0203

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
8
namespace ZBateson\MailMimeParser\Header\Part;
9
10
/**
11
 * A quoted literal header string part.  The value of the part is stripped of CR
12
 * and LF characters, and whitespace between two adjacent MimeTokens is removed.
13
 *
14
 * @author Zaahid Bateson
15
 */
16
class QuotedLiteralPart extends ContainerPart
17
{
18
    /**
19
     * Strips spaces found between two adjacent MimeToken parts.
20
     * Other whitespace is returned as-is.
21
     *
22
     * @param HeaderPart[] $parts
23
     * @return HeaderPart[]
24
     */
25 102
    protected function filterIgnoredSpaces(array $parts) : array
26
    {
27 102
        $filtered = \array_reduce(
28 102
            \array_keys($parts),
29 102
            function($carry, $key) use ($parts) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
30 102
                $cur = $parts[$key];
31 102
                $last = ($carry !== null) ? \end($carry) : null;
32 102
                $next = (count($parts) > $key + 1) ? $parts[$key + 1] : null;
33 102
                if ($last !== null && $next !== null && $cur->isSpace && (
34 102
                    $last->canIgnoreSpacesAfter
35 102
                    && $next->canIgnoreSpacesBefore
36 102
                    && $last instanceof MimeToken
37 102
                    && $next instanceof MimeToken
38
                )) {
39
                    return $carry;
40
                }
41 102
                return \array_merge($carry ?? [], [$cur]);
42 102
            }
43 102
        );
44 102
        return $filtered;
45
    }
46
}
47