GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7b42e1...c93f43 )
by Marius
10:41 queued 04:09
created

SessionRequestTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 102
Duplicated Lines 6.86 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 12.5%

Importance

Changes 0
Metric Value
dl 7
loc 102
ccs 5
cts 40
cp 0.125
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hasSession() 0 3 1
A initSession() 0 5 3
A getSessionVars() 0 3 1
A hasSessionVar() 0 5 1
A getSessionName() 0 3 1
B startSession() 0 20 9
A destroySession() 0 8 1
A getSessionVar() 7 7 2
A setSessionVar() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @package presentation
4
 * @subpackage requests
5
 * @author marius orcsik <[email protected]>
6
 * @date 21.02.15
7
 */
8
namespace vsc\presentation\requests;
9
10
use vsc\infrastructure\vsc;
11
12
trait SessionRequestTrait {
13
	protected $aSessionVars = [];
14
15 17
	protected function initSession() {
0 ignored issues
show
Coding Style introduced by
initSession 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...
16 17
		if (static::hasSession() && isset($_SESSION)) {
17
			$this->aSessionVars = $_SESSION;
18
		}
19 17
	}
20
21
	/**
22
	 * @return bool
23
	 */
24 17
	public static function hasSession() {
25 17
		return (session_id() != '');
26
	}
27
28
	/**
29
	 * @return array
30
	 */
31
	public function getSessionVars() {
32
		return $this->aSessionVars;
33
	}
34
35
	/**
36
	 * @param string $sVarName
37
	 * @return bool
38
	 */
39
	public function hasSessionVar($sVarName) {
40
		return (
41
			array_key_exists($sVarName, $this->aSessionVars)
42
		);
43
	}
44
45
	/**
46
	 * @return string
47
	 */
48
	public static function getSessionName() {
49
		return session_id();
50
	}
51
52
	/**
53
	 * @param string $sSessionName
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sSessionName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
54
	 * @throws ExceptionRequest
55
	 */
56
	public static function startSession($sSessionName = null) {
0 ignored issues
show
Coding Style introduced by
startSession 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...
57
		if (!static::hasSession()) {
58
			if (((double)PHP_VERSION >= 5.4 && session_status() == PHP_SESSION_DISABLED)) {
59
				throw new ExceptionRequest('Sessions are not available');
60
			}
61
62
			if (((double)PHP_VERSION >= 5.4 && session_status() == PHP_SESSION_NONE)) {
63
				$oRequest = vsc::getEnv()->getHttpRequest();
64
				if (!vsc::isCli()) {
65
					session_set_cookie_params(0, '/', $oRequest->getUriObject()->getHost(), HttpRequestA::isSecure(), true);
66
				}
67
				if (@session_start()) {
68
					$_SESSION = array();
69
					if (!is_null($sSessionName)) {
70
						session_id($sSessionName);
71
					}
72
				}
73
			}
74
		}
75
	}
76
77
	/**
78
	 * return void
79
	 */
80
	public static function destroySession() {
81
		$aSessionCookieParams = session_get_cookie_params();
82
83
		setcookie(session_name(), "", -1, $aSessionCookieParams['path'], $aSessionCookieParams['domain'], $aSessionCookieParams['secure'], $aSessionCookieParams['httponly']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
84
		session_unset();
85
		session_destroy();
86
		session_write_close();
87
	}
88
89
	/**
90
	 *
91
	 * @param string $sVarName
92
	 * @return mixed
93
	 */
94 View Code Duplication
	public function getSessionVar($sVarName) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
95
		if (array_key_exists($sVarName, $this->aSessionVars)) {
96
			return HttpRequestA::getDecodedVar($this->aSessionVars[$sVarName]);
97
		} else {
98
			return null;
99
		}
100
	}
101
102
	/**
103
	 * @param string $sVarName
104
	 * @param string $sVarValue
105
	 * @return bool
106
	 */
107
	public function setSessionVar($sVarName, $sVarValue) {
0 ignored issues
show
Coding Style introduced by
setSessionVar 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...
108
		$this->aSessionVars[$sVarName] = $sVarValue;
109
		$_SESSION[$sVarName] = $sVarValue;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
110
		return true;
111
	}
112
113
}
114