Completed
Pull Request — master (#68)
by Cristiano
05:36
created

QualifiedNamePart   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the php-code-generator package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license Apache-2.0
8
 */
9
10
namespace gossi\codegen\model\parts;
11
12
use phootwork\lang\Text;
13
14
/**
15
 * Qualified name part
16
 *
17
 * For all models that have a name and namespace
18
 *
19
 * @author Thomas Gossmann
20
 */
21
trait QualifiedNamePart {
22
	use NamePart;
23
24
	/** @var string */
25
	private string $namespace = '';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26 10
27 10
	/**
28
	 * Sets the namespace
29 10
	 *
30
	 * @param string $namespace
31
	 *
32
	 * @return $this
33
	 */
34
	public function setNamespace(string $namespace = ''): self {
35
		$this->namespace = $namespace;
36
37
		return $this;
38
	}
39 65
40 65
	/**
41 45
	 * In contrast to setName(), this method accepts the fully qualified name
42
	 * including the namespace.
43
	 *
44 26
	 * @param string $fullName
45 6
	 *
46 6
	 * @return $this
47
	 */
48 6
	public function setQualifiedName(string $fullName = ''): self {
49
		$fullName = new Text($fullName);
50
51 22
		if ($fullName->isEmpty()) {
52 22
			return $this;
53
		}
54 22
55
		$this->name = $fullName->substring((int) $fullName->lastIndexOf('\\'))->trimStart('\\')->toString();
56
		$this->namespace = $fullName->trimEnd($this->name)->trim('\\')->toString();
57
58
		return $this;
59
	}
60
61
	/**
62 19
	 * Returns the namespace
63 19
	 *
64
	 * @return string
65
	 */
66
	public function getNamespace(): string {
67
		return $this->namespace;
68
	}
69
70
	/**
71 3
	 * Returns the qualified name
72 3
	 *
73 3
	 * @return string
74
	 */
75
	public function getQualifiedName(): string {
76 1
		if ($this->namespace) {
77
			return $this->namespace . '\\' . $this->name;
78
		}
79
80
		return $this->name;
81
	}
82
}
83