Issues (16)

src/VObjects/Person/Name.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DBUnt1tled\VO\VObjects\Person;
6
7
use DBUnt1tled\VO\Exception\InvalidVOArgumentException;
8
use DBUnt1tled\VO\VObjects\Scalar\Strings;
9
use DBUnt1tled\VO\VObjects\ValueObjectComplex;
10
use DBUnt1tled\VO\VObjects\ValueObjectInterface;
11
12
class Name extends ValueObjectComplex
13
{
14
    public const NAME_MIN_LENGTH = 4;
15
    /** @var Strings */
16
    private $firstName;
17
    /** @var Strings */
18
    private $lastName;
19
20
    /**
21
     * Name constructor.
22
     * @param Strings $firstName
23
     * @param Strings $lastName
24
     * @throws \ReflectionException
25
     */
26 4
    public function __construct(Strings $firstName, Strings $lastName)
27
    {
28 4
        $this->guard($firstName, $lastName);
29 3
        $this->firstName = $firstName;
0 ignored issues
show
Documentation Bug introduced by
$firstName is of type DBUnt1tled\VO\VObjects\ValueObjectInterface, but the property $firstName was declared to be of type DBUnt1tled\VO\VObjects\Scalar\Strings. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
30 3
        $this->lastName = $lastName;
31 3
    }
32
33
    /**
34
     * @param mixed $value
35
     * @param mixed ...$other
36
     * @throws \ReflectionException
37
     */
38 4
    public function guard($value, ...$other): void
39
    {
40 4
        parent::guard($value);
41
        /** @var ValueObjectInterface $value*/
42 4
        parent::guard($other[0]);
43
        /** @var ValueObjectInterface[] $other*/
44 4
        if (mb_strlen($value->getValue()) < self::NAME_MIN_LENGTH) {
45 2
            throw new InvalidVOArgumentException(sprintf('First name must contain a minimum of %s symbols.', self::NAME_MIN_LENGTH), $value);
46
        }
47 4
        if (mb_strlen($other[0]->getValue()) < self::NAME_MIN_LENGTH) {
48 2
            throw new InvalidVOArgumentException(sprintf('Last name must contain a minimum of %s symbols.', self::NAME_MIN_LENGTH), $value);
49
        }
50 3
    }
51
52
    /**
53
     * @param string $firstName
54
     * @param string $lastName
55
     * @return Name
56
     * @throws \ReflectionException
57
     */
58 2
    public static function createFromString(string $firstName, string $lastName)
59
    {
60 2
        return new static(new Strings($firstName), new Strings($lastName));
61
    }
62
63
    /**
64
     * @return string
65
     */
66 2
    public function getFirstName(): string
67
    {
68 2
        return $this->firstName->getValue();
69
    }
70
71
    /**
72
     * @return string
73
     */
74 2
    public function getLastName(): string
75
    {
76 2
        return $this->lastName->getValue();
77
    }
78
79
    /**
80
     * @return string
81
     */
82 1
    public function getFullName(): string
83
    {
84 1
        return $this->firstName->getValue().' '.$this->lastName->getValue();
85
    }
86
87
    /**
88
     * @return string
89
     */
90 2
    public function __toString(): string
91
    {
92 2
        return $this->getFullName();
93
    }
94
95
    /**
96
     * @return string
97
     */
98 2
    public function getValue(): string
99
    {
100 2
        return $this->getFullName();
101
    }
102
}
103