Parser::parse()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 25
rs 9.2222
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Serializer\Signature\Der;
5
6
use FG\ASN1\ASNObject;
7
use FG\ASN1\Identifier;
8
use FG\ASN1\Universal\Integer;
9
use Mdanter\Ecc\Crypto\Signature\Signature;
10
use Mdanter\Ecc\Crypto\Signature\SignatureInterface;
11
use Mdanter\Ecc\Exception\SignatureDecodeException;
12
13
class Parser
14
{
15
    /**
16
     * @param string $binary
17
     * @return SignatureInterface
18
     * @throws \FG\ASN1\Exception\ParserException
19
     */
20
    public function parse(string $binary): SignatureInterface
21
    {
22
        $offsetIndex = 0;
23
        $asnObject = ASNObject::fromBinary($binary, $offsetIndex);
24
25
        if ($offsetIndex != strlen($binary)) {
26
            throw new SignatureDecodeException('Invalid data.');
27
        }
28
29
        // Set inherits from Sequence, so use getType!
30
        if ($asnObject->getType() !== Identifier::SEQUENCE) {
31
            throw new SignatureDecodeException('Invalid tag for sequence.');
32
        }
33
34
        if ($asnObject->getNumberofChildren() !== 2) {
0 ignored issues
show
Bug introduced by
The method getNumberofChildren() does not exist on FG\ASN1\ASNObject. It seems like you code against a sub-type of FG\ASN1\ASNObject such as FG\ASN1\Construct. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        if ($asnObject->/** @scrutinizer ignore-call */ getNumberofChildren() !== 2) {
Loading history...
35
            throw new SignatureDecodeException('Invalid data.');
36
        }
37
38
        if (!($asnObject[0] instanceof Integer && $asnObject[1] instanceof Integer)) {
39
            throw new SignatureDecodeException('Invalid data.');
40
        }
41
42
        return new Signature(
43
            gmp_init($asnObject[0]->getContent(), 10),
0 ignored issues
show
Bug introduced by
It seems like gmp_init($asnObject[0]->getContent(), 10) can also be of type resource; however, parameter $r of Mdanter\Ecc\Crypto\Signa...ignature::__construct() does only seem to accept GMP, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            /** @scrutinizer ignore-type */ gmp_init($asnObject[0]->getContent(), 10),
Loading history...
44
            gmp_init($asnObject[1]->getContent(), 10)
0 ignored issues
show
Bug introduced by
It seems like gmp_init($asnObject[1]->getContent(), 10) can also be of type resource; however, parameter $s of Mdanter\Ecc\Crypto\Signa...ignature::__construct() does only seem to accept GMP, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            /** @scrutinizer ignore-type */ gmp_init($asnObject[1]->getContent(), 10)
Loading history...
45
        );
46
    }
47
}
48