Completed
Pull Request — 2.x (#80)
by
unknown
02:00
created

Segment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 1
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Auth\Session;
10
11
/**
12
 *
13
 * A $_SESSION segment; it attaches to $_SESSION lazily (i.e., only after a
14
 * session becomes available.)
15
 *
16
 * @package Aura.Auth
17
 *
18
 */
19
class Segment implements SegmentInterface
20
{
21
    /**
22
     *
23
     * The name of the $_SESSION segment, typically a class name.
24
     *
25
     * @var string
26
     *
27
     */
28
    protected $name;
29
30
    /**
31
     *
32
     * Constructor.
33
     *
34
     * @param bool $name The name of the $_SESSION segment.
35
     *
36
     */
37 14
    public function __construct($name = 'Aura\Auth\Auth')
38
    {
39 14
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name can also be of type boolean. However, the property $name is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
40 14
    }
41
42
    /**
43
     *
44
     * Gets a value from the segment.
45
     *
46
     * @param mixed $key A key for the segment value.
47
     *
48
     * @param mixed $alt Return this value if the segment key does not exist.
49
     *
50
     * @return mixed
51
     *
52
     */
53 2
    public function get($key, $alt = null)
0 ignored issues
show
Coding Style introduced by
get 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...
54
    {
55 2
        if (isset($_SESSION[$this->name][$key])) {
56 1
            return $_SESSION[$this->name][$key];
57
        }
58
59 2
        return $alt;
60
    }
61
62
    /**
63
     *
64
     * Sets a value in the segment.
65
     *
66
     * @param mixed $key The key in the segment.
67
     *
68
     * @param mixed $val The value to set.
69
     *
70
     */
71 2
    public function set($key, $val)
0 ignored issues
show
Coding Style introduced by
set 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...
72
    {
73 2
        if (! isset($_SESSION)) {
74 1
            return;
75
        }
76
77 1
        $_SESSION[$this->name][$key] = $val;
78 1
    }
79
}
80