Passed
Push — master ( f112c9...a480d7 )
by Gabriel
02:57
created

HasNameTrait::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Properties\AbstractProperty\Traits;
4
5
use ReflectionClass;
6
7
/**
8
 * Trait HasNameTrait
9
 * @package ByTIC\Models\SmartProperties\Properties\AbstractProperty\Traits
10
 */
11
trait HasNameTrait
12
{
13
    protected $name = null;
14
15
    protected $aliases = [];
16
17
    /**
18 15
     * @return array
19
     */
20 15
    public function getAliases(): array
21 15
    {
22
        return $this->aliases;
23
    }
24 15
25
    /**
26
     * @return string
27 15
     */
28
    public function getName(): string
29 15
    {
30 15
        if ($this->name == null) {
31
            $this->initName();
32
        }
33
34
        return $this->name;
35 15
    }
36
37 15
    /**
38 15
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
39
     */
40 15
    public function setName($name): void
41
    {
42
        $this->name = $name;
43
    }
44
45
    protected function initName()
46 15
    {
47
        $this->setName($this->generateName());
48
    }
49 15
50
    /**
51
     * @return string
52
     */
53
    protected function generateName(): string
54
    {
55
        $name = $this->generateNameFromClass();
56
        $name = inflector()->unclassify($name);
57
58
        return $name;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    protected function generateNameFromClass(): string
65
    {
66
        try {
67
            return (new ReflectionClass($this))->getShortName();
68
        } catch (\ReflectionException $e) {
69
            return '';
70
        }
71
    }
72
}
73