Obfuscate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 36
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A filterSingle() 0 20 4
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Filtration\Filter;
4
5
class Obfuscate extends AbstractFilter
6
{
7
    // how may characters from the benining are left untouched
8
    const OPTION_START_CHARACTERS = 'start_characters';
9
    // how may characters from the end are left untouched
10
    const OPTION_END_CHARACTERS = 'end_characters';
11
    // replacement character
12
    const OPTION_REPLACEMENT_CHAR = 'replacement_char';
13
14
    protected $options = [
15
        self::OPTION_START_CHARACTERS => 0,
16
        self::OPTION_END_CHARACTERS => 0,
17
        self::OPTION_REPLACEMENT_CHAR => '*'
18
    ];
19
20 7
    public function filterSingle($value, string $valueIdentifier = null)
21
    {
22
        // not a string, move along
23 7
        if (! is_string($value)) {
24 1
            return $value;
25
        }
26
        
27 6
        $len = strlen($value);
28 6
        $start = $this->options[self::OPTION_START_CHARACTERS] ?
29 3
            substr($value, 0, $this->options[self::OPTION_START_CHARACTERS]) :
30 6
            '';
31 6
        $end = $this->options[self::OPTION_END_CHARACTERS] ?
32 3
            substr($value, $len - $this->options[self::OPTION_END_CHARACTERS]) :
33 6
            '';
34 6
        $middle = str_repeat(
35 6
            $this->options[self::OPTION_REPLACEMENT_CHAR],
36 6
            $len - $this->options[self::OPTION_START_CHARACTERS] - $this->options[self::OPTION_END_CHARACTERS]
37
        );
38 6
        return $start . $middle . $end;
39
    }
40
}
41