Passed
Push — master ( a651e4...4f7192 )
by Gabriel
11:34
created

HasNameTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 23
c 1
b 0
f 0
dl 0
loc 82
ccs 13
cts 15
cp 0.8667
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A generateNameFromClass() 0 11 4
A getNamespace() 0 3 1
A initName() 0 3 1
A getName() 0 7 2
A setName() 0 3 1
A setNamespace() 0 3 1
A getAliases() 0 3 1
A generateName() 0 6 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 $namespace = null;
16
17
    protected $aliases = [];
18 15
19
    /**
20 15
     * @return array
21 15
     */
22
    public function getAliases(): array
23
    {
24 15
        return $this->aliases;
25
    }
26
27 15
    /**
28
     * @return string
29 15
     */
30 15
    public function getName(): string
31
    {
32
        if ($this->name == null) {
33
            $this->initName();
34
        }
35 15
36
        return $this->name;
37 15
    }
38 15
39
    /**
40 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...
41
     */
42
    public function setName($name): void
43
    {
44
        $this->name = $name;
45
    }
46 15
47
    /**
48
     * @return null
49 15
     */
50
    public function getNamespace()
51
    {
52
        return $this->namespace;
53
    }
54
55
    /**
56
     * @param null $namespace
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $namespace is correct as it would always require null to be passed?
Loading history...
57
     */
58
    public function setNamespace($namespace): void
59
    {
60
        $this->namespace = $namespace;
61
    }
62
63
    protected function initName()
64
    {
65
        $this->setName($this->generateName());
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    protected function generateName(): string
72
    {
73
        $name = $this->generateNameFromClass();
74
        $name = inflector()->unclassify($name);
75
76
        return $name;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    protected function generateNameFromClass(): string
83
    {
84
        $class = static::class;
85
        $base = $this->getNamespace();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $base is correct as $this->getNamespace() targeting ByTIC\Models\SmartProper...meTrait::getNamespace() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
86
        if ($base && strpos($class, $base) ===0) {
0 ignored issues
show
introduced by
$base is of type null, thus it always evaluated to false.
Loading history...
87
            return str_replace($base, '', $class);
88
        }
89
        try {
90
            return (new ReflectionClass($this))->getShortName();
91
        } catch (\ReflectionException $e) {
92
            return '';
93
        }
94
    }
95
}
96