Failed Conditions
Push — master ( 5cdde8...0d92d9 )
by Florent
04:33
created

ClaimManager::getUserInfo()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 21
nc 9
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
                        $value = $claim->getForUserAccount($userAccount, $claimLocale);
77
                        switch (true) {
78
                            case is_array($config) && array_key_exists('value', $config):
79
                                if ($claim === $config['value']) {
80
                                    $result[$claimName] = $value;
81
                                }
82
83
                                break;
84
                            case is_array($config) && array_key_exists('values', $config) && is_array($config['values']):
85
                                if (in_array($claim, $config['values'])) {
86
                                    $result[$claimName] = $value;
87
                                }
88
89
                                break;
90
                            default:
91
                                $result[$claimName] = $value;
92
                        }
93
                    }
94
                }
95
            }
96
        }
97
98
        return $result;
99
    }
100
}
101