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 ( caac67...e703d4 )
by Christian
06:26
created

StringTwigExtension::encryptMailText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\AntiSpamBundle\Twig\Extension;
13
14
use Twig\Extension\AbstractExtension;
15
use Twig\TwigFilter;
16
17
final class StringTwigExtension extends AbstractExtension
18
{
19
    private const MAIL_HTML_PATTERN = '/\<a(?:[^>]+)href\=\"mailto\:([^">]+)\"(?:[^>]*)\>(.*?)\<\/a\>/ism';
20
    private const MAIL_TEXT_PATTERN = '/(([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})(\((.+?)\))?)/i';
21
22
    /**
23
     * @var string
24
     */
25
    private $mailCssClass;
26
27
    /**
28
     * @var string[]
29
     */
30
    private $mailAtText;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $mailDotText;
36
37
    /**
38
     * StringTwigExtension constructor.
39
     *
40
     * @param string   $mailCssClass
41
     * @param string[] $mailAtText
42
     * @param string[] $mailDotText
43
     */
44
    public function __construct(string $mailCssClass, array $mailAtText, array $mailDotText)
45
    {
46
        $this->mailCssClass = $mailCssClass;
47
        $this->mailAtText   = $mailAtText;
48
        $this->mailDotText  = $mailDotText;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getFilters()
55
    {
56
        return [
57
            new TwigFilter('antispam', [$this, 'antispam'], [
58
                'is_safe' => ['html'],
59
            ]),
60
        ];
61
    }
62
63
    /**
64
     * Replaces E-Mail addresses with an alternative text representation.
65
     *
66
     * @param string $string input string
67
     * @param bool   $html   Secure html or text
68
     *
69
     * @return string with replaced links
70
     */
71
    public function antispam(string $string, bool $html = true): string
72
    {
73
        if ($html) {
74
            return (string) preg_replace_callback(self::MAIL_HTML_PATTERN, [$this, 'encryptMail'], $string);
75
        }
76
77
        return (string) preg_replace_callback(self::MAIL_TEXT_PATTERN, [$this, 'encryptMailText'], $string);
78
    }
79
80
    /**
81
     * @param string[] $matches
82
     *
83
     * @return string
84
     */
85
    private function encryptMailText(array $matches): string
86
    {
87
        $email = $matches[1];
88
89
        return $this->getSecuredName($email).
90
            $this->mailAtText[array_rand($this->mailAtText)].
91
            $this->getSecuredName($email, true);
92
    }
93
94
    /**
95
     * @param string[] $matches
96
     *
97
     * @return string
98
     */
99
    private function encryptMail(array $matches): string
100
    {
101
        [, $email, $text] = $matches;
0 ignored issues
show
Bug introduced by
The variable $email does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $text 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...
102
103
        if ($text === $email) {
0 ignored issues
show
Bug introduced by
The variable $text 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...
104
            $text = '';
105
        }
106
107
        return
108
            '<span class="'.$this->mailCssClass.'">'.
109
            '<span>'.$this->getSecuredName($email).'</span>'.
110
                $this->mailAtText[array_rand($this->mailAtText)].
111
            '<span>'.$this->getSecuredName($email, true).'</span>'.
112
            ($text ? ' (<span>'.$text.'</span>)' : '').
0 ignored issues
show
Bug introduced by
The variable $text 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...
113
            '</span>';
114
    }
115
116
    /**
117
     * @param string $name
118
     * @param bool   $isDomain
119
     *
120
     * @return string
121
     */
122
    private function getSecuredName(string $name, bool $isDomain = false): string
123
    {
124
        $index = strpos($name, '@');
125
126
        if ($index === -1) {
127
            return '';
128
        }
129
130
        if ($isDomain) {
131
            $name = (string) substr($name, $index + 1);
132
        } else {
133
            $name = (string) substr($name, 0, $index);
134
        }
135
136
        return str_replace('.', $this->mailDotText[array_rand($this->mailDotText)], $name);
137
    }
138
}
139