Completed
Push — master ( 9de1be...100987 )
by David
8s
created

TestSessionStorage::getMetadataBag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCacheBundle\Tests\Functional\Fixtures\Session;
13
14
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
17
18
/**
19
 * {@inheritdoc}
20
 */
21
class TestSessionStorage implements SessionStorageInterface
22
{
23
    private $started = false;
24
25
    private $bags = array();
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function start()
31
    {
32
        return $this->started = true;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function isStarted()
39
    {
40
        return $this->started;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getId()
47
    {
48
        return 'test';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function setId($id)
55
    {
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getName()
62
    {
63
        return 'TESTSESSID';
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function setName($name)
70
    {
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function regenerate($destroy = false, $lifetime = null)
77
    {
78
        return true;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function save()
85
    {
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function clear()
92
    {
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getBag($name)
99
    {
100
        return $this->bags[$name];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function registerBag(SessionBagInterface $bag)
107
    {
108
        if ($bag->getName() == 'attributes') {
109
            $bag->set('_security_secured_area', serialize(new UsernamePasswordToken('user', 'user', 'in_memory', array('ROLE_USER'))));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\HttpFo...ion\SessionBagInterface as the method set() does only exist in the following implementations of said interface: Symfony\Component\HttpFo...\Attribute\AttributeBag, Symfony\Component\HttpFo...\NamespacedAttributeBag, Symfony\Component\HttpFo...lash\AutoExpireFlashBag, Symfony\Component\HttpFo...\Session\Flash\FlashBag.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
110
        }
111
112
        $this->bags[$bag->getName()] = $bag;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getMetadataBag()
119
    {
120
        return;
121
    }
122
}
123