Passed
Push — master ( b0f5d7...c171ec )
by Sergey
02:30
created

Session::getKeysData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 2
1
<?php
2
3
namespace PhpConsole\Storage;
4
5
/**
6
 * $_SESSION storage for postponed response data. Is used by default.
7
 *
8
 * @package PhpConsole
9
 * @version 3.1
10
 * @link http://consle.com
11
 * @author Sergey Barbushin http://linkedin.com/in/barbushin
12
 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
13
 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
14
 */
15
class Session extends AllKeysList {
16
17
	protected $sessionKey;
18
19
	/**
20
	 * @param string $sessionKey Key name in $_SESSION variable
21
	 * @param bool $autoStart Start session if it's not started
22
	 */
23 3
	public function __construct($sessionKey = '__PHP_Console_postponed', $autoStart = true) {
24 3
        	if($autoStart && (defined('PHP_SESSION_ACTIVE') ? session_status() != PHP_SESSION_ACTIVE : !session_id()) && !headers_sent()) {
25
        		session_start();
26
		}
27 3
		register_shutdown_function('session_write_close'); // force saving session data if session handler is overridden
28 3
		$this->sessionKey = $sessionKey;
29 3
	}
30
31 3
	protected function getKeysData() {
0 ignored issues
show
Coding Style introduced by
getKeysData uses the super-global variable $_SESSION 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...
32 3
		return isset($_SESSION[$this->sessionKey]) ? $_SESSION[$this->sessionKey] : array();
33
	}
34
35 3
	protected function saveKeysData(array $keysData) {
0 ignored issues
show
Coding Style introduced by
saveKeysData uses the super-global variable $_SESSION 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...
36 3
		$_SESSION[$this->sessionKey] = $keysData;
37 3
	}
38
}
39