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.

ConcealedEmailCollection   A
last analyzed

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
final 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(iterable $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
        [$localPart] = explode('@', $email);
0 ignored issues
show
Bug introduced by
The variable $localPart seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
47
48
        while (in_array($localPart.'@'.$this->domain, $this->dictionary)) {
0 ignored issues
show
Bug introduced by
The variable $localPart does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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