GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ca04f5...09c5f8 )
by Sebastian
69:15 queued 53:11
created

ConcealedEmailCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 4 1
A fill() 0 8 2
A getIterator() 0 4 1
A add() 0 14 3
A addOrUpdateIncrement() 0 13 2
1
<?php
2
3
namespace Spatie\EmailConcealer;
4
5
use ArrayIterator;
6
use IteratorAggregate;
7
8
class ConcealedEmailCollection implements IteratorAggregate
9
{
10
    /** @var string */
11
    private $domain;
12
13
    /** @var array */
14
    private $dictionary = [];
15
16
    public function __construct(string $domain)
17
    {
18
        $this->domain = $domain;
19
    }
20
21
    public static function make(string $domain): self
22
    {
23
        return new self($domain);
24
    }
25
26
    public function fill(array $emails): self
27
    {
28
        foreach ($emails as $email) {
29
            $this->add($email);
30
        }
31
32
        return $this;
33
    }
34
35
    public function getIterator()
36
    {
37
        return new ArrayIterator($this->dictionary);
38
    }
39
40
    private function add(string $email)
41
    {
42
        if (array_key_exists($email, $this->dictionary)) {
43
            return;
44
        }
45
46
        list($localPart) = explode('@', $email);
47
48
        while (in_array($localPart.'@'.$this->domain, $this->dictionary)) {
49
            $localPart = $this->addOrUpdateIncrement($localPart);
50
        }
51
52
        $this->dictionary[$email] = $localPart.'@'.$this->domain;
53
    }
54
55
    private function addOrUpdateIncrement(string $string): string
56
    {
57
        $pattern = '/-(\d+$)/';
58
        $matches = [];
59
60
        if (! preg_match($pattern, $string, $matches)) {
61
            return $string.'-1';
62
        }
63
64
        $increment = $matches[1] + 1;
65
66
        return preg_replace($pattern, "-{$increment}", $string);
67
    }
68
}
69