Failed Conditions
Push — master ( ed7204...c7c4bc )
by Florent
01:40
created

PublicKeyCredentialDescriptor::splitId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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 U2FAuthentication\Fido2;
15
16
class PublicKeyCredentialDescriptor implements \JsonSerializable
17
{
18
    public const PUBLIC_KEY_CREDENTIAL_TYPE_PUBLIC_KEY = 'public-key';
19
20
    public const AUTHENTICATOR_TRANSPORT_USB = 'usb';
21
    public const AUTHENTICATOR_TRANSPORT_NFC = 'nfc';
22
    public const AUTHENTICATOR_TRANSPORT_BLE = 'ble';
23
    public const AUTHENTICATOR_TRANSPORT_INTERNAL = 'internal';
24
25
    private $type;
26
27
    private $id;
28
29
    /**
30
     * @var string[]
31
     */
32
    private $transports;
33
34
    /**
35
     * @param string[] $transports
36
     */
37
    public function __construct(string $type, string $id, array $transports = [])
38
    {
39
        $this->type = $type;
40
        $this->id = $id;
41
        $this->transports = $transports;
42
    }
43
44
    public function getType(): string
45
    {
46
        return $this->type;
47
    }
48
49
    public function getId(): string
50
    {
51
        return $this->id;
52
    }
53
54
    /**
55
     * @return string[]
56
     */
57
    public function getTransports(): array
58
    {
59
        return $this->transports;
60
    }
61
62
    public function jsonSerialize()
63
    {
64
        $json = [
65
            'type' => $this->type,
66
            'id' => base64_encode($this->id),
67
        ];
68
        if ($this->transports && !empty($this->transports)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->transports of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69
            $json['transports'] = $this->transports;
70
        }
71
72
        return $json;
73
    }
74
}
75