Failed Conditions
Push — master ( 1312dd...bb342a )
by Florent
04:40
created

isAvailableForUserAccount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 4
nc 2
nop 2
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\AuthenticationContextClassReferenceSupport;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
18
use OAuth2Framework\Component\Core\UserAccount\UserAccountManager;
19
20
final class AuthenticationContextClassReference implements Claim
21
{
22
    private const CLAIM_NAME = 'acr';
23
24
    /**
25
     * @var UserAccountManager|AuthenticationContextClassReferenceSupport
26
     */
27
    private $userAccountManager;
28
29
    public function __construct(UserAccountManager $userAccountManager)
30
    {
31
        $this->userAccountManager = $userAccountManager;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function name(): string
38
    {
39
        return self::CLAIM_NAME;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function isAvailableForUserAccount(UserAccount $userAccount, ?string $claimLocale): bool
46
    {
47
        if (null === $this->userAccountManager || !$this->userAccountManager instanceof AuthenticationContextClassReferenceSupport) {
48
            return false;
49
        }
50
51
        return null !== $this->userAccountManager->getAuthenticationContextClassReferenceFor($userAccount);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getForUserAccount(UserAccount $userAccount, ?string $claimLocale)
58
    {
59
        return $this->userAccountManager->getAuthenticationContextClassReferenceFor($userAccount);
0 ignored issues
show
Bug introduced by
The method getAuthenticationContextClassReferenceFor does only exist in OAuth2Framework\Componen...xtClassReferenceSupport, but not in OAuth2Framework\Componen...ount\UserAccountManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
60
    }
61
}
62