QualifiedNamePart::setQualifiedName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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