Completed
Push — master ( 167670...8f24e9 )
by Martijn
02:30
created

ObjectType::parseRange()   C

Complexity

Conditions 15
Paths 130

Size

Total Lines 19
Code Lines 13

Duplication

Lines 16
Ratio 84.21 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 19
rs 6.4166
cc 15
eloc 13
nc 130
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SwaggerGen\Swagger\Type;
4
5
/**
6
 * Basic object type definition.
7
 *
8
 * @package    SwaggerGen
9
 * @author     Martijn van der Lee <[email protected]>
10
 * @copyright  2014-2015 Martijn van der Lee
11
 * @license    https://opensource.org/licenses/MIT MIT
12
 */
13
class ObjectType extends AbstractType
14
{
15
16
	const REGEX_PROP_START = '/([^:\?]+)(\?)?:';
17
	const REGEX_PROP_FORMAT = '[a-z]+';
18
	const REGEX_PROP_PROPERTIES = '(?:\(.*\))?';
19
	const REGEX_PROP_RANGE = '(?:[[<][^,]*,[^,]*[\\]>])?';
20
	const REGEX_PROP_DEFAULT = '(?:=.+?)?';
21
	const REGEX_PROP_END = '(?:,|$)/i';
22
23
	private $minProperties = null;
24
	private $maxProperties = null;
25
	private $required = array();
26
27
	/**
28
	 * @var Property[]
29
	 */
30
	private $properties = array();
31
32
	protected function parseDefinition($definition)
33
	{
34
		$match = array();
35
		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_END, $definition, $match) !== 1) {
36
			throw new \SwaggerGen\Exception("Unparseable object definition: '{$definition}'");
37
		}
38
39
		$this->parseFormat($definition, $match);
40
		$this->parseProperties($definition, $match);
41
		$this->parseRange($definition, $match);
42
	}
43
44
	private function parseFormat($definition, $match)
45
	{
46
		if (strtolower($match[1]) !== 'object') {
47
			throw new \SwaggerGen\Exception("Not an object: '{$definition}'");
48
		}
49
	}
50
51
	private function parseProperties($definition, $match)
52
	{
53
		if (!empty($match[2])) {
54
			$prop_matches = array();
55
			if (preg_match_all(self::REGEX_PROP_START . '(' . self::REGEX_PROP_FORMAT . self::REGEX_PROP_PROPERTIES . self::REGEX_PROP_RANGE . self::REGEX_PROP_DEFAULT . ')' . self::REGEX_PROP_END, $match[2], $prop_matches, PREG_SET_ORDER) === 0) {
56
				throw new \SwaggerGen\Exception("Unparseable properties definition: '{$definition}'");
57
			}
58
			foreach ($prop_matches as $prop_match) {
59
				$this->properties[$prop_match[1]] = new Property($this, $prop_match[3]);
60
				if ($prop_match[2] !== '?') {
61
					$this->required[$prop_match[1]] = true;
62
				}
63
			}
64
		}
65
	}
66
67
	private function parseRange($definition, $match)
68
	{
69 View Code Duplication
		if (!empty($match[3])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
			if ($match[4] === '' && $match[5] === '') {
71
				throw new \SwaggerGen\Exception("Empty object range: '{$definition}'");
72
			}
73
74
			$exclusiveMinimum = isset($match[3]) ? ($match[3] == '<') : null;
75
			$this->minProperties = $match[4] === '' ? null : intval($match[4]);
76
			$this->maxProperties = $match[5] === '' ? null : intval($match[5]);
77
			$exclusiveMaximum = isset($match[6]) ? ($match[6] == '>') : null;
78
			if ($this->minProperties && $this->maxProperties && $this->minProperties > $this->maxProperties) {
79
				self::swap($this->minProperties, $this->maxProperties);
80
				self::swap($exclusiveMinimum, $exclusiveMaximum);
81
			}
82
			$this->minProperties = $this->minProperties === null ? null : max(0, $exclusiveMinimum ? $this->minProperties + 1 : $this->minProperties);
83
			$this->maxProperties = $this->maxProperties === null ? null : max(0, $exclusiveMaximum ? $this->maxProperties - 1 : $this->maxProperties);
84
		}
85
	}
86
87
	/**
88
	 * @param string $command The comment command
89
	 * @param string $data Any data added after the command
90
	 * @return \SwaggerGen\Swagger\Type\AbstractType|boolean
91
	 */
92
	public function handleCommand($command, $data = null)
93
	{
94
		switch (strtolower($command)) {
95
			// type name description...
96
			case 'property':
97
			case 'property?':
98
				$definition = self::wordShift($data);
99
				if (empty($definition)) {
100
					throw new \SwaggerGen\Exception("Missing property definition");
101
				}
102
103
				$name = self::wordShift($data);
104
				if (empty($name)) {
105
					throw new \SwaggerGen\Exception("Missing property name: '{$definition}'");
106
				}
107
108
				$this->properties[$name] = new Property($this, $definition, $data);
109
110
				unset($this->required[$name]);
111
				if (substr($command, -1) !== '?') {
112
					$this->required[$name] = true;
113
				}
114
				return $this;
115
116 View Code Duplication
			case 'min':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
				$this->minProperties = intval($data);
118
				if ($this->minProperties < 0) {
119
					throw new \SwaggerGen\Exception("Minimum less than zero: '{$data}'");
120
				}
121
				if ($this->maxProperties !== null && $this->minProperties > $this->maxProperties) {
122
					throw new \SwaggerGen\Exception("Minimum greater than maximum: '{$data}'");
123
				}
124
				$this->minProperties = intval($data);
125
				return $this;
126
127 View Code Duplication
			case 'max':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
				$this->maxProperties = intval($data);
129
				if ($this->minProperties !== null && $this->minProperties > $this->maxProperties) {
130
					throw new \SwaggerGen\Exception("Maximum less than minimum: '{$data}'");
131
				}
132
				if ($this->maxProperties < 0) {
133
					throw new \SwaggerGen\Exception("Maximum less than zero: '{$data}'");
134
				}
135
				return $this;
136
		}
137
138
		return parent::handleCommand($command, $data);
139
	}
140
141
	public function toArray()
142
	{
143
		return self::arrayFilterNull(array(
144
					'type' => 'object',
145
					'required' => array_keys($this->required),
146
					'properties' => self::objectsToArray($this->properties),
147
					'minProperties' => $this->minProperties,
148
					'maxProperties' => $this->maxProperties,
149
		));
150
	}
151
152
	public function __toString()
153
	{
154
		return __CLASS__;
155
	}
156
157
}
158