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

ObjectType   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 145
Duplicated Lines 24.14 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 40
c 0
b 0
f 0
lcom 1
cbo 3
dl 35
loc 145
rs 8.2608

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseDefinition() 0 11 2
A parseFormat() 0 6 2
B parseProperties() 0 15 5
C parseRange() 16 19 15
C handleCommand() 19 48 14
A toArray() 0 10 1
A __toString() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ObjectType often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ObjectType, and based on these observations, apply Extract Interface, too.

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