Completed
Pull Request — master (#67)
by
unknown
02:38
created

QualifiedNamePart::getNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\model\parts;
5
6
use gossi\codegen\utils\TypeUtils;
7
8
/**
9
 * Qualified name part
10
 *
11
 * For all models that have a name and namespace
12
 *
13
 * @author Thomas Gossmann
14
 */
15
trait QualifiedNamePart {
16
17
	use NamePart;
18
19
	/** @var string */
20
	private $namespace;
21
22
	/**
23
	 * Sets the namespace
24
	 *
25
	 * @param string $namespace
26
	 * @return $this
27
	 */
28 11
	public function setNamespace(?string $namespace) {
29 11
		$this->namespace = $namespace;
30
31 11
		return $this;
32
	}
33
34
	/**
35
	 * In contrast to setName(), this method accepts the fully qualified name
36
	 * including the namespace.
37
	 *
38
	 * @param string $name
39
	 * @return $this
40
	 */
41 76
	public function setQualifiedName(?string $name) {
42 76
		if ($name === null) {
43 46
			return $this;
44
		}
45
46 44
		if (!TypeUtils::isGlobalQualifiedName($name) && false !== $pos = strrpos($name, '\\')) {
47 8
			$this->namespace = trim(substr($name, 0, $pos), '\\');
48 8
			$this->name = substr($name, $pos + 1);
49
50 8
			return $this;
51
		}
52
53 41
		$this->namespace = null;
54 41
		$this->name = $name;
55
56 41
		return $this;
57
	}
58
59
	/**
60
	 * Returns the namespace
61
	 *
62
	 * @return string
63
	 */
64 24
	public function getNamespace(): ?string {
65 24
		return $this->namespace;
66
	}
67
68
	/**
69
	 * Returns the qualified name
70
	 *
71
	 * @return string
72
	 */
73 26
	public function getQualifiedName(): string {
74 26
		if ($this->namespace) {
75 5
			return $this->namespace . '\\' . $this->name;
76
		}
77
78 23
		return $this->name;
79
	}
80
81 22
    public function __toString(): string
82
    {
83 22
        return $this->getQualifiedName();
84
    }
85
}
86