Completed
Push — master ( cfeaa0...522b4d )
by Martijn
03:14
created

Operation::toArray()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 43
rs 5.2653

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;
4
5
/**
6
 * Describes a Swagger Operation object, which describes a method call of a
7
 * specific endpoint.
8
 *
9
 * @package    SwaggerGen
10
 * @author     Martijn van der Lee <[email protected]>
11
 * @copyright  2014-2015 Martijn van der Lee
12
 * @license    https://opensource.org/licenses/MIT MIT
13
 */
14
class Operation extends AbstractDocumentableObject
15
{
16
17
	private $tags = array();
18
	private $summary;
19
	private $description;
20
	private $operationid;
21
	private $consumes = array();
22
	private $produces = array();
23
24
	/**
25
	 * @var IParameter[]
26
	 */
27
	private $parameters = array();
28
	private $responses = array();
29
	private $schemes = array();
30
	private $deprecated = false;
31
	private $security = array();
32
33
	public function getConsumes()
34
	{
35
		return $this->consumes;
36
	}
37
38
	public function __construct(AbstractObject $parent, $summary = null, Tag $tag = null)
39
	{
40
		parent::__construct($parent);
41
		$this->summary = $summary;
42
		$this->operationid = uniqid('', true); //@todo getSwagger()->title -> Do some complex construction?
43
		if ($tag) {
44
			$this->tags[] = $tag->getName();
45
		}
46
	}
47
48
	public function handleCommand($command, $data = null)
49
	{
50
		switch (strtolower($command)) {
51
			// string
52
			case 'summary':
53
			case 'description':
54
				$this->$command = $data;
55
				return $this;
56
57
			// string[]
58
			case 'tags':
59 View Code Duplication
			case 'schemes':
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...
60
				$this->$command = array_merge($this->$command, self::wordSplit($data));
61
				return $this;
62
63
			// MIME[]
64
			case 'consumes':
65 View Code Duplication
			case 'produces':
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...
66
				$this->$command = array_merge($this->$command, self::translateMimeTypes(self::wordSplit($data)));
67
				return $this;
68
69
			// boolean
70
			case 'deprecated':
71
				$this->deprecated = true;
72
				return $this;
73
74
			case 'error':
75
				$code = self::wordShift($data);
76
				$reasoncode = Response::getCode($code);
77
				if ($reasoncode === null) {
78
					throw new \SwaggerGen\Exception("Invalid error code: '$code'");
79
				}
80
				$description = $data;
81
				$Error = new Error($this, $reasoncode, $description);
82
				$this->responses[$reasoncode] = $Error;
83
				return $Error;
84
85
			case 'errors':
86
				foreach (self::wordSplit($data) as $code) {
87
					$reasoncode = Response::getCode($code);
88
					if ($reasoncode === null) {
89
						throw new \SwaggerGen\Exception("Invalid error code: '$code'");
90
					}
91
					$this->responses[$reasoncode] = new Error($this, $reasoncode);
92
				}
93
				return $this;
94
95
			case 'path':
96
			case 'query': case 'query?':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
97
			case 'header': case 'header?':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
98
			case 'form': case 'form?':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
99
				$in = rtrim($command, '?');
100
				$Parameter = new Parameter($this, $in, $data, substr($command, -1) !== '?');
101
				$this->parameters[$Parameter->getName()] = $Parameter;
102
				return $Parameter;
103
104
			case 'body': case 'body?':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
105
				$Parameter = new BodyParameter($this, $data, substr($command, -1) !== '?');
106
				$this->parameters[$Parameter->getName()] = $Parameter;
107
				return $Parameter;
108
109
			case 'response':
110
				$code = self::wordShift($data);
111
				$reasoncode = Response::getCode($code);
112
				if ($reasoncode === null) {
113
					throw new \SwaggerGen\Exception("Invalid response code: '$code'");
114
				}
115
				$definition = self::wordShift($data);
116
				$description = $data;
117
				$Response = new Response($this, $reasoncode, $definition, $description);
118
				$this->responses[$reasoncode] = $Response;
119
				return $Response;
120
121 View Code Duplication
			case 'require':
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...
122
				$name = self::wordShift($data);
123
				if (empty($name)) {
124
					throw new \SwaggerGen\Exception('Empty security requirement name');
125
				}
126
				$scopes = self::wordSplit($data);
127
				sort($scopes);
128
				$this->security[] = array(
129
					$name => empty($scopes) ? array() : $scopes,
130
				);
131
				return $this;
132
133
			//@todo operationId			
134
		}
135
136
		return parent::handleCommand($command, $data);
137
	}
138
139
	public function toArray()
140
	{
141
		if (empty($this->responses)) {
142
			throw new \SwaggerGen\Exception('No response defined for operation');
143
		}
144
		ksort($this->responses);
145
146
		$tags = array_unique($this->tags);
147
		sort($tags);
148
149
		$schemes = array_unique($this->schemes);
150
		sort($schemes);
151
152
		$consumes = array_unique($this->consumes);
153
		sort($consumes);
154
155
		$produces = array_unique($this->produces);
156
		sort($produces);
157
158
		foreach ($this->security as $security) {
159
			foreach ($security as $name => $scope) {
160
				if ($this->getRoot()->getSecurity($name) === false) {
161
					throw new \SwaggerGen\Exception("Required security scheme not defined: '{$name}'");
162
				}
163
			}
164
		}
165
		
166
		$parameters = $this->parameters ? array_values($this->parameters) : null;
167
168
		return self::arrayFilterNull(array_merge(array(
169
					'deprecated' => $this->deprecated ? true : null,
170
					'tags' => $tags,
171
					'summary' => empty($this->summary) ? null : $this->summary,
172
					'description' => empty($this->description) ? null : $this->description,
173
					//'operationId' => $this->description,
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
174
					'consumes' => $consumes,
175
					'produces' => $produces,
176
					'parameters' => $parameters ? self::objectsToArray($parameters) : null,
177
					'schemes' => $schemes,
178
					'responses' => $this->responses ? self::objectsToArray($this->responses) : null,
179
					'security' => $this->security,
180
								), parent::toArray()));
181
	}
182
183
	public function __toString()
184
	{
185
		return __CLASS__ . ' ' . $this->summary;
186
	}
187
188
}
189