TraitsPart::getTraits()   A
last analyzed

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