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::addOrUpdateIncrement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
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