AbstractPhpStruct   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 328
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 37
lcom 3
cbo 8
dl 0
loc 328
ccs 96
cts 96
cp 1
rs 9.44
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 7 1
A setRequiredFiles() 0 5 1
A addRequiredFile() 0 5 1
A getRequiredFiles() 0 3 1
A setUseStatements() 0 8 2
A addUseStatement() 0 13 3
A clearUseStatements() 0 5 1
A declareUses() 0 6 2
A declareUse() 0 7 2
A hasUseStatement() 0 3 1
A getUseAlias() 0 3 1
A removeUseStatement() 0 4 1
A getUseStatements() 0 3 1
A setMethods() 0 12 3
A setMethod() 0 6 1
A removeMethod() 0 14 3
A hasMethod() 0 7 2
A getMethod() 0 11 3
A getMethods() 0 3 1
A getMethodNames() 0 3 1
A clearMethods() 0 8 2
A generateDocblock() 0 11 2
1
<?php
2
/*
3
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
declare(strict_types=1);
18
19
namespace gossi\codegen\model;
20
21
use gossi\codegen\model\parts\DocblockPart;
22
use gossi\codegen\model\parts\LongDescriptionPart;
23
use gossi\codegen\model\parts\QualifiedNamePart;
24
use gossi\docblock\Docblock;
25
use phootwork\collection\Map;
26
use phootwork\collection\Set;
27
28
/**
29
 * Represents an abstract php structure (class, trait or interface).
30
 *
31
 * @author Johannes M. Schmitt <[email protected]>
32
 * @author Thomas Gossmann
33
 */
34
abstract class AbstractPhpStruct extends AbstractModel implements NamespaceInterface, DocblockInterface {
35
36
	use DocblockPart;
37
	use LongDescriptionPart;
38
	use QualifiedNamePart;
39
40
	/** @var Map */
41
	private $useStatements;
42
43
	/** @var Set */
44
	private $requiredFiles;
45
46
	/** @var Map */
47
	private $methods;
48
49
	/**
50
	 * Creates a new struct
51
	 *
52
	 * @param string $name the fqcn
53
	 * @return static
54
	 */
55 13
	public static function create(?string $name = null) {
56 13
		return new static($name);
57
	}
58
59
	/**
60
	 * Creates a new struct
61
	 *
62
	 * @param string $name the fqcn
63
	 */
64 56
	public function __construct(?string $name = null) {
65 56
		$this->setQualifiedName($name);
66 56
		$this->docblock = new Docblock();
67 56
		$this->useStatements = new Map();
68 56
		$this->requiredFiles = new Set();
69 56
		$this->methods = new Map();
70 56
	}
71
72
	/**
73
	 * Sets requried files
74
	 *
75
	 * @param array $files
76
	 * @return $this
77
	 */
78 1
	public function setRequiredFiles(array $files) {
79 1
		$this->requiredFiles = new Set($files);
80
81 1
		return $this;
82
	}
83
84
	/**
85
	 * Adds a new required file
86
	 *
87
	 * @param string $file
88
	 * @return $this
89
	 */
90 2
	public function addRequiredFile(string $file) {
91 2
		$this->requiredFiles->add($file);
92
93 2
		return $this;
94
	}
95
96
	/**
97
	 * Returns required files
98
	 *
99
	 * @return Set collection of filenames
100
	 */
101 17
	public function getRequiredFiles(): Set {
102 17
		return $this->requiredFiles;
103
	}
104
105
	/**
106
	 * Sets use statements
107
	 *
108
	 * @see #addUseStatement
109
	 * @see #declareUses()
110
	 * @param array $useStatements
111
	 * @return $this
112
	 */
113 1
	public function setUseStatements(array $useStatements) {
114 1
		$this->useStatements->clear();
115 1
		foreach ($useStatements as $alias => $useStatement) {
116 1
			$this->addUseStatement($useStatement, $alias);
117
		}
118
119 1
		return $this;
120
	}
121
122
	/**
123
	 * Adds a use statement with an optional alias
124
	 *
125
	 * @param string $qualifiedName
126
	 * @param null|string $alias
127
	 * @return $this
128
	 */
129 5
	public function addUseStatement(string $qualifiedName, string $alias = null) {
130 5
		if (!is_string($alias)) {
131 4
			if (false === strpos($qualifiedName, '\\')) {
132 2
				$alias = $qualifiedName;
133
			} else {
134 4
				$alias = substr($qualifiedName, strrpos($qualifiedName, '\\') + 1);
135
			}
136
		}
137
138 5
		$this->useStatements->set($alias, $qualifiedName);
139
140 5
		return $this;
141
	}
142
143
	/**
144
	 * Clears all use statements
145
	 *
146
	 * @return $this
147
	 */
148 1
	public function clearUseStatements() {
149 1
		$this->useStatements->clear();
150
151 1
		return $this;
152
	}
153
154
	/**
155
	 * Declares multiple use statements at once.
156
	 *
157
	 * @param ... use statements multiple qualified names
158
	 * @return $this
159
	 */
160 1
	public function declareUses(string ...$uses) {
161 1
		foreach ($uses as $name) {
162 1
			$this->declareUse($name);
163
		}
164 1
		return $this;
165
	}
166
167
	/**
168
	 * Declares a "use $fullClassName" with " as $alias" if $alias is available,
169
	 * and returns its alias (or not qualified classname) to be used in your actual
170
	 * php code.
171
	 *
172
	 * If the class has already been declared you get only the set alias.
173
	 *
174
	 * @param string $qualifiedName
175
	 * @param null|string $alias
176
	 * @return string the used alias
177
	 */
178 1
	public function declareUse(string $qualifiedName, string $alias = null) {
179 1
		$qualifiedName = trim($qualifiedName, '\\');
180 1
		if (!$this->hasUseStatement($qualifiedName)) {
181 1
			$this->addUseStatement($qualifiedName, $alias);
182
		}
183 1
		return $this->getUseAlias($qualifiedName);
184
	}
185
186
	/**
187
	 * Returns whether the given use statement is present
188
	 *
189
	 * @param string $qualifiedName
190
	 * @return bool
191
	 */
192 3
	public function hasUseStatement(string $qualifiedName): bool {
193 3
		return $this->useStatements->contains($qualifiedName);
194
	}
195
196
	/**
197
	 * Returns the usable alias for a qualified name
198
	 *
199
	 * @param string $qualifiedName
200
	 * @return string the alias
201
	 */
202 1
	public function getUseAlias(string $qualifiedName): string {
203 1
		return $this->useStatements->getKey($qualifiedName);
204
	}
205
206
	/**
207
	 * Removes a use statement
208
	 *
209
	 * @param string $qualifiedName
210
	 * @return $this
211
	 */
212 3
	public function removeUseStatement(string $qualifiedName) {
213 3
		$this->useStatements->remove($this->useStatements->getKey($qualifiedName));
214 3
		return $this;
215
	}
216
217
	/**
218
	 * Returns all use statements
219
	 *
220
	 * @return Map collection of use statements
221
	 */
222 17
	public function getUseStatements(): Map {
223 17
		return $this->useStatements;
224
	}
225
226
	/**
227
	 * Sets a collection of methods
228
	 *
229
	 * @param PhpMethod[] $methods
230
	 * @return $this
231
	 */
232 1
	public function setMethods(array $methods) {
233 1
		foreach ($this->methods as $method) {
234 1
			$method->setParent(null);
235
		}
236
237 1
		$this->methods->clear();
238 1
		foreach ($methods as $method) {
239 1
			$this->setMethod($method);
240
		}
241
242 1
		return $this;
243
	}
244
245
	/**
246
	 * Adds a method
247
	 *
248
	 * @param PhpMethod $method
249
	 * @return $this
250
	 */
251 15
	public function setMethod(PhpMethod $method) {
252 15
		$method->setParent($this);
253 15
		$this->methods->set($method->getName(), $method);
254
255 15
		return $this;
256
	}
257
258
	/**
259
	 * Removes a method
260
	 *
261
	 * @param string|PhpMethod $nameOrMethod method name or Method instance
262
	 * @throws \InvalidArgumentException if the method cannot be found
263
	 * @return $this
264
	 */
265 2
	public function removeMethod($nameOrMethod) {
266 2
		if ($nameOrMethod instanceof PhpMethod) {
267 1
			$nameOrMethod = $nameOrMethod->getName();
268
		}
269
270 2
		if (!$this->methods->has($nameOrMethod)) {
271 1
			throw new \InvalidArgumentException(sprintf('The method "%s" does not exist.', $nameOrMethod));
272
		}
273 1
		$m = $this->methods->get($nameOrMethod);
274 1
		$m->setParent(null);
275 1
		$this->methods->remove($nameOrMethod);
276
277 1
		return $this;
278
	}
279
280
	/**
281
	 * Checks whether a method exists or not
282
	 *
283
	 * @param string|PhpMethod $nameOrMethod method name or Method instance
284
	 * @return bool `true` if it exists and `false` if not
285
	 */
286 2
	public function hasMethod($nameOrMethod): bool {
287 2
		if ($nameOrMethod instanceof PhpMethod) {
288 1
			$nameOrMethod = $nameOrMethod->getName();
289
		}
290
291 2
		return $this->methods->has($nameOrMethod);
292
	}
293
294
	/**
295
	 * Returns a method
296
	 *
297
	 * @param string $nameOrMethod the methods name
298
	 * @throws \InvalidArgumentException if the method cannot be found
299
	 * @return PhpMethod
300
	 */
301 9
	public function getMethod($nameOrMethod): PhpMethod {
302 9
		if ($nameOrMethod instanceof PhpMethod) {
303 1
			$nameOrMethod = $nameOrMethod->getName();
304
		}
305
306 9
		if (!$this->methods->has($nameOrMethod)) {
307 2
			throw new \InvalidArgumentException(sprintf('The method "%s" does not exist.', $nameOrMethod));
308
		}
309
310 8
		return $this->methods->get($nameOrMethod);
311
	}
312
313
	/**
314
	 * Returns all methods
315
	 *
316
	 * @return Map collection of methods
317
	 */
318 17
	public function getMethods(): Map {
319 17
		return $this->methods;
320
	}
321
322
	/**
323
	 * Returns all method names
324
	 *
325
	 * @return Set
326
	 */
327 1
	public function getMethodNames(): Set {
328 1
		return $this->methods->keys();
329
	}
330
331
	/**
332
	 * Clears all methods
333
	 *
334
	 * @return $this
335
	 */
336 1
	public function clearMethods() {
337 1
		foreach ($this->methods as $method) {
338 1
			$method->setParent(null);
339
		}
340 1
		$this->methods->clear();
341
342 1
		return $this;
343
	}
344
345
	/**
346
	 * Generates a docblock from provided information
347
	 *
348
	 * @return $this
349
	 */
350 11
	public function generateDocblock() {
351 11
		$docblock = $this->getDocblock();
352 11
		$docblock->setShortDescription($this->getDescription());
353 11
		$docblock->setLongDescription($this->getLongDescription());
354
355 11
		foreach ($this->methods as $method) {
356 7
			$method->generateDocblock();
357
		}
358
359 11
		return $this;
360
	}
361
}
362