Completed
Push — master ( 0cb618...205443 )
by Steve
11:02
created

HttpRequestFactory::marshalProtocolVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.1971

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 3
cts 8
cp 0.375
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 5.1971
1
<?php
2
/**
3
 * @package    Fuel\Foundation
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2016 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
namespace Fuel\Foundation\Request;
12
13
use UnexpectedValueException;
14
use Zend\Diactoros\ServerRequestFactory;
15
16
/**
17
 * Temporary request factory until fuel has a http message implementation
18
 *
19
 * @package Fuel\Foundation\Request
20
 */
21
class HttpRequestFactory extends ServerRequestFactory
22
{
23 6
	public static function fromGlobals(
24
		array $server = null,
25
		array $query = null,
26
		array $body = null,
27
		array $cookies = null,
28
		array $files = null
29
	) {
30 6
		$server  = static::normalizeServer($server ?: $_SERVER);
31 6
		$files   = static::normalizeFiles($files ?: $_FILES);
32 6
		$headers = static::marshalHeaders($server);
33
34 6
		return new Http(
35
			$server,
36
			$files,
37 6
			static::marshalUriFromServer($server, $headers),
38 6
			static::get('REQUEST_METHOD', $server, 'GET'),
39 6
			'php://input',
40
			$headers,
41 6
			$cookies ?: $_COOKIE,
42 6
			$query ?: $_GET,
43 6
			$body ?: $_POST,
44 6
			static::marshalProtocolVersion($server)
0 ignored issues
show
Bug introduced by
Since marshalProtocolVersion() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of marshalProtocolVersion() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
45
		);
46
47
	}
48
49 6
	private static function marshalProtocolVersion(array $server)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
50
	{
51 6
		if (! isset($server['SERVER_PROTOCOL'])) {
52 6
			return '1.1';
53
		}
54
55
		if (! preg_match('#^(HTTP/)?(?P<version>[1-9]\d*(?:\.\d)?)$#', $server['SERVER_PROTOCOL'], $matches)) {
56
			throw new UnexpectedValueException(sprintf(
57
				'Unrecognized protocol version (%s)',
58
				$server['SERVER_PROTOCOL']
59
			));
60
		}
61
62
		return $matches['version'];
63
	}
64
}
65