Passed
Push — master ( 3949e5...920751 )
by Maurício
08:08
created

TriggerName::tryFromValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Triggers;
6
7
use Stringable;
8
use Webmozart\Assert\Assert;
9
use Webmozart\Assert\InvalidArgumentException;
10
11
final class TriggerName implements Stringable
12
{
13
    /**
14
     * @see https://dev.mysql.com/doc/refman/en/identifier-length.html
15
     * @see https://mariadb.com/kb/en/identifier-names/#maximum-length
16
     */
17
    private const MAX_LENGTH = 64;
18
19
    /** @psalm-var non-empty-string */
20
    private string $name;
21
22
    /** @throws InvalidTriggerName */
23 48
    private function __construct(mixed $name)
24
    {
25
        try {
26 48
            Assert::stringNotEmpty($name);
27 16
        } catch (InvalidArgumentException) {
28 16
            throw InvalidTriggerName::fromEmptyName();
29
        }
30
31
        try {
32 32
            Assert::maxLength($name, self::MAX_LENGTH);
33 4
        } catch (InvalidArgumentException) {
34 4
            throw InvalidTriggerName::fromLongName(self::MAX_LENGTH);
35
        }
36
37
        try {
38 28
            Assert::notEndsWith($name, ' ');
39 4
        } catch (InvalidArgumentException) {
40 4
            throw InvalidTriggerName::fromNameWithTrailingSpace();
41
        }
42
43 24
        $this->name = $name;
44
    }
45
46
    /** @throws InvalidTriggerName */
47 36
    public static function fromValue(mixed $name): self
48
    {
49 36
        return new self($name);
50
    }
51
52 36
    public static function tryFromValue(mixed $name): self|null
53
    {
54
        try {
55 36
            return new self($name);
56 24
        } catch (InvalidTriggerName) {
57 24
            return null;
58
        }
59
    }
60
61
    /** @psalm-return non-empty-string */
62 24
    public function getName(): string
63
    {
64 24
        return $this->name;
65
    }
66
67
    /** @psalm-return non-empty-string */
68 24
    public function __toString(): string
69
    {
70 24
        return $this->name;
71
    }
72
}
73