Completed
Pull Request — master (#33)
by Martijn
02:18
created

Example::getUserHeight_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Api\Rest;
4
5
/*
6
 * @rest\description SwaggerGen 2 Example API
7
 * @rest\title Example API
8
 * @rest\contact http://example.com Arthur D. Author
9
 * @rest\license MIT
10
 * @rest\security api_key apikey X-Api-Authentication header Authenticate using this fancy header
11
 * @rest\require api_key
12
 */
13
14
/**
15
 * @rest\error 404 This is not the resource you are looking for
16
 * @rest\x-http-message 404
17
 * @rest\doc http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
18
 * RFC2616, sec.10: W3C status code definitions
19
 */
20
function NotFound()
21
{
22
	header("HTTP/1.0 404 Not Found");
23
}
24
25
/**
26
 * This class demonstrates how to use the SwaggerGen PHPDoc comments.
27
 * It is not meant as an example of a solid Rest API or a complete demonstration
28
 * of all SwaggerGen features.
29
 *
30
 * @rest\api Users Get some useful information on users
31
 */
32
class Example
33
{
34
35
	private $data = array();
36
37
	public function __construct()
38
	{
39
		$this->data = json_decode(file_get_contents('example.json'), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode(file_get_con...('example.json'), true) of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
	}
41
42
	public function request($method, $path, $data = null)
43
	{
44
		preg_match_all('/([^\/]+)(?:\/([^\/]+))?/', $path, $matches, PREG_SET_ORDER);
45
46
		$arguments = array();
47
		$methodname = strtolower($method);
48
		foreach ($matches as $match) {
49
			$methodname .= ucfirst($match[1]);
50
			if (empty($match[2])) {
51
				$methodname .= '_data';
52
			} else {
53
				$arguments[] = $match[2];
54
			}
55
		}
56
57
		if (!method_exists($this, $methodname)) {
58
			throw new \Exception('Method Not Allowed', 405); // @rest\errors 405
59
		}
60
61
		$arguments[] = $data;
62
		return call_user_func_array(array($this, $methodname), $arguments);
63
	}
64
65
	/**
66
	 * @rest\endpoint /user
67
	 * @rest\method GET Get a list of all users
68
	 * @rest\require api_key
69
	 * @rest\response 200 array(string)
70
	 */
71
	private function getUser_data()
72
	{
73
		return array_keys($this->data['users']);
74
	}
75
76
	/**
77
	 * @rest\ifdef admin
78
	 * @rest\endpoint /user/{username}
79
	 * @rest\method DELETE Delete a specific user
80
	 * @rest\endif
81
	 */
82
	private function deleteUser($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
	{
84
		// Pretend we're deleting the user.
85
		return array();
86
	}
87
88
	/**
89
	 * @rest\ifdef root
90
	 * @rest\endpoint /user
91
	 * @rest\method DELETE Delete all users
92
	 * @rest\endif
93
	 */
94
	private function deleteAllUsers($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
	{
96
		// Pretend we're deleting all users.
97
		return array();
98
	}
99
100
	/**
101
	 * @rest\endpoint /user/{username}
102
	 * @rest\method GET Get a list of all users
103
	 * @rest\path String username Name of the user
104
	 * @rest\see self::request
105
	 * @todo get "errors" from self::request, without context issues; filter out methods without a @rest\method statement
106
	 */
107
	private function getUser($name)
108
	{
109
		/**
110
		 * @rest\model User
111
		 * @rest\property int age Age of the user in years
112
		 * @rest\example 123
113
		 * @rest\property int height Height of the user in centimeters
114
		 */
115
		//return $this->data['users'][$name]; // @rest\response OK User
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
116
		return $this->data['users'][$name]; // @rest\response OK object(age:int[0,100>,height:float) User
117
	}
118
119
	private function getUserAge_data($name)
120
	{
121
		return $this->data['users'][$name]['age'];
122
	}
123
124
	private function getUserHeight_data($name)
125
	{
126
		return $this->data['users'][$name]['height'];
127
	}
128
129
	private function getUserHeight($name, $unit)
130
	{
131
		static $units = array(
132
			'meter' => 100,
133
			'inch' => 2.54,
134
			'foot' => 30.48,
135
			'yard' => 91.44,
136
		);
137
138
		return $this->data['users'][$name]['height'] / $units[$unit];
139
	}
140
141
	private function getAge_data()
142
	{
143
		$age = 0;
144
145
		foreach ($this->data['users'] as $user) {
146
			$age += $user['age'];
147
		}
148
149
		return $age / count($this->data['users']);
150
	}
151
152
	/**
153
	 * @rest\endpoint /user/{username}/image
154
	 * @rest\method put
155
	 * @rest\consumes fileform
156
	 * @rest\path String username Name of the user
157
	 * @rest\form file File Image file (PNG, GIF or JPEG) for the user.
158
	 * Any transparency in the file is replaced with a white background color.
159
	 * Files will be cropped to a square image.
160
	 */
161
	private function putUserImage($name, $data)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
162
	{
163
		// not implemented
164
		// @rest\see NotFound
165
	}
166
167
	/**
168
	 * @rest\endpoint /user/{username}/ip
169
	 * @rest\method put
170
	 * @rest\path String username Name of the user
171
	 * @rest\form ipv4 IpAddress IP address of the user
172
	 * @rest\response 204
173
	 */
174
	private function putUserIp($name, $data)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
	{
176
		// not implemented
177
	}
178
179
}
180