JWEDecrypter::decryptUsingKeySet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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 Jose\Bundle\JoseFramework\Services;
15
16
use Jose\Bundle\JoseFramework\Event\JWEDecryptionFailureEvent;
17
use Jose\Bundle\JoseFramework\Event\JWEDecryptionSuccessEvent;
18
use Jose\Component\Core\AlgorithmManager;
19
use Jose\Component\Core\JWK;
20
use Jose\Component\Core\JWKSet;
21
use Jose\Component\Encryption\Compression\CompressionMethodManager;
22
use Jose\Component\Encryption\JWE;
23
use Jose\Component\Encryption\JWEDecrypter as BaseJWEDecrypter;
24
use Psr\EventDispatcher\EventDispatcherInterface;
25
26
final class JWEDecrypter extends BaseJWEDecrypter
27
{
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    private $eventDispatcher;
32
33
    public function __construct(AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionMethodManager, EventDispatcherInterface $eventDispatcher)
34
    {
35
        parent::__construct($keyEncryptionAlgorithmManager, $contentEncryptionAlgorithmManager, $compressionMethodManager);
36
        $this->eventDispatcher = $eventDispatcher;
37
    }
38
39
    public function decryptUsingKeySet(JWE &$jwe, JWKSet $jwkset, int $recipient, JWK &$jwk = null, ?JWK $senderKey = null): bool
40
    {
41
        $success = parent::decryptUsingKeySet($jwe, $jwkset, $recipient, $jwk, $senderKey);
42
        if ($success) {
43
            $this->eventDispatcher->dispatch(new JWEDecryptionSuccessEvent(
0 ignored issues
show
Documentation introduced by
new \Jose\Bundle\JoseFra...kset, $jwk, $recipient) is of type object<Jose\Bundle\JoseF...DecryptionSuccessEvent>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
                $jwe,
45
                $jwkset,
46
                $jwk,
0 ignored issues
show
Bug introduced by
It seems like $jwk defined by parameter $jwk on line 39 can be null; however, Jose\Bundle\JoseFramewor...essEvent::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
47
                $recipient
48
            ));
49
        } else {
50
            $this->eventDispatcher->dispatch(new JWEDecryptionFailureEvent(
0 ignored issues
show
Documentation introduced by
new \Jose\Bundle\JoseFra...ureEvent($jwe, $jwkset) is of type object<Jose\Bundle\JoseF...DecryptionFailureEvent>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
                $jwe,
52
                $jwkset
53
            ));
54
        }
55
56
        return $success;
57
    }
58
}
59