Completed
Push — master ( 63f9e6...34eb4c )
by Kenji
10s
created

replacing/helpers/old/3.1.7-url_helper.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
/**
12
 * CodeIgniter URL Helpers with little modification for testing
13
 *
14
 * @package		CodeIgniter
15
 * @subpackage	Helpers
16
 * @category	Helpers
17
 * @author		Kenji Suzuki <http://github.com/kenjis/ci-phpunit-test>
18
 * @author		EllisLab Dev Team
19
 * @link		https://codeigniter.com/user_guide/helpers/url_helper.html
20
 */
21
22
// ------------------------------------------------------------------------
23
24 View Code Duplication
if ( ! function_exists('redirect'))
0 ignored issues
show
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...
25
{
26
	function redirect($uri = '', $method = 'auto', $code = NULL)
0 ignored issues
show
The function redirect() has been defined more than once; this definition is ignored, only the first definition in application/tests/_ci_ph...ld/3.1.6-url_helper.php (L26-81) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
redirect uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
27
	{
28
		if ( ! preg_match('#^(\w+:)?//#i', $uri))
29
		{
30
			$uri = site_url($uri);
31
		}
32
33
		// IIS environment likely? Use 'refresh' for better compatibility
34
		if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
35
		{
36
			$method = 'refresh';
37
		}
38
		elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
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...
39
		{
40
			if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
41
			{
42
				$code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
43
					? 303	// reference: http://en.wikipedia.org/wiki/Post/Redirect/Get
44
					: 307;
45
			}
46
			else
47
			{
48
				$code = 302;
49
			}
50
		}
51
52
		switch ($method)
53
		{
54
			case 'refresh':
55
				if (ENVIRONMENT !== 'testing')
56
				{
57
					header('Refresh:0;url='.$uri);
58
				}
59
				break;
60
			default:
61
				if (ENVIRONMENT !== 'testing')
62
				{
63
					header('Location: '.$uri, TRUE, $code);
64
				}
65
				break;
66
		}
67
68
		if (ENVIRONMENT !== 'testing')
69
		{
70
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
71
		}
72
		else
73
		{
74
			while (ob_get_level() > 1)
75
			{
76
				ob_end_clean();
77
			}
78
79
			throw new CIPHPUnitTestRedirectException('Redirect to ' . $uri, $code);
80
		}
81
	}
82
}
83