CanonicalizeBodyRelaxed::canonicalize()   C
last analyzed

Complexity

Conditions 11
Paths 12

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 11.1306

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 35
cts 39
cp 0.8974
rs 6.7042
c 0
b 0
f 0
cc 11
nc 12
nop 1
crap 11.1306

How to fix   Long Method    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
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Dkim;
5
6
use Genkgo\Mail\StreamInterface;
7
8
final class CanonicalizeBodyRelaxed implements CanonicalizeBodyInterface
9
{
10
    /**
11
     * @param StreamInterface $body
12
     * @return string
13
     */
14 2
    public function canonicalize(StreamInterface $body): string
15
    {
16 2
        $string = (string)$body;
17 2
        if ($string === '') {
18
            return $string;
19
        }
20
21 2
        $length = \strlen($string);
22 2
        $canon = '';
23 2
        $lastChar = null;
24 2
        $space = false;
25 2
        $line = '';
26 2
        $emptyCounter = 0;
27
28 2
        for ($i = 0; $i < $length; ++$i) {
29 2
            switch ($string[$i]) {
30 2
                case "\r":
31 1
                    $lastChar = "\r";
32 1
                    break;
33
34 2
                case "\n":
35 1
                    if ($lastChar === "\r") {
36 1
                        $space = false;
37
38 1
                        if ($line === '') {
39 1
                            ++$emptyCounter;
40
                        } else {
41 1
                            $line = '';
42 1
                            $canon .= "\r\n";
43
                        }
44
                    } else {
45
                        throw new \RuntimeException('This should never happen');
46
                    }
47
48 1
                    break;
49
50 2
                case ' ':
51 2
                case "\t":
52 2
                    $space = true;
53 2
                    break;
54
55
                default:
56 2
                    if ($emptyCounter > 0) {
57
                        $canon .= \str_repeat("\r\n", $emptyCounter);
58
                        $emptyCounter = 0;
59
                    }
60
61 2
                    if ($space) {
62 2
                        $line .= ' ';
63 2
                        $canon .= ' ';
64 2
                        $space = false;
65
                    }
66
67 2
                    $line .= $string[$i];
68 2
                    $canon .= $string[$i];
69 2
                    break;
70
            }
71
        }
72
73 2
        return \rtrim($canon, "\r\n") . "\r\n";
74
    }
75
76
    /**
77
     * @return string
78
     */
79 3
    public function name(): string
80
    {
81 3
        return 'relaxed';
82
    }
83
}
84