Parameters::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace gossi\swagger\collections;
3
4
use gossi\swagger\Parameter;
5
use gossi\swagger\parts\RefPart;
6
use phootwork\collection\ArrayList;
7
use phootwork\collection\CollectionUtils;
8
use phootwork\lang\Arrayable;
9
use gossi\swagger\AbstractModel;
10
11
class Parameters extends AbstractModel implements Arrayable, \Iterator {
12
13
	use RefPart;
14
15
	/** @var ArrayList */
16
	private $parameters;
17
18 13
	public function __construct($contents = []) {
19 13
		$this->parse($contents === null ? [] : $contents);
20 13
	}
21
22 13
	private function parse($contents) {
23 13
		$data = CollectionUtils::toMap($contents);
24
25 13
		$this->parameters = new ArrayList();
26 13
		$this->parseRef($data);
27
28 13
		if (!$this->hasRef()) {
29 13
			foreach ($data as $param) {
30 4
				$this->parameters->add(new Parameter($param));
31 13
			}
32 13
		}
33 13
	}
34
35 9
	public function toArray() {
36 9
		if ($this->hasRef()) {
37 1
			return ['$ref' => $this->getRef()];
38
		}
39
40 9
		return CollectionUtils::toArrayRecursive($this->parameters);
41
	}
42
43 1
	public function size() {
44 1
		return $this->parameters->size();
45
	}
46
47
	/**
48
	 * Searches whether a parameter with the given name exists
49
	 *
50
	 * @param string $name
51
	 * @return bool
52
	 */
53 1
	public function searchByName($name) {
54
		return $this->parameters->search($name, function (Parameter $param, $name) {
55 1
			return $param->getName() == $name;
56 1
		});
57
	}
58
59
	/**
60
	 * Returns parameter with the given name if it exists
61
	 *
62
	 * @param string $name
63
	 * @return Parameter|void
64
	 */
65 1
	public function findByName($name) {
66 1
		foreach ($this->parameters as $param) {
67 1
			if ($param->getName() == $name) {
68 1
				return $param;
69
			}
70 1
		}
71 1
	}
72
73
	/**
74
	 * Searches for the parameter with the given name. Creates a parameter with the given name
75
	 * if none exists
76
	 *
77
	 * @param string $name
78
	 * @return Parameter
79
	 */
80
	public function getByName($name) {
81
		$param = $this->findByName($name);
82
		if (empty($param)) {
83
			$param = new Parameter();
84
			$param->setName($name);
85
			$this->parameters->add($param);
86
		}
87
88
		return $param;
89
	}
90
91
	/**
92
	 * Searches whether a parameter with the given unique combination exists
93
	 *
94
	 * @param string $name
95
	 * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
	 * @return bool
97
	 */
98
	public function search($name, $in) {
99 1
		return $this->parameters->search(function (Parameter $param) use ($name, $in) {
100 1
			return $param->getIn() == $in && $param->getName() == $name;
101 1
		});
102
	}
103
104 1
	public function find($name, $in) {
105 1
		foreach ($this->parameters as $param) {
106 1
			if ($param->getIn() == $in && $param->getName() == $name) {
107 1
				return $param;
108
			}
109 1
		}
110 1
	}
111
112 1
	public function get($name, $in) {
113 1
		$param = $this->find($name, $in);
114 1
		if (empty($param)) {
115 1
			$param = new Parameter();
116 1
			$param->setName($name);
117 1
			$param->setIn($in);
118 1
			$this->parameters->add($param);
119 1
		}
120
121 1
		return $param;
122
	}
123
124
	/**
125
	 * Adds a parameter
126
	 *
127
	 * @param Parameter $param
128
	 */
129 1
	public function add(Parameter $param) {
130 1
		$this->parameters->add($param);
131 1
	}
132
133
	/**
134
	 * Removes a parameter
135
	 *
136
	 * @param Parameter $param
137
	 */
138 1
	public function remove(Parameter $param) {
139 1
		$this->parameters->remove($param);
140 1
	}
141
142
	/**
143
	 * Returns whether a given parameter exists
144
	 *
145
	 * @param Parameter $param
146
	 * @return bool
147
	 */
148 1
	public function contains(Parameter $param) {
149 1
		return $this->parameters->contains($param);
150
	}
151
152
	public function current() {
153
		return $this->parameters->current();
154
	}
155
156
	public function key() {
157
		return $this->parameters->key();
158
	}
159
160
	public function next() {
161
		return $this->parameters->next();
162
	}
163
164
	public function rewind() {
165
		return $this->parameters->rewind();
166
	}
167
168
	public function valid() {
169
		return $this->parameters->valid();
170
	}
171
}
172