Completed
Push — master ( a88656...9f39a0 )
by Alexander
130:24 queued 126:50
created

RequestFactory::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * HTTP request object factory
4
 *
5
 * This factory should be used in index.php for request object initialization.
6
 * Example:
7
 *
8
 * $validator = (new Validator)->setAdapter(new PhpFilters);
9
 * $request = RequestFactory::create($_SERVER['CONTENT_TYPE'])->setValidator($validator);
10
 *
11
 * @file      RequestFactory.php
12
 *
13
 * PHP version 5.6+
14
 *
15
 * @author    Yancharuk Alexander <alex at itvault dot info>
16
 * @copyright © 2012-2017 Alexander Yancharuk <alex at itvault at info>
17
 * @date      2017-04-23 16:12
18
 * @license   The BSD 3-Clause License
19
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
20
 */
21
22
namespace Veles\Request;
23
24
class RequestFactory
25
{
26
	/**
27
	 * Create HTTP-request object depending on Content-type HTTP-header
28
	 *
29
	 * @param string $type Value of Content-type HTTP-header
30
	 *
31
	 * @return HttpRequestAbstract
32
	 */
33 8
	public static function create($type)
34
	{
35 8
		if (!isset($type) or 0 === strpos($type, 'text/html')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
36 3
			return new HttpGetRequest;
37
		}
38
39 5
		return new HttpPostRequest;
40
	}
41
}
42