Completed
Push — master ( c29848...6d2813 )
by Gabriel
02:24
created

NameWorksTrait::setClassNameParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nip\Utility\Traits;
4
5
/**
6
 * Class NameWorksTrait
7
 * @package Nip\Utility\Traits
8
 */
9
trait NameWorksTrait
10
{
11
12
    /**
13
     * @var null|boolean
14
     */
15
    protected $className = null;
16
17
    /**
18
     * @var null|array
19
     */
20
    protected $classNameParts = null;
21
22
    /**
23
     * @var null|boolean
24
     */
25
    protected $classFirstName = null;
26
27
    /**
28
     * @var null|string
29
     */
30
    protected $namespacePath = null;
31
32
    /**
33
     * @var null|boolean
34
     */
35
    protected $isNamespaced = null;
36
37
    /**
38
     * @return string
39
     */
40 3
    public function getClassName()
41
    {
42 3
        if ($this->className === null) {
43 3
            $this->setClassName($this->generateClassName());
0 ignored issues
show
Bug introduced by
$this->generateClassName() of type string is incompatible with the type boolean|null expected by parameter $className of Nip\Utility\Traits\NameWorksTrait::setClassName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            $this->setClassName(/** @scrutinizer ignore-type */ $this->generateClassName());
Loading history...
44
        }
45
46 3
        return $this->className;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->className also could return the type boolean which is incompatible with the documented return type string.
Loading history...
47
    }
48
49
    /**
50
     * @param bool|null $className
51
     */
52 3
    public function setClassName($className)
53
    {
54 3
        $this->className = $className;
55 3
    }
56
57
    /**
58
     * @return string
59
     */
60 3
    protected function generateClassName()
61
    {
62 3
        return get_class($this);
63
    }
64
65
    /**
66
     * @return mixed|null
67
     */
68 1
    public function getNamespaceParentFolder()
69
    {
70 1
        if (!$this->isNamespaced()) {
71
            return null;
72
        }
73 1
        $parts = $this->getClassNameParts();
74 1
        array_pop($parts);
75 1
        return end($parts);
76
    }
77
78
    /**
79
     * @return bool
80
     */
81 2
    public function isNamespaced()
82
    {
83 2
        if ($this->isNamespaced === null) {
84 2
            $class = $this->getClassName();
85
86 2
            $this->isNamespaced = strpos($class, '\\') !== false;
87
        }
88
89 2
        return $this->isNamespaced;
90
    }
91
92
    /**
93
     * @return array|null
94
     */
95 1
    public function getClassNameParts()
96
    {
97 1
        if ($this->classNameParts === null) {
98 1
            $this->initClassNameParts();
99
        }
100 1
        return $this->classNameParts;
101
    }
102
103
    /**
104
     * @param array|null $classNameParts
105
     */
106 1
    public function setClassNameParts($classNameParts)
107
    {
108 1
        $this->classNameParts = $classNameParts;
109 1
    }
110
111 1
    protected function initClassNameParts()
112
    {
113 1
        $class = $this->getClassName();
114 1
        $parts = explode('\\', $class);
115 1
        $this->setClassNameParts($parts);
116 1
    }
117
118
    /**
119
     * @return null|string
120
     */
121
    public function getNamespacePath()
122
    {
123
        if ($this->namespacePath === null) {
124
            $this->initNamespacePath();
125
        }
126
        return $this->namespacePath;
127
    }
128
129
    protected function initNamespacePath()
130
    {
131
        $this->namespacePath = '';
132
        if ($this->isNamespaced()) {
133
            $parts = $this->getClassNameParts();
134
            array_pop($parts);
135
            $this->namespacePath = implode('\\', $parts);
136
        }
137
    }
138
139
    /**
140
     * @return bool|null
141
     */
142
    public function getClassFirstName()
143
    {
144
        if ($this->classFirstName === null) {
145
            $this->initClassFirstName();
146
        }
147
148
        return $this->classFirstName;
149
    }
150
151
    protected function initClassFirstName()
152
    {
153
        $parts = $this->getClassNameParts();
154
        $this->classFirstName = array_pop($parts);
155
    }
156
}
157