| Conditions | 3 |
| Paths | 3 |
| Total Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 13 |
| CRAP Score | 3 |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | 5 | public static function mask($email) |
|
| 16 | { |
||
| 17 | 5 | if (strpos($email, '@') === false) { |
|
| 18 | 1 | return Str::mask($email, 1); |
|
| 19 | } |
||
| 20 | |||
| 21 | 4 | list ($username, $domain) = explode("@", $email); |
|
| 22 | |||
| 23 | 4 | $parts[] = Str::mask($username, 1); |
|
|
|
|||
| 24 | 4 | $parts[] = '@'; |
|
| 25 | |||
| 26 | 4 | $domainParts = explode('.', $domain); |
|
| 27 | 4 | $lastPart = array_pop($domainParts); |
|
| 28 | 4 | foreach ($domainParts as $part) { |
|
| 29 | 3 | $parts[] = Str::mask($part, 1); |
|
| 30 | 3 | $parts[] = '.'; |
|
| 31 | } |
||
| 32 | 4 | $parts[] = $lastPart; |
|
| 33 | 4 | return implode('', $parts); |
|
| 34 | } |
||
| 35 | } |
||
| 36 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.