Failed Conditions
Push — master ( 2fcdae...1d0766 )
by Florent
04:40
created

ClaimManager::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\OpenIdConnect\UserInfo\Claim;
15
16
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
17
18
class ClaimManager
19
{
20
    /**
21
     * @var Claim[]
22
     */
23
    private $claims = [];
24
25
    /**
26
     * @param Claim $claim
27
     *
28
     * @return ClaimManager
29
     */
30
    public function add(Claim $claim): self
31
    {
32
        $this->claims[$claim->name()] = $claim;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @return Claim[]
39
     */
40
    public function list(): array
41
    {
42
        return array_keys($this->claims);
43
    }
44
45
    /**
46
     * @return Claim[]
47
     */
48
    public function all(): array
49
    {
50
        return $this->claims;
51
    }
52
53
    public function has(string $claim): bool
54
    {
55
        return array_key_exists($claim, $this->claims);
56
    }
57
58
    public function get(string $claim): Claim
59
    {
60
        if (!$this->has($claim)) {
61
            throw new \InvalidArgumentException(sprintf('Unsupported claim "%s".', $claim));
62
        }
63
64
        return $this->claims[$claim];
65
    }
66
67
    public function getUserInfo(UserAccount $userAccount, array $claims, array $claimLocales): array
68
    {
69
        $result = [];
70
        $claimLocale[] = null;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$claimLocale was never initialized. Although not strictly required by PHP, it is generally a good practice to add $claimLocale = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
71
        foreach ($claims as $claimName => $config) {
72
            if ($this->has($claimName)) {
73
                $claim = $this->get($claimName);
74
                foreach ($claimLocales as $claimLocale) {
75
                    if ($claim->isAvailableForUserAccount($userAccount, $claimLocale)) {
76
                        $result[$claimName] = $claim->getForUserAccount($userAccount, $claimLocale);
77
                    }
78
                }
79
            }
80
        }
81
82
        return $result;
83
    }
84
}
85