Completed
Pull Request — master (#18)
by Frederik
01:29
created

RelaxedCanonicalizeBody::canonicalize()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 60
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
ccs 0
cts 50
cp 0
rs 6.2926
cc 11
eloc 41
nc 21
nop 1
crap 132

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\Header\Dkim;
5
6
final class RelaxedCanonicalizeBody implements CanonicalizeBodyInterface
0 ignored issues
show
Bug introduced by
There is one abstract method getName in this class; you could implement it, or declare this class as abstract.
Loading history...
7
{
8
9
    /**
10
     * @param string $string
11
     * @return string
12
     */
13
    public function canonicalize(string $string): string
14
    {
15
        $length = strlen($string);
16
        $canon = '';
17
        $lastChar = null;
18
        $space = false;
19
        $line = '';
20
        $emptyCounter = 0;
21
22
        for ($i = 0; $i < $length; ++$i) {
23
            switch ($string[$i]) {
24
                case "\r":
25
                    $lastChar = "\r";
26
                    break;
27
28
                case "\n":
29
                    if ($lastChar === "\r") {
30
                        $space = false;
31
32
                        if ($line === '') {
33
                            ++$emptyCounter;
34
                        } else {
35
                            $line = '';
36
                            $canon .= "\r\n";
37
                        }
38
                    } else {
39
                        throw new \RuntimeException('This should never happen');
40
                    }
41
42
                    break;
43
44
                case ' ':
45
                case "\t":
46
                    $space = true;
47
                    break;
48
49
                default:
50
                    if ($emptyCounter > 0) {
51
                        $canon .= str_repeat("\r\n", $emptyCounter);
52
                        $emptyCounter = 0;
53
                    }
54
55
                    if ($space) {
56
                        $line .= ' ';
57
                        $canon .= ' ';
58
                        $space = false;
59
                    }
60
61
                    $line .= $string[$i];
62
                    $canon .= $string[$i];
63
                    break;
64
            }
65
        }
66
67
        if ($line === '') {
68
            $canon .= "\r\n";
69
        }
70
71
        return $canon;
72
    }
73
}