Failed Conditions
Push — master ( 7c3864...930f9b )
by Florent
14:15
created

Claim/AuthenticationContextClassReference.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function name(): string
35
    {
36
        return self::CLAIM_NAME;
37
    }
38
39
    public function isAvailableForUserAccount(UserAccount $userAccount, ?string $claimLocale): bool
40
    {
41
        if (null === $this->userAccountManager || !$this->userAccountManager instanceof AuthenticationContextClassReferenceSupport) {
42
            return false;
43
        }
44
45
        return null !== $this->userAccountManager->getAuthenticationContextClassReferenceFor($userAccount);
46
    }
47
48
    public function getForUserAccount(UserAccount $userAccount, ?string $claimLocale)
49
    {
50
        return $this->userAccountManager->getAuthenticationContextClassReferenceFor($userAccount);
0 ignored issues
show
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...
51
    }
52
}
53