Passed
Branch master (f1e8df)
by Milad
05:36 queued 02:45
created

RsaPublicKey::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace MiladRahimi\Jwt\Cryptography\Keys;
4
5
use MiladRahimi\Jwt\Exceptions\InvalidKeyException;
6
use Throwable;
7
8
class RsaPublicKey
9
{
10
    /**
11
     * @var resource Key file resource handler
12
     */
13
    private $resource;
14
15
    /**
16
     * @throws InvalidKeyException
17
     */
18
    public function __construct(string $filePath)
19
    {
20
        try {
21
            $this->resource = openssl_pkey_get_public(file_get_contents(realpath($filePath)));
0 ignored issues
show
Documentation Bug introduced by
It seems like openssl_pkey_get_public(...s(realpath($filePath))) can also be of type OpenSSLAsymmetricKey. However, the property $resource is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
22
        } catch (Throwable $e) {
23
            throw new InvalidKeyException('Failed to read the key.', 0, $e);
24
        }
25
26
        if ($this->resource === false) {
27
            throw new InvalidKeyException(openssl_error_string());
28
        }
29
    }
30
31
    /**
32
     * @return resource
33
     */
34
    public function getResource()
35
    {
36
        return $this->resource;
37
    }
38
}
39