InterfacesPart   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 131
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
addUseStatement() 0 1 ?
removeUseStatement() 0 1 ?
getNamespace() 0 1 ?
A initInterfaces() 0 3 1
A addInterface() 0 17 3
A getInterfaces() 0 3 1
A hasInterfaces() 0 3 1
A hasInterface() 0 8 4
A removeInterface() 0 14 2
A setInterfaces() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\model\parts;
5
6
use gossi\codegen\model\PhpInterface;
7
use phootwork\collection\Set;
8
9
/**
10
 * Interfaces part
11
 *
12
 * For all models that can contain interfaces
13
 *
14
 * @author Thomas Gossmann
15
 */
16
trait InterfacesPart {
17
18
	/** @var Set */
19
	private $interfaces;
20
21 52
	private function initInterfaces() {
22 52
		$this->interfaces = new Set();
23 52
	}
24
25
	/**
26
	 * Adds a use statement with an optional alias
27
	 *
28
	 * @param string $qualifiedName
29
	 * @param null|string $alias
30
	 * @return $this
31
	 */
32
	abstract public function addUseStatement(string $qualifiedName, string $alias = null);
33
34
	/**
35
	 * Removes a use statement
36
	 *
37
	 * @param string $qualifiedName
38
	 * @return $this
39
	 */
40
	abstract public function removeUseStatement(string $qualifiedName);
41
42
	/**
43
	 * Returns the namespace
44
	 *
45
	 * @return string
46
	 */
47
	abstract public function getNamespace(): ?string;
48
49
	/**
50
	 * Adds an interface.
51
	 *
52
	 * If the interface is passed as PhpInterface object,
53
	 * the interface is also added as use statement.
54
	 *
55
	 * @param PhpInterface|string $interface interface or qualified name
56
	 * @return $this
57
	 */
58 8
	public function addInterface($interface) {
59 8
		if ($interface instanceof PhpInterface) {
60 1
			$name = $interface->getName();
61 1
			$qname = $interface->getQualifiedName();
62 1
			$namespace = $interface->getNamespace();
63
64 1
			if ($namespace != $this->getNamespace()) {
65 1
				$this->addUseStatement($qname);
66
			}
67
		} else {
68 8
			$name = $interface;
69
		}
70
71 8
		$this->interfaces->add($name);
72
73 8
		return $this;
74
	}
75
76
	/**
77
	 * Returns the interfaces
78
	 *
79
	 * @return Set
80
	 */
81 5
	public function getInterfaces(): Set {
82 5
		return $this->interfaces;
83
	}
84
85
	/**
86
	 * Checks whether interfaces exists
87
	 *
88
	 * @return bool `true` if interfaces are available and `false` if not
89
	 */
90 16
	public function hasInterfaces(): bool {
91 16
		return !$this->interfaces->isEmpty();
92
	}
93
94
	/**
95
	 * Checks whether an interface exists
96
	 *
97
	 * @param PhpInterface|string $interface interface name or instance
98
	 * @return bool
99
	 */
100 4
	public function hasInterface($interface): bool {
101 4
		if ($interface instanceof PhpInterface) {
102 1
			return $this->interfaces->contains($interface->getName())
103 1
				|| $this->interfaces->contains($interface->getQualifiedName());
104
		}
105
106 4
		return $this->interfaces->contains($interface) || $this->hasInterface(new PhpInterface($interface));
107
	}
108
109
	/**
110
	 * Removes an interface.
111
	 *
112
	 * If the interface is passed as PhpInterface object,
113
	 * the interface is also remove from the use statements.
114
	 *
115
	 * @param PhpInterface|string $interface interface or qualified name
116
	 * @return $this
117
	 */
118 1
	public function removeInterface($interface) {
119 1
		if ($interface instanceof PhpInterface) {
120 1
			$name = $interface->getName();
121 1
			$qname = $interface->getQualifiedName();
122
123 1
			$this->removeUseStatement($qname);
124
		} else {
125 1
			$name = $interface;
126
		}
127
128 1
		$this->interfaces->remove($name);
129
130 1
		return $this;
131
	}
132
133
	/**
134
	 * Sets a collection of interfaces
135
	 *
136
	 * @param PhpInterface[] $interfaces
137
	 * @return $this
138
	 */
139 1
	public function setInterfaces(array $interfaces) {
140 1
		foreach ($interfaces as $interface) {
141 1
			$this->addInterface($interface);
142
		}
143
144 1
		return $this;
145
	}
146
}
147