Completed
Push — master ( ce62ef...a1e916 )
by Thomas
02:37
created

TraitsPart::removeTrait()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

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