Issues (1)

src/Services/Traits/Initials.php (1 issue)

Severity
1
<?php
2
3
namespace CelsoNery\Initials\Services\Traits;
4
5
use function PHPUnit\Framework\isEmpty;
6
7
trait Initials
8
{
9
    public function getInitials(string $firstName, string $lastName = ''): string
10
    {
11
        if (empty($firstName)) {
12
            return "The name is necessary";
13
        }
14
15
        $firstName = trim($firstName);
16
        $lastName = trim($lastName);
0 ignored issues
show
The assignment to $lastName is dead and can be removed.
Loading history...
17
18
        if (mb_strpos($firstName, ' ')) {
19
            $nameArr = explode(' ', $firstName);
20
            return $this->getInitialsFullName($nameArr[0], $nameArr[1]);
21
        }
22
23
        return $this->getInitialsFirstName($firstName);
24
    }
25
26
    private function getInitialsFirstName(string $firstName): string
27
    {
28
        $initial = mb_substr($firstName, 0, 1);
29
        return mb_strtoupper($initial);
30
    }
31
32
    private function getInitialsFullName(string $firstName, string $lastName): string
33
    {
34
        $initials = mb_substr($firstName, 0, 1) . mb_substr($lastName, 0, 1);
35
        return mb_strtoupper($initials);
36
    }
37
}
38