Passed
Push — master ( 3c6756...8b3b37 )
by Michael
20:43 queued 12:59
created

Request   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 111
rs 10
wmc 23

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getColor() 0 10 4
A get() 0 6 2
A getFloat() 0 7 2
A getInstance() 0 5 2
A getInt() 0 7 2
A set() 0 3 1
A getRegex() 0 7 3
A getOption() 0 7 3
A __construct() 0 3 1
A getCoordinate() 0 7 3
1
<?php
2
	/**
3
    This file is part of WideImage.
4
		
5
    WideImage is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU Lesser General Public License as published by
7
    the Free Software Foundation; either version 2.1 of the License, or
8
    (at your option) any later version.
9
		
10
    WideImage is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU Lesser General Public License for more details.
14
		
15
    You should have received a copy of the GNU Lesser General Public License
16
    along with WideImage; if not, write to the Free Software
17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
  **/
19
	
20
	/**
21
	 * @package Demos
22
	 */
23
	class Request
24
	{
25
		protected $vars = array();
26
		
27
		protected static $instance;
28
		static function getInstance()
29
		{
30
			if (self::$instance === null)
31
				self::$instance = new Request;
32
			return self::$instance;
33
		}
34
		
35
		protected function __construct()
36
		{
37
			$this->vars = $_GET;
38
			
39
			/*
40
			// have to rely on parsing QUERY_STRING, thanks to PHP
41
			// http://bugs.php.net/bug.php?id=39078
42
			// http://bugs.php.net/bug.php?id=45149
43
			$all_vars = explode('&', $_SERVER['QUERY_STRING']);
44
			foreach ($all_vars as $keyval)
45
			{
46
				if (strlen($keyval) == 0)
47
					continue;
48
				
49
				if (strpos($keyval, '=') === false)
50
				{
51
					$key = $keyval;
52
					$value = true;
53
				}
54
				else
55
				{
56
					list($key, $value) = explode('=', $keyval);
57
					#$value = str_replace('%2B', '[[PLUS]]', $value);
58
					$value = urldecode($value);
59
					#$value = str_replace('[[PLUS]]', '+', $value);
60
				}
61
				$this->vars[$key] = $value;
62
			}
63
			*/
64
		}
65
		
66
		function get($key, $default = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
		{
68
			if (isset($this->vars[$key]))
69
				return $this->vars[$key];
70
			else
71
				return $default;
72
		}
73
		
74
		function set($key, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
		{
76
			$this->vars[$key] = $value;
77
		}
78
		
79
		function getInt($key, $default = 0)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
		{
81
			$value = self::get($key);
0 ignored issues
show
Bug Best Practice introduced by
The method Request::get() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
			/** @scrutinizer ignore-call */ 
82
   $value = self::get($key);
Loading history...
82
			if (strlen($value) > 0)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
			if (strlen(/** @scrutinizer ignore-type */ $value) > 0)
Loading history...
83
				return intval($value);
84
			else
85
				return $default;
86
		}
87
		
88
		function getFloat($key, $default = 0)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
		{
90
			$value = self::get($key);
0 ignored issues
show
Bug Best Practice introduced by
The method Request::get() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
			/** @scrutinizer ignore-call */ 
91
   $value = self::get($key);
Loading history...
91
			if (strlen($value) > 0)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
			if (strlen(/** @scrutinizer ignore-type */ $value) > 0)
Loading history...
92
				return floatval($value);
93
			else
94
				return $default;
95
		}
96
		
97
		function getCoordinate($key, $default = 0)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
		{
99
			$v = self::get($key);
0 ignored issues
show
Bug Best Practice introduced by
The method Request::get() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
			/** @scrutinizer ignore-call */ 
100
   $v = self::get($key);
Loading history...
100
			if (strlen($v) > 0 && \WideImage\Coordinate::parse($v) !== null)
0 ignored issues
show
Bug introduced by
It seems like $v can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
			if (strlen(/** @scrutinizer ignore-type */ $v) > 0 && \WideImage\Coordinate::parse($v) !== null)
Loading history...
101
				return self::get($key);
102
			else
103
				return $default;
104
		}
105
		
106
		function getOption($key, $valid = array(), $default = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
107
		{
108
			$value = self::get($key);
0 ignored issues
show
Bug Best Practice introduced by
The method Request::get() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
			/** @scrutinizer ignore-call */ 
109
   $value = self::get($key);
Loading history...
109
			if ($value !== null && in_array($value, $valid))
110
				return strval($value);
111
			else
112
				return $default;
113
		}
114
		
115
		function getColor($key, $default = '000000')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
		{
117
			$value = self::get($key);
0 ignored issues
show
Bug Best Practice introduced by
The method Request::get() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
			/** @scrutinizer ignore-call */ 
118
   $value = self::get($key);
Loading history...
118
			if (substr($value, 0, 1) == '#')
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
			if (substr(/** @scrutinizer ignore-type */ $value, 0, 1) == '#')
Loading history...
119
				$value = substr($value, 1);
120
			
121
			if ($value === '' || preg_match('~^[0-9a-f]{1,6}$~i', $value))
122
				return $value;
123
			else
124
				return $default;
125
		}
126
		
127
		function getRegex($key, $regex, $default = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
		{
129
			$value = self::get($key);
0 ignored issues
show
Bug Best Practice introduced by
The method Request::get() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
			/** @scrutinizer ignore-call */ 
130
   $value = self::get($key);
Loading history...
130
			if ($value !== null && preg_match($regex, $value))
131
				return $value;
132
			else
133
				return $default;
134
		}
135
	}
136