Completed
Push — master ( 10a6dd...fc4128 )
by Markus
02:21
created

ComponentUtil   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 49
ccs 16
cts 16
cp 1
rs 10
c 4
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B fold() 0 34 6
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Util;
13
14
class ComponentUtil
15
{
16
    /**
17
     * Folds a single line.
18
     *
19
     * According to RFC 5545, all lines longer than 75 characters should be folded
20
     *
21
     * @see https://tools.ietf.org/html/rfc5545#section-5
22
     * @see https://tools.ietf.org/html/rfc5545#section-3.1
23
     *
24
     * @param string $string
25
     *
26
     * @return array
27 4
     */
28
    public static function fold($string)
29 4
    {
30 4
        $lines = array();
31
32 4
        if (function_exists('mb_strcut')) {
33 4
            while (strlen($string) > 0) {
34 4
                if (strlen($string) > 75) {
35 4
                    $lines[] = mb_strcut($string, 0, 75, 'utf-8');
36 4
                    $string = ' ' . mb_strcut($string, 75, strlen($string), 'utf-8');
37 4
                } else {
38 1
                    $lines[] = $string;
39 1
                    $string = '';
0 ignored issues
show
Unused Code introduced by
$string is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40 1
                    break;
41 4
                }
42
            }
43 4
        } else {
44 4
            $array = preg_split('/(?<!^)(?!$)/u', $string);
45
            $line = '';
46 4
            $lineNo = 0;
47
            foreach ($array as $char) {
48
                $charLen = strlen($char);
49
                $lineLen = strlen($line);
50
                if ($lineLen + $charLen > 75) {
51
                    $line = ' ' . $char;
52
                    ++$lineNo;
53
                } else {
54
                    $line .= $char;
55
                }
56
                $lines[$lineNo] = $line;
57
            }
58
        }
59
60
        return $lines;
61
    }
62
}
63