PropertiesPart   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 129
ccs 42
cts 42
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initProperties() 0 3 1
A setProperties() 0 13 3
A setProperty() 0 6 1
A removeProperty() 0 14 3
A hasProperty() 0 7 2
A getProperty() 0 11 3
A getProperties() 0 3 1
A getPropertyNames() 0 3 1
A clearProperties() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\model\parts;
5
6
use gossi\codegen\model\PhpProperty;
7
use phootwork\collection\Map;
8
use phootwork\collection\Set;
9
10
/**
11
 * Properties part
12
 *
13
 * For all models that can have properties
14
 *
15
 * @author Thomas Gossmann
16
 */
17
trait PropertiesPart {
18
19
	/** @var Map */
20
	private $properties;
21
22 50
	private function initProperties() {
23 50
		$this->properties = new Map();
24 50
	}
25
26
	/**
27
	 * Sets a collection of properties
28
	 *
29
	 * @param PhpProperty[] $properties
30
	 * @return $this
31
	 */
32 1
	public function setProperties(array $properties) {
33 1
		foreach ($this->properties as $prop) {
34 1
			$prop->setParent(null);
35
		}
36
37 1
		$this->properties->clear();
38
39 1
		foreach ($properties as $prop) {
40 1
			$this->setProperty($prop);
41
		}
42
43 1
		return $this;
44
	}
45
46
	/**
47
	 * Adds a property
48
	 *
49
	 * @param PhpProperty $property
50
	 * @return $this
51
	 */
52 11
	public function setProperty(PhpProperty $property) {
53 11
		$property->setParent($this);
54 11
		$this->properties->set($property->getName(), $property);
55
56 11
		return $this;
57
	}
58
59
	/**
60
	 * Removes a property
61
	 *
62
	 * @param PhpProperty|string $nameOrProperty property name or instance
63
	 * @throws \InvalidArgumentException If the property cannot be found
64
	 * @return $this
65
	 */
66 2
	public function removeProperty($nameOrProperty) {
67 2
		if ($nameOrProperty instanceof PhpProperty) {
68 1
			$nameOrProperty = $nameOrProperty->getName();
69
		}
70
71 2
		if (!$this->properties->has($nameOrProperty)) {
72 1
			throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $nameOrProperty));
73
		}
74 1
		$p = $this->properties->get($nameOrProperty);
75 1
		$p->setParent(null);
76 1
		$this->properties->remove($nameOrProperty);
77
78 1
		return $this;
79
	}
80
81
	/**
82
	 * Checks whether a property exists
83
	 *
84
	 * @param PhpProperty|string $nameOrProperty property name or instance
85
	 * @return bool `true` if a property exists and `false` if not
86
	 */
87 1
	public function hasProperty($nameOrProperty): bool {
88 1
		if ($nameOrProperty instanceof PhpProperty) {
89 1
			$nameOrProperty = $nameOrProperty->getName();
90
		}
91
92 1
		return $this->properties->has($nameOrProperty);
93
	}
94
95
	/**
96
	 * Returns a property
97
	 *
98
	 * @param string $nameOrProperty property name
99
	 * @throws \InvalidArgumentException If the property cannot be found
100
	 * @return PhpProperty
101
	 */
102 5
	public function getProperty($nameOrProperty): PhpProperty {
103 5
		if ($nameOrProperty instanceof PhpProperty) {
104 1
			$nameOrProperty = $nameOrProperty->getName();
105
		}
106
107 5
		if (!$this->properties->has($nameOrProperty)) {
108 1
			throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $nameOrProperty));
109
		}
110
111 5
		return $this->properties->get($nameOrProperty);
112
	}
113
114
	/**
115
	 * Returns a collection of properties
116
	 *
117
	 * @return Map
118
	 */
119 15
	public function getProperties(): Map {
120 15
		return $this->properties;
121
	}
122
123
	/**
124
	 * Returns all property names
125
	 *
126
	 * @return Set
127
	 */
128 1
	public function getPropertyNames(): Set {
129 1
		return $this->properties->keys();
130
	}
131
132
	/**
133
	 * Clears all properties
134
	 *
135
	 * @return $this
136
	 */
137 1
	public function clearProperties() {
138 1
		foreach ($this->properties as $property) {
139 1
			$property->setParent(null);
140
		}
141 1
		$this->properties->clear();
142
143 1
		return $this;
144
	}
145
}
146