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

TraitsPart   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 124
ccs 33
cts 33
cp 1
rs 10
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 gossi\codegen\model\PhpTrait;
13
use phootwork\collection\Set;
14
15
/**
16
 * Traits part
17
 *
18
 * For all models that can have traits
19
 *
20
 * @author Thomas Gossmann
21
 */
22
trait TraitsPart {
23
24
	/** @var Set */
25
	private Set $traits;
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
27
	/**
28
	 * Adds a use statement with an optional alias
29
	 *
30
	 * @param string $qualifiedName
31
	 * @param string $alias
32
	 *
33
	 * @return $this
34
	 */
35
	abstract public function addUseStatement(string $qualifiedName, string $alias = '');
36
37
	/**
38
	 * Removes a use statement
39
	 *
40
	 * @param string $qualifiedName
41
	 *
42
	 * @return $this
43
	 */
44
	abstract public function removeUseStatement(string $qualifiedName);
45
46
	/**
47
	 * Returns the namespace
48
	 *
49
	 * @return string
50
	 */
51
	abstract public function getNamespace(): string;
52
53 4
	protected function initTraits(): void {
54 4
		$this->traits = new Set();
55 1
	}
56 1
57 1
	/**
58
	 * Adds a trait.
59 1
	 *
60 1
	 * If the trait is passed as PhpTrait object,
61
	 * the trait is also added as use statement.
62
	 *
63 4
	 * @param PhpTrait|string $trait trait or qualified name
64
	 *
65
	 * @return $this
66 4
	 */
67 4
	public function addTrait(PhpTrait|string $trait): self {
68
		if ($trait instanceof PhpTrait) {
69
			$name = $trait->getName();
70 4
			$qname = $trait->getQualifiedName();
71
			$namespace = $trait->getNamespace();
72
73
			if ($namespace != $this->getNamespace()) {
74
				$this->addUseStatement($qname);
75
			}
76
		} else {
77
			$name = $trait;
78 15
		}
79 15
80
		$this->traits->add($name);
81
82
		return $this;
83
	}
84
85
	/**
86
	 * Returns a collection of traits
87
	 *
88 2
	 * @return Set
89 2
	 */
90 2
	public function getTraits(): Set {
91
		return $this->traits;
92 2
	}
93 2
94
	/**
95
	 * Checks whether a trait exists
96
	 *
97
	 * @param PhpTrait|string $trait
98
	 *
99
	 * @return bool `true` if it exists and `false` if not
100
	 */
101
	public function hasTrait(PhpTrait|string $trait): bool {
102
		if ($trait instanceof PhpTrait) {
103
			return $this->traits->contains($trait->getName())
104
				|| $this->traits->contains($trait->getQualifiedName());
105 1
		}
106 1
107 1
		return $this->traits->contains($trait) || $this->hasTrait(new PhpTrait($trait));
108
	}
109 1
110
	/**
111
	 * Removes a trait.
112 1
	 *
113 1
	 * If the trait is passed as PhpTrait object,
114 1
	 * the trait is also removed from use statements.
115
	 *
116 1
	 * @param PhpTrait|string $trait trait or qualified name
117 1
	 *
118 1
	 * @return $this
119
	 */
120
	public function removeTrait(PhpTrait|string $trait): self {
121
		if ($trait instanceof PhpTrait) {
122 1
			$name = $trait->getName();
123
			$qname = $trait->getQualifiedName();
124
125
			$this->removeUseStatement($qname);
126
		} else {
127
			$name = $trait;
128
		}
129
130
		$this->traits->remove($name);
131 1
132 1
		return $this;
133 1
	}
134
135
	/**
136 1
	 * Sets a collection of traits
137
	 *
138
	 * @param PhpTrait[]|string[] $traits
139
	 *
140
	 * @return $this
141
	 */
142
	public function setTraits(array $traits): self {
143
		array_map([$this, 'addTrait'], $traits);
144
145
		return $this;
146
	}
147
}
148