JWELoader   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A loadAndDecryptWithKeySet() 0 22 2
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\JWELoadingFailureEvent;
17
use Jose\Bundle\JoseFramework\Event\JWELoadingSuccessEvent;
18
use Jose\Component\Checker\HeaderCheckerManager;
0 ignored issues
show
Bug introduced by Florent Morselli
This use statement conflicts with another class in this namespace, Jose\Bundle\JoseFramewor...es\HeaderCheckerManager.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Jose\Component\Core\JWKSet;
20
use Jose\Component\Encryption\JWE;
21
use Jose\Component\Encryption\JWEDecrypter;
0 ignored issues
show
Bug introduced by Florent Morselli
This use statement conflicts with another class in this namespace, Jose\Bundle\JoseFramework\Services\JWEDecrypter.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
22
use Jose\Component\Encryption\JWELoader as BaseJWELoader;
23
use Jose\Component\Encryption\Serializer\JWESerializerManager;
24
use Psr\EventDispatcher\EventDispatcherInterface;
25
use Throwable;
26
27
final class JWELoader extends BaseJWELoader
28
{
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34
    public function __construct(JWESerializerManager $serializerManager, JWEDecrypter $jweDecrypter, ?HeaderCheckerManager $headerCheckerManager, EventDispatcherInterface $eventDispatcher)
35
    {
36
        parent::__construct($serializerManager, $jweDecrypter, $headerCheckerManager);
37
        $this->eventDispatcher = $eventDispatcher;
38
    }
39
40
    public function loadAndDecryptWithKeySet(string $token, JWKSet $keyset, ?int &$recipient): JWE
41
    {
42
        try {
43
            $jwe = parent::loadAndDecryptWithKeySet($token, $keyset, $recipient);
44
            $this->eventDispatcher->dispatch(new JWELoadingSuccessEvent(
0 ignored issues
show
Documentation introduced by Spomky
new \Jose\Bundle\JoseFra...e, $keyset, $recipient) is of type object<Jose\Bundle\JoseF...JWELoadingSuccessEvent>, 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...
45
                $token,
46
                $jwe,
47
                $keyset,
48
                $recipient
49
            ));
50
51
            return $jwe;
52
        } catch (Throwable $throwable) {
53
            $this->eventDispatcher->dispatch(new JWELoadingFailureEvent(
0 ignored issues
show
Documentation introduced by Spomky
new \Jose\Bundle\JoseFra...n, $keyset, $throwable) is of type object<Jose\Bundle\JoseF...JWELoadingFailureEvent>, 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...
54
                $token,
55
                $keyset,
56
                $throwable
57
            ));
58
59
            throw $throwable;
60
        }
61
    }
62
}
63