Truncate::filterSingle()   B
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 20
cts 20
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 14
nop 2
crap 10

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
declare(strict_types=1);
3
namespace Sirius\Filtration\Filter;
4
5
class Truncate extends AbstractFilter
6
{
7
    const OPTION_LIMIT = 'limit';
8
9
    const OPTION_ELLIPSIS = 'ellipsis';
10
11
    const OPTION_BREAK_WORDS = 'break_words';
12
13
    protected $options = [
14
        self::OPTION_LIMIT => false,
15
        self::OPTION_BREAK_WORDS => true,
16
        self::OPTION_ELLIPSIS => '...'
17
    ];
18
19 9
    public function filterSingle($value, string $valueIdentifier = null)
20
    {
21
        // not a string, move along
22 9
        if (! is_string($value)) {
23 1
            return $value;
24
        }
25
26 8
        if (! $this->options[self::OPTION_LIMIT] || strlen($value) <= $this->options[self::OPTION_LIMIT]) {
27 2
            return $value;
28
        }
29
30 6
        $limit = (int) $this->options[self::OPTION_LIMIT];
31 6
        $firstSpace = strpos($value, ' ');
32
33
        // in case word breaking is not allowed find the previous space
34 6
        if (!$this->options[self::OPTION_BREAK_WORDS]) {
35 3
            if ($firstSpace === false) {
36 1
                $limit = strlen($value);
37
            } else {
38 2
                $limit = max($firstSpace, min($limit, (int)strrpos(substr($value, 0, $limit), ' ')));
39
            }
40
        }
41
42 6
        $isWordBreaker = false;
0 ignored issues
show
Unused Code introduced by
$isWordBreaker 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...
43 6
        if (substr($value, $limit, 1) != ' '
44 6
            && substr($value, $limit + 1, 1) != ' ') {
45 2
            $isWordBreaker = true;
0 ignored issues
show
Unused Code introduced by
$isWordBreaker 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...
46
        }
47
48 6
        $truncated = rtrim(substr($value, 0, $limit));
49
50 6
        if ($this->options[self::OPTION_ELLIPSIS]
51 6
            && $truncated != $value) {
52 5
            $truncated .= $this->options[self::OPTION_ELLIPSIS];
53
        }
54
55 6
        return $truncated;
56
    }
57
}
58