Completed
Push — master ( 7a8275...fe77fb )
by thomas
114:31 queued 111:38
created

Signature::doEquals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Signature\DerSignatureSerializer;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BitWasp\Bitcoin\Crypto\E...ture\SignatureInterface.

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...
8
use BitWasp\Bitcoin\Serializable;
9
10
class Signature extends Serializable implements SignatureInterface
11
{
12
    /**
13
     * @var \GMP
14
     */
15
    private $r;
16
17
    /**
18
     * @var \GMP
19
     */
20
    private $s;
21
22
    /**
23
     * @var EcAdapter
24
     */
25
    private $ecAdapter;
26
27
    /**
28
     * @param EcAdapter $ecAdapter
29
     * @param \GMP $r
30
     * @param \GMP $s
31
     */
32 112
    public function __construct(EcAdapter $ecAdapter, \GMP $r, \GMP $s)
33
    {
34 112
        $this->ecAdapter = $ecAdapter;
35 112
        $this->r = $r;
36 112
        $this->s = $s;
37 112
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 105
    public function getR()
43
    {
44 105
        return $this->r;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 105
    public function getS()
51
    {
52 105
        return $this->s;
53
    }
54
55
    /**
56
     * @param Signature $signature
57
     * @return bool
58
     */
59 25
    public function doEquals(Signature $signature)
60
    {
61 25
        $math = $this->ecAdapter->getMath();
62 25
        return $math->equals($this->getR(), $signature->getR())
63 25
            && $math->equals($this->getS(), $signature->getS());
64
    }
65
66
    /**
67
     * @param SignatureInterface $signature
68
     * @return bool
69
     */
70 25
    public function equals(SignatureInterface $signature)
71
    {
72
        /** @var Signature $signature */
73 25
        return $this->doEquals($signature);
74
    }
75
76
    /**
77
     * @return \BitWasp\Buffertools\BufferInterface
78
     */
79 2
    public function getBuffer()
80
    {
81 2
        return (new DerSignatureSerializer($this->ecAdapter))->serialize($this);
82
    }
83
}
84