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

InterfacesPart   A

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