Completed
Push — master ( a82d7f...7812ce )
by Kenji
02:40
created

CIPHPUnitTestSuperGlobal::set_Global()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
class CIPHPUnitTestSuperGlobal
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
	public static function set_Global($name, $value)
0 ignored issues
show
Coding Style introduced by
set_Global uses the super-global variable $GLOBALS 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...
14
	{
15
		$GLOBALS[$name] = $value;
16
	}
17
18
	public function set_POST($params)
0 ignored issues
show
Coding Style introduced by
set_POST 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...
Coding Style introduced by
set_POST uses the super-global variable $_POST 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...
19
	{
20
		if (is_array($params))
21
		{
22
			if ($_SERVER['REQUEST_METHOD'] === 'POST')
23
			{
24
				$_POST = $params;
25
			}
26
		}
27
	}
28
29
	public function set_GET(&$argv, $params)
0 ignored issues
show
Coding Style introduced by
set_GET uses the super-global variable $_GET 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...
Coding Style introduced by
set_GET 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...
30
	{
31
		if (is_string($argv))
32
		{
33
			$query_string = $this->getQueryString($argv);
34
			if ($query_string !== null)
35
			{
36
				// Set $_GET if URI string has query string
37
				parse_str($query_string, $_GET);
38
				// Remove query string from URI string
39
				$argv = substr($argv, 0, -strlen($query_string)-1);
40
			}
41
		}
42
43
		if (is_array($params))
44
		{
45
			if ($_SERVER['REQUEST_METHOD'] === 'GET')
46
			{
47
				// if GET params are passed, overwrite $_GET
48
				if ($params !== [])
49
				{
50
					$_GET = $params;
51
				}
52
			}
53
		}
54
	}
55
56
	public function set_SERVER_REQUEST_URI($argv)
0 ignored issues
show
Coding Style introduced by
set_SERVER_REQUEST_URI uses the super-global variable $_GET 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...
Coding Style introduced by
set_SERVER_REQUEST_URI 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...
57
	{
58
		$path = '';
59
		if (is_string($argv))
60
		{
61
			$path = $argv;
62
		}
63
		elseif (is_array($argv))
64
		{
65
			// Generate URI path from array of controller, method, arg, ...
66
			$path = implode('/', $argv);
67
		}
68
69
		if ($_GET !== [])
70
		{
71
			$_SERVER['REQUEST_URI'] =
72
				'/' . $path . '?'
73
				. http_build_query($_GET);
74
		}
75
		else
76
		{
77
			$_SERVER['REQUEST_URI'] = '/' . $path;
78
		}
79
	}
80
81
	/**
82
	 * Parse URI string and Get query string
83
	 * 
84
	 * @param string $uri
85
	 * @return string|null
86
	 * @throws LogicException
87
	 */
88
	protected function getQueryString($uri)
89
	{
90
		$query_string = parse_url('http://localhost/'.$uri, PHP_URL_QUERY);
91
92
		if ($query_string === false)
93
		{
94
			throw new LogicException('Bad URI string: ' . $uri);
95
		}
96
97
		return $query_string;
98
	}
99
100
	/**
101
	 * Set HTTP request header to $_SERVER
102
	 * 
103
	 * @param string $name  header name
104
	 * @param string $value value
105
	 */
106
	public function set_SERVER_HttpHeader($name, $value)
0 ignored issues
show
Coding Style introduced by
set_SERVER_HttpHeader 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...
107
	{
108
		$normalized_name = str_replace('-', '_', strtoupper($name));
109
110
		if (
111
			$normalized_name === 'CONTENT_LENGTH' 
112
			|| $normalized_name === 'CONTENT_TYPE'
113
		)
114
		{
115
			$key = $normalized_name;
116
		}
117
		else
118
		{
119
			$key = 'HTTP_' . $normalized_name;
120
		}
121
122
		$_SERVER[$key] = $value;
123
	}
124
}
125