Completed
Pull Request — master (#363)
by Yanick
06:20 queued 01:48
created

RoleProvider::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 4
cts 8
cp 0.5
rs 9.2
cc 4
eloc 7
nc 2
nop 1
crap 6
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\UserContext;
13
14
use FOS\HttpCache\UserContext\ContextProvider;
15
use FOS\HttpCache\UserContext\UserContext;
16
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Role\RoleInterface;
19
use Symfony\Component\Security\Core\SecurityContextInterface;
20
21
/**
22
 * The RoleProvider adds roles to the UserContext for the hash generation.
23
 */
24
class RoleProvider implements ContextProvider
25
{
26
    /**
27
     * @var SecurityContextInterface|null
28
     */
29
    private $context;
30
31
    /**
32
     * Create the role provider with a security context.
33
     *
34
     * The security context is optional to not fail on routes that have no
35
     * firewall. It is however not valid to call updateUserContext when not in
36
     * a firewall context.
37
     *
38
     * @param SecurityContextInterface|TokenStorageInterface $context
0 ignored issues
show
Documentation introduced by
Should the type for parameter $context not be SecurityContextInterface...enStorageInterface|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...
39
     */
40 18
    public function __construct($context = null)
41
    {
42 18
        if ($context
43
            && !$context instanceof SecurityContextInterface
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Securi...ecurityContextInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
44
            && !$context instanceof TokenStorageInterface
45
        ) {
46
            throw new \InvalidArgumentException(
47
                'Context must implement either TokenStorageInterface or SecurityContextInterface'
48
            );
49
        }
50
51 18
        $this->context = $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $context can also be of type object<Symfony\Component...\TokenStorageInterface>. However, the property $context is declared as type object<Symfony\Component...yContextInterface>|null. 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...
52 18
    }
53
54
    /**
55
     * {@inheritdoc}
56
     *
57
     * @throws InvalidConfigurationException when called without a security context being set
58
     */
59 3
    public function updateUserContext(UserContext $context)
60
    {
61 3
        if (null === $this->context) {
62 1
            throw new InvalidConfigurationException('The context hash URL must be under a firewall.');
63
        }
64
65 2
        if (null === $token = $this->context->getToken()) {
66 1
            return;
67
        }
68
69 1
        $roles = array_map(function (RoleInterface $role) {
70 1
            return $role->getRole();
71 1
        }, $token->getRoles());
72
73
        // Order is not important for roles and should not change hash.
74 1
        sort($roles);
75
76 1
        $context->addParameter('roles', $roles);
77 1
    }
78
}
79