Initials   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 2
b 0
f 0
dl 0
loc 29
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInitials() 0 15 3
A getInitialsFullName() 0 4 1
A getInitialsFirstName() 0 4 1
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
Unused Code introduced by
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