Completed
Pull Request — master (#591)
by thomas
19:03
created

PublicKeyFactory::validateHex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class PublicKeyFactory
15
{
16
    /**
17
     * @param string $hex
18
     * @param EcAdapterInterface|null $ecAdapter
19
     * @return PublicKeyInterface
20
     * @throws \Exception
21
     */
22 86
    public static function fromHex(string $hex, EcAdapterInterface $ecAdapter = null): PublicKeyInterface
23
    {
24 86
        return self::fromBuffer(Buffer::hex($hex), $ecAdapter);
25
    }
26
27
    /**
28
     * @param BufferInterface $buffer
29
     * @param EcAdapterInterface|null $ecAdapter
30
     * @return PublicKeyInterface
31
     */
32 108
    public static function fromBuffer(BufferInterface $buffer, EcAdapterInterface $ecAdapter = null): PublicKeyInterface
33
    {
34 108
        return EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter)
35 108
            ->parse($buffer)
36
        ;
37
    }
38
39
    /**
40
     * @param BufferInterface|string $hex
41
     * @param EcAdapterInterface|null $ecAdapter
42
     * @return bool
43
     */
44 2
    public static function validateHex($hex, EcAdapterInterface $ecAdapter = null)
45
    {
46
        try {
47 2
            self::fromHex($hex, $ecAdapter);
0 ignored issues
show
Bug introduced by
It seems like $hex defined by parameter $hex on line 44 can also be of type object<BitWasp\Buffertools\BufferInterface>; however, BitWasp\Bitcoin\Key\PublicKeyFactory::fromHex() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48 2
            return true;
49 2
        } catch (\Exception $e) {
50 2
            return false;
51
        }
52
    }
53
}
54